import axios from "axios"; import qs from "qs"; import { FETCH_PROJECTS_REQUEST, FETCH_PROJECTS_SUCCESS, FETCH_PROJECTS_FAIL, FETCH_PROJECT_REQUEST, FETCH_PROJECT_SUCCESS, FETCH_PROJECT_FAIL, CLEAR_ERRORS, FETCH_SIMILAR_PROJECTS_REQUEST, FETCH_SIMILAR_PROJECTS_SUCCESS, FETCH_SIMILAR_PROJECTS_FAIL } from "../constants/projectsConstants"; export const getProjects = ({ loadedFrom = "home", currentPage = 1, resultsPerPage = 6, featuredOnHome = null, townshipId = null, locationId = null, projectType = null, configuration = null, published = true }) => async dispatch => { try { // dispatch({ // type: published ? PUBLISHED_ROOMS_REQUEST : ADMIN_ROOMS_REQUEST // }); dispatch({ type: FETCH_PROJECTS_REQUEST }); const query = { filters: {}, populate: ["projectImages", "projectImages.image", "location", "township"], pagination: { pageSize: resultsPerPage, page: currentPage }, // sort: ['title:asc'], // fields: ['title'], // publicationState: 'live', // locale: ['en'], publicationState: published ? "live" : "preview" }; // Add all the optional filters. if (featuredOnHome) query.filters["featuredOnHome"] = { $eq: featuredOnHome }; if (townshipId) query.filters["township"] = { id: { $eq: townshipId } }; if (locationId) query.filters["location"] = { id: { $eq: locationId } }; if (projectType) query.filters["projectType"] = { $eq: projectType }; if (configuration) { query.filters["configurations"] = { bedrooms: { $in: Number(configuration) } }; } /** If we are loading projects from anywhere other than the home page, then we load the configuration information also. */ if (loadedFrom !== "listing") { query.populate.push("configurations"); } const queryString = qs.stringify(query, { encodeValuesOnly: true }); const endpoint = `${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/projects?${queryString}`; console.log(`Final url: ${endpoint}`); const response = await axios.get(endpoint); dispatch({ // type: published ? PUBLISHED_ROOMS_SUCCESS : ADMIN_ROOMS_SUCCESS, type: FETCH_PROJECTS_SUCCESS, payload: response.data }); } catch (error) { console.log("Real error is: "); console.log(error.response.data); dispatch({ // type: published ? PUBLISHED_ROOMS_FAIL : ADMIN_ROOMS_FAIL, type: FETCH_PROJECTS_FAIL, payload: error.response.data }); } }; export const getProject = projectId => async dispatch => { try { dispatch({ type: FETCH_PROJECT_REQUEST }); const query = { filters: {}, populate: ["projectImages", "projectImages.image", "location", "township","township.amenities","township.amenities.icon", "configurations","configurations.unitPlanImages","configurations.unitPlanImages.image", "amenities", "amenities.icon", "floorPlanImages.image","masterPlanImages.image"], pagination: {} }; const queryString = qs.stringify(query, { encodeValuesOnly: true }); const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/projects/${projectId}?${queryString}`); dispatch({ // type: published ? PUBLISHED_ROOMS_SUCCESS : ADMIN_ROOMS_SUCCESS, type: FETCH_PROJECT_SUCCESS, payload: response.data }); } catch (error) { console.log("Real error is: "); console.log(error.response.data); dispatch({ // type: published ? PUBLISHED_ROOMS_FAIL : ADMIN_ROOMS_FAIL, type: FETCH_PROJECT_FAIL, payload: error.response.data }); } }; export const getSimilarProjects = (townshipId, noOfBeds) => async dispatch => { try { dispatch({ type: FETCH_SIMILAR_PROJECTS_REQUEST }); const query = { filters: { configurations: { bedrooms: { $in: noOfBeds } } }, populate: ["projectImages", "projectImages.image", "location", "township", "configurations"], pagination: {} }; if (townshipId) query.filters["township"] = { id: { $eq: townshipId}}; const queryString = qs.stringify(query, { encodeValuesOnly: true }); const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/projects?${queryString}`); dispatch({ // type: published ? PUBLISHED_ROOMS_SUCCESS : ADMIN_ROOMS_SUCCESS, type: FETCH_SIMILAR_PROJECTS_SUCCESS, payload: response.data }); } catch (error) { console.log("Real error is: "); console.log(error.response.data); dispatch({ // type: published ? PUBLISHED_ROOMS_FAIL : ADMIN_ROOMS_FAIL, type: FETCH_SIMILAR_PROJECTS_FAIL, payload: error.response.data }); } };