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