import ApplicationService from "../../../services/ApplicationService"; export const getAreas = () => { return async (dispatch) => { ApplicationService.http() .post( "/graphql", { query: ` { areas { id tag isDeleted cities { id name coordinates country { id name } } } } `, }, { headers: { Authorization: "Bearer " + localStorage.getItem("accessToken"), }, } ) .then((response) => { const areas1 = response?.data?.data?.areas; const areas = areas1?.filter((area) => area.isDeleted != true) || []; dispatch({ type: "GET_AREAS", payload: { areas, }, }); }) .catch((error) => { console.error("Error fetching areas:", error); dispatch({ type: "GET_AREAS", payload: { areas: [], }, }); }); }; }; export const getAreasWithPaginate = (data) => { return async (dispatch) => { ApplicationService.http() .post( "/graphql", { query: ` { paginateAreas( pagination: { page: ${data.currentPage - 1}, rowsPerPage: ${ data.rowsPerPage } } criteria: { deleted:false } sortBy: [{ direction: ASC, field: "tag" }] ) { pageInfo { totalElements totalPages numberOfElements pageNumber pageSize } content { id tag countries { id countryCode name } cities{ id name country{ id name } } districts{ id name city{ id name country{ id name } } } neighborhoods{ id name district{ id name city{ id name country{ id name } } } } isDeleted defaultArea } } } `, }, { headers: { Authorization: "Bearer " + localStorage.getItem("accessToken"), }, } ) .then((response) => { const areasWithPaginate = response?.data?.data?.paginateAreas; dispatch({ type: "GET_AREAS_WITH_PAGINATE", payload: { areasWithPaginate, }, }); }); }; }; export const getAreasWithCriteria = (organizationId) => { return async (dispatch) => { // Don't make the request if organizationId is undefined, null, or empty if (!organizationId || organizationId === "undefined") { dispatch({ type: "GET_AREAS_WITH_CRITERIA", payload: { getAreasWithCriteria: [], }, }); return; } ApplicationService.http() .post( "/graphql", { query: ` { areas( criteria: { organizations:"${organizationId}" } ) { id tag countries { id countryCode name } cities { id name coordinates country{ id name } } districts { id name coordinates city{ id name country{ id name } } } neighborhoods { id name minLong maxLong minLat maxLat district{ id name city{ id name country{ id name } } } } isDeleted } } `, }, { headers: { Authorization: "Bearer " + localStorage.getItem("accessToken"), }, } ) .then((response) => { const areas1 = response?.data?.data?.areas; const getAreasWithCriteria = areas1?.filter( (area) => area.isDeleted != true ) || []; dispatch({ type: "GET_AREAS_WITH_CRITERIA", payload: { getAreasWithCriteria, }, }); }) .catch((error) => { console.error("Error fetching areas with criteria:", error); dispatch({ type: "GET_AREAS_WITH_CRITERIA", payload: { getAreasWithCriteria: [], }, }); }); }; }; export const getAreasByDataCenter = (dataCenterId) => { return async (dispatch) => { // Don't make the request if dataCenterId is undefined, null, or empty if (!dataCenterId || dataCenterId === "undefined") { dispatch({ type: "GET_AREAS_WITH_CRITERIA", payload: { getAreasWithCriteria: [], }, }); return; } ApplicationService.http() .post( "/graphql", { query: ` { dataCenter(id: "${dataCenterId}") { id dataCenter area { id tag countries { id countryCode name } cities { id name coordinates country{ id name } } districts { id name coordinates city{ id name country{ id name } } } neighborhoods { id name minLong maxLong minLat maxLat district{ id name city{ id name country{ id name } } } } isDeleted } } } `, }, { headers: { Authorization: "Bearer " + localStorage.getItem("accessToken"), }, } ) .then((response) => { const dataCenter = response?.data?.data?.dataCenter; let areas = []; if (dataCenter && dataCenter.area && !dataCenter.area.isDeleted) { areas = [dataCenter.area]; } dispatch({ type: "GET_AREAS_WITH_CRITERIA", payload: { getAreasWithCriteria: areas, }, }); }) .catch((error) => { console.error("Error fetching areas by data center:", error); dispatch({ type: "GET_AREAS_WITH_CRITERIA", payload: { getAreasWithCriteria: [], }, }); }); }; }; export const addArea = (data) => { const dataToJSON = JSON.stringify(data); const deleteQuotesFromKey = dataToJSON.replace(/"([^(")"]+)":/g, "$1:"); return async (dispatch) => { ApplicationService.http() .post( "/graphql", { query: ` mutation { createArea( input: ${deleteQuotesFromKey} ) { id tag countries { id countryCode name } cities { id name country{ id name } } districts { id name city{ id name country{ id name } } } neighborhoods { id name district{ id name city{ id name country{ id name } } } } isDeleted defaultArea } } `, }, { headers: { Authorization: "Bearer " + localStorage.getItem("accessToken"), }, } ) .then((response) => { dispatch({ type: "ADD_AREA", payload: { areas: response.data.data.createArea, }, }); }) .catch((error) => { console.log("error", error); }); }; }; export const updateArea = (data) => { let deleteIdFromData = { ...data }; delete deleteIdFromData.id; const dataToJSON = JSON.stringify(deleteIdFromData); const deleteQuotesFromKey = dataToJSON.replace(/"([^(")"]+)":/g, "$1:"); return async (dispatch) => { ApplicationService.http() .post( "/graphql", { query: ` mutation { updateArea(id: "` + data.id + `", input: ${deleteQuotesFromKey}) { id tag countries { id countryCode name } cities { id name country{ id name } } districts { id name city{ id name country{ id name } } } neighborhoods { id name district{ id name city{ id name country{ id name } } } } isDeleted defaultArea } } `, }, { headers: { Authorization: "Bearer " + localStorage.getItem("accessToken"), }, } ) .then((response) => { dispatch({ type: "UPDATE_AREA", payload: { areas: response.data.data.updateArea, }, }); }) .catch((error) => { console.log("error", error); }); }; }; export const deleteArea = (id) => { return async (dispatch) => { ApplicationService.http() .post( "/graphql", { query: `mutation { deleteArea(id:"${id}") } `, }, { headers: { Authorization: "Bearer " + localStorage.getItem("accessToken"), }, } ) .then(() => { dispatch({ type: "DELETE_AREA", payload: { id: id, }, }); }) .catch((error) => { console.log("error", error); }); }; };