roomActions.js
4.33 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
168
169
170
171
172
173
174
175
176
177
178
import axios from "axios";
import qs from "qs";
import {
ROOM_CHECK_AVAILABILITY_REQUEST,
ROOM_CHECK_AVAILABILITY_SUCCESS,
ROOM_CHECK_AVAILABILITY_RESET,
ROOM_CHECK_AVAILABILITY_FAIL,
ROOM_MAKE_REVIEW_REQUEST,
ROOM_MAKE_REVIEW_SUCCESS,
ROOM_MAKE_REVIEW_RESET,
ROOM_MAKE_REVIEW_FAIL,
ROOM_FETCH_BOOKED_DATES_SUCCESS,
ROOM_FETCH_BOOKED_DATES_FAIL,
ALL_ROOMS_FAIL,
ALL_ROOMS_SUCCESS,
ROOM_DETAILS_SUCCESS,
ROOM_DETAILS_FAIL,
CLEAR_ERRORS
} from "../constants/roomConstants";
import { getSession } from "next-auth/react";
// Get all rooms
export const getRooms =
(currentPage = 1, location = "", guests, category) =>
async dispatch => {
try {
const roomsPerPage = 4;
const query = {
filters: {
address: {
$contains: location
}
},
populate: ["images"],
pagination: {
pageSize: roomsPerPage,
page: currentPage
}
// sort: ['title:asc'],
// fields: ['title'],
// publicationState: 'live',
// locale: ['en'],
};
// Add all the optional filters.
if (guests) query.filters["guestCapacity"] = { $eq: guests };
if (category) query.filters["category"] = { $eq: category };
const queryString = qs.stringify(query, {
encodeValuesOnly: true // prettify URL
});
const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/rooms?${queryString}`);
dispatch({
type: ALL_ROOMS_SUCCESS,
payload: response.data
});
} catch (error) {
console.log("Real error is: ");
console.log(error);
dispatch({
type: ALL_ROOMS_FAIL,
payload: error.response.data
});
}
};
// Get room details
export const getRoomDetails = roomId => async dispatch => {
try {
const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/rooms/${roomId}?populate[0]=images`);
dispatch({
type: ROOM_DETAILS_SUCCESS,
payload: response.data
});
} catch (error) {
dispatch({
type: ROOM_DETAILS_FAIL,
payload: error.response.data
});
}
};
// Check for room availability
export const checkRoomAvailability = (roomId, checkInDate, checkOutDate) => async dispatch => {
try {
dispatch({
type: ROOM_CHECK_AVAILABILITY_REQUEST
});
const response = await axios.get(
`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/rooms/check-availability?roomId=${roomId}&checkInDate=${checkInDate}&checkOutDate=${checkOutDate}`
);
dispatch({
type: ROOM_CHECK_AVAILABILITY_SUCCESS,
payload: response.data
});
} catch (error) {
// console.log("Error while check room availability");
// console.log(error);
dispatch({
type: ROOM_CHECK_AVAILABILITY_FAIL,
payload: error.response.data
});
}
};
// Fetch room booked dates.
export const fetchRoomBookedDates = roomId => async dispatch => {
try {
const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/rooms/fetch-booked-dates?roomId=${roomId}`);
dispatch({
type: ROOM_FETCH_BOOKED_DATES_SUCCESS,
payload: response.data
});
} catch (error) {
dispatch({
type: ROOM_FETCH_BOOKED_DATES_FAIL,
payload: error.response.data
});
}
};
// update profile.
export const makeReview = reviewData => async dispatch => {
const session = await getSession();
if (!session) {
throw new Error("You need to be logged in before creating a review.");
}
try {
dispatch({
type: ROOM_MAKE_REVIEW_REQUEST
});
const config = {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${session.jwt}`
}
};
const response = await axios.post(
`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/rooms/reviews`,
{
rating: reviewData.rating,
comment: reviewData.comment,
roomId: reviewData.roomId
},
config
);
dispatch({
type: ROOM_MAKE_REVIEW_SUCCESS,
payload: response.data
});
} catch (error) {
console.log("Error while making a review: ");
console.log(error);
dispatch({
type: ROOM_MAKE_REVIEW_FAIL,
payload: error.response.data
});
}
};
// Clear errors
export const clearErrors = () => async dispatch => {
dispatch({
type: CLEAR_ERRORS
});
};