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