notificationAction.js 3.38 KB
import axios from "axios";
import qs from "qs";
import { FETCH_NOTIFICATIONS_FAIL, FETCH_NOTIFICATIONS_REQUEST, FETCH_NOTIFICATIONS_SUCCESS } from "../constants/notificationConstants";

export const getNotifications =
  ({ id }) =>
  async dispatch => {
    try {
      dispatch({
        type: FETCH_NOTIFICATIONS_REQUEST
      });

      const config = {
        headers: {
          "Content-Type": "application/json"
        }
      };
      const query = {
        populate: ["users_permissions_user"],
        filters: {
          users_permissions_user: {
            id: { $eq: id }
          },
          status: "pending"
        },
        sort: ["createdAt:desc"]
      };

      const queryString = qs.stringify(query, {
        encodeValuesOnly: true
      });

      const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/notifications?${queryString}`, config);
      console.log("response > ", response.data);
      dispatch({
        type: FETCH_NOTIFICATIONS_SUCCESS,
        payload: response.data
      });
      //   console.log("gift card here");

      return response.data;
    } catch (error) {
      dispatch({
        type: FETCH_NOTIFICATIONS_FAIL,
        payload: error.response.data
      });
    }
  };

  
export const createNotification = async (data) => {
    console.log("asd here");
    try {
      //   dispatch({
      //     type: FETCH_NOTIFICATIONS_REQUEST
      //   });
  
      const config = {
        headers: {
          "Content-Type": "application/json"
        }
      };
      // const query = {
      //   populate: ["users_permissions_user"],
      //   filters: {
      //     users_permissions_user: {
      //       id: { $eq: id }
      //     }
      //   }
      // };
  
      //   const queryString = qs.stringify(query, {
      //     encodeValuesOnly: true
      //   });
  
      const response = await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/notifications`, { data: { ...data } }, config);
      console.log("response here ", response.data);
      //   dispatch({
      //     type: FETCH_NOTIFICATIONS_SUCCESS,
      //     payload: response.data
      //   });
      //   console.log("gift card here");
  
      return response.data;
    } catch (error) {
      //   dispatch({
      //     type: FETCH_NOTIFICATIONS_FAIL,
      //     payload: error.response.data
      //   });
    }
  };

  
export const changeNotificationStatus = async ({ id, status }) => {
  try {
    //   dispatch({
    //     type: FETCH_NOTIFICATIONS_REQUEST
    //   });

    const config = {
      headers: {
        "Content-Type": "application/json"
      }
    };
    // const query = {
    //   populate: ["users_permissions_user"],
    //   filters: {
    //     users_permissions_user: {
    //       id: { $eq: id }
    //     }
    //   }
    // };

    //   const queryString = qs.stringify(query, {
    //     encodeValuesOnly: true
    //   });

    const response = await axios.put(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/notifications/${id}`, { data: { status } }, config);
    console.log("response here ", response.data);
    //   dispatch({
    //     type: FETCH_NOTIFICATIONS_SUCCESS,
    //     payload: response.data
    //   });
    //   console.log("gift card here");

    return response.data;
  } catch (error) {
    //   dispatch({
    //     type: FETCH_NOTIFICATIONS_FAIL,
    //     payload: error.response.data
    //   });
  }
};