projectsAction.js
4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
});
}
};