· 4 years ago · Aug 09, 2021, 12:14 PM
1/* eslint-disable import/prefer-default-export */
2import { RequestType } from "@/constants";
3import Game from "@/Game";
4import { Dispatch } from "redux";
5import User from "@/User";
6import { initialState } from "@/reducers/filterReducer";
7import { BuyItems } from "@/reducers/cartReducer";
8import {
9 NEW_GAMES,
10 SEARCH_GAMES,
11 SIGN_IN_SUCCESS,
12 SIGN_IN_ERROR,
13 SIGN_UP_SUCCESS,
14 SIGN_UP_ERROR,
15 LOGOUT,
16 DELETE_CART,
17 CHANGE_PASSWORD_SUCCESS,
18 CHANGE_PASSWORD_ERROR,
19 CHANGE_USER_INFO_ERROR,
20 CHANGE_USER_INFO_SUCCESS,
21 CHANGE_USER_IMAGE_SUCCESS,
22 CHANGE_USER_IMAGE_ERROR,
23 CHANGE_FILTERS,
24 RESET_FILTERS,
25 START_LOADING,
26 ADD_TO_CART,
27 SHOW_SIGN_IN,
28 SHOW_SIGN_UP,
29 CLOSE_MODAL,
30 GET_CART_ITEMS,
31 SELECT_CART_ITEM,
32 DELETE_CART_ITEM,
33 CHANGE_TOTAL_PRICE,
34 CHANGE_COUNT,
35 DELETE_FROM_CART,
36 SHOW_EDIT_MODAL,
37 ADD_GAME,
38 DELETE_GAME,
39 UPDATE_GAME,
40 RESET_ERROR,
41 GET_USER,
42} from "./types";
43import Api from "../api/axiosConfig";
44
45export const getNewGames =
46 () =>
47 async (dispatch: Dispatch): Promise<void> => {
48 const res = await Api.get<Game[]>(RequestType.NewGames);
49 dispatch({ type: NEW_GAMES, payload: res.data });
50 };
51
52export const getSearchGames =
53 (params: string) =>
54 async (dispatch: Dispatch): Promise<void> => {
55 const res = await Api.get<Game[]>(`${RequestType.Search}/?${params}`);
56 dispatch({ type: SEARCH_GAMES, payload: res.data });
57 };
58
59export const startLoading =
60 () =>
61 (dispatch: Dispatch): void => {
62 dispatch({ type: START_LOADING, payload: true });
63 };
64
65export const signIn =
66 (login: string, password: string) =>
67 async (dispatch: Dispatch): Promise<void> => {
68 try {
69 const res = await Api.post<User>(RequestType.SignIn, {
70 login,
71 password,
72 });
73 dispatch({ type: SIGN_IN_SUCCESS, payload: res.data });
74 } catch (error) {
75 dispatch({ type: SIGN_IN_ERROR, payload: JSON.parse(JSON.stringify(error.response.data)) });
76 }
77 };
78
79export const signUp =
80 (login: string, password: string) =>
81 async (dispatch: Dispatch): Promise<void> => {
82 try {
83 const res = await Api.post<User>(RequestType.SignUp, {
84 login,
85 password,
86 });
87
88 dispatch({ type: SIGN_UP_SUCCESS, payload: res.data });
89 } catch (error) {
90 dispatch({ type: SIGN_UP_ERROR, payload: JSON.parse(JSON.stringify(error.response.data)) });
91 }
92 };
93
94export const getUser =
95 () =>
96 async (dispatch: Dispatch): Promise<void> => {
97 try {
98 const res = await Api.get(RequestType.User);
99 dispatch({ type: GET_USER, payload: res.data });
100 } catch {
101 dispatch({ type: GET_USER, payload: {} });
102 }
103 };
104
105export const logout =
106 () =>
107 async (dispatch: Dispatch): Promise<void> => {
108 const res = await Api.post(RequestType.Logout);
109 dispatch({ type: LOGOUT, payload: res.data });
110 };
111
112export const changeUserPassword =
113 (_id: string, password: string) =>
114 async (dispatch: Dispatch): Promise<void> => {
115 try {
116 const res = await Api.post<string>(
117 RequestType.ChangePassword,
118 {
119 _id,
120 password,
121 },
122 { withCredentials: true }
123 );
124
125 dispatch({ type: CHANGE_PASSWORD_SUCCESS, payload: res.data });
126 } catch (error) {
127 if (JSON.parse(JSON.stringify(error.response.data)).error) {
128 dispatch({ type: CHANGE_PASSWORD_ERROR, payload: JSON.parse(JSON.stringify(error.response.data)) });
129 } else {
130 dispatch({ type: LOGOUT, payload: {} });
131 }
132 }
133 };
134
135export const changeUserInfo =
136 (user: User) =>
137 async (dispatch: Dispatch): Promise<void> => {
138 try {
139 const res = await Api.post<User>(RequestType.SaveProfile, {
140 user,
141 });
142
143 dispatch({ type: CHANGE_USER_INFO_SUCCESS, payload: res.data });
144 } catch (error) {
145 if (JSON.parse(JSON.stringify(error.response.data)).error) {
146 dispatch({ type: CHANGE_USER_INFO_ERROR, payload: JSON.parse(JSON.stringify(error.response.data)) });
147 } else {
148 dispatch({ type: LOGOUT, payload: {} });
149 }
150 }
151 };
152
153export const changeUserImage =
154 (_id: string, file: string) =>
155 async (dispatch: Dispatch): Promise<void> => {
156 try {
157 const res = await Api.post(`${RequestType.SaveProfileImage}`, {
158 _id,
159 file,
160 });
161
162 dispatch({ type: CHANGE_USER_IMAGE_SUCCESS, payload: res.data });
163 } catch (error) {
164 if (JSON.parse(JSON.stringify(error.response.data)).error) {
165 dispatch({ type: CHANGE_USER_IMAGE_ERROR, payload: JSON.parse(JSON.stringify(error.response.data)) });
166 } else {
167 dispatch({ type: LOGOUT, payload: {} });
168 }
169 }
170 };
171
172export const changeFilter =
173 (key: string, value: string) =>
174 (dispatch: Dispatch): void => {
175 dispatch({ type: CHANGE_FILTERS, payload: { [`${key}`]: value } });
176 };
177
178export const resetFilters =
179 () =>
180 (dispatch: Dispatch): void => {
181 dispatch({ type: RESET_FILTERS, payload: initialState });
182 };
183
184export const addToCard =
185 (gameId: string) =>
186 (dispatch: Dispatch): void => {
187 dispatch({ type: ADD_TO_CART, payload: gameId });
188 };
189
190export const showSignIn =
191 () =>
192 (dispatch: Dispatch): void => {
193 dispatch({ type: SHOW_SIGN_IN });
194 };
195
196export const showSignUp =
197 () =>
198 (dispatch: Dispatch): void => {
199 dispatch({ type: SHOW_SIGN_UP });
200 };
201
202export const closeModal =
203 () =>
204 (dispatch: Dispatch): void => {
205 dispatch({ type: CLOSE_MODAL });
206 dispatch({ type: RESET_ERROR });
207 };
208
209export const setEdit =
210 (game: Game | null) =>
211 (dispatch: Dispatch): void => {
212 dispatch({ type: SHOW_EDIT_MODAL, payload: game });
213 };
214
215export const getCartItems =
216 (gamesId: string[]) =>
217 async (dispatch: Dispatch): Promise<void> => {
218 try {
219 const res = await Api.post(`${RequestType.CartItems}`, {
220 gamesId,
221 });
222 dispatch({ type: GET_CART_ITEMS, payload: res.data });
223 } catch (error) {
224 if (!JSON.parse(JSON.stringify(error.response.data)).error) {
225 dispatch({ type: LOGOUT, payload: {} });
226 }
227 }
228 };
229
230export const selectCartItem =
231 (game: Game, count: number, isAdd: boolean) =>
232 (dispatch: Dispatch): void => {
233 if (isAdd) {
234 dispatch({ type: SELECT_CART_ITEM, payload: { _id: game._id, price: game.price, count } });
235 } else {
236 dispatch({ type: DELETE_CART_ITEM, payload: { _id: game._id } });
237 }
238 };
239
240export const changeTotalPrice =
241 (buyItems: BuyItems) =>
242 (dispatch: Dispatch): void => {
243 dispatch({
244 type: CHANGE_TOTAL_PRICE,
245 payload: Array.from(Object.values(buyItems)).reduce(
246 (resParams, { count, price }) => resParams + count * price,
247 0
248 ),
249 });
250 };
251
252export const changeGamesCount =
253 (_id: string, count: number) =>
254 (dispatch: Dispatch): void => {
255 dispatch({ type: CHANGE_COUNT, payload: { _id, count } });
256 };
257
258export const deleteSelectedItems =
259 (buyItems: BuyItems, cart: Record<string, number>) =>
260 (dispatch: Dispatch): void => {
261 const itemToDelete = Array.from(Object.keys(buyItems));
262 const newCart = { ...cart };
263 for (let i = 0; i < itemToDelete.length; i++) {
264 delete newCart[itemToDelete[i]];
265 }
266 dispatch({ type: DELETE_FROM_CART, payload: newCart });
267 };
268
269export const addNewGame =
270 (game: {
271 name: string;
272 category: string;
273 price: number;
274 gameImage: string;
275 description: string;
276 age: number;
277 isPc: boolean;
278 isPlaystation: boolean;
279 isXbox: boolean;
280 }) =>
281 async (dispatch: Dispatch): Promise<void> => {
282 try {
283 const res = await Api.post(RequestType.Product, {
284 game,
285 });
286 dispatch({ type: ADD_GAME, payload: res.data });
287 dispatch({ type: CLOSE_MODAL });
288 } catch (error) {
289 if (!JSON.parse(JSON.stringify(error.response.data)).error) {
290 dispatch({ type: LOGOUT, payload: {} });
291 }
292 }
293 };
294
295export const deteleGame =
296 (id: string) =>
297 async (dispatch: Dispatch): Promise<void> => {
298 try {
299 const res = await Api.delete(`${RequestType.Product}/${id}`);
300 dispatch({ type: DELETE_GAME, paylad: res });
301 dispatch({ type: DELETE_CART, payload: { _id: id } });
302 dispatch({ type: CLOSE_MODAL });
303 } catch (error) {
304 if (!JSON.parse(JSON.stringify(error.response.data)).error) {
305 dispatch({ type: LOGOUT, payload: {} });
306 }
307 }
308 };
309
310export const updateGame =
311 (game: Game) =>
312 async (dispatch: Dispatch): Promise<void> => {
313 try {
314 const res = await Api.put(RequestType.Product, { game });
315 dispatch({ type: UPDATE_GAME, payload: res });
316 dispatch({ type: CLOSE_MODAL });
317 } catch (error) {
318 if (!JSON.parse(JSON.stringify(error.response.data)).error) {
319 dispatch({ type: LOGOUT, payload: {} });
320 }
321 }
322 };
323