· 4 years ago · Aug 03, 2021, 01:02 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 {
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} from "./types";
25import Api from "../api/axiosConfig";
26
27export const getNewGames =
28 () =>
29 async (dispatch: Dispatch): Promise<void> => {
30 const res = await Api.get<Game[]>(`${RequestType.NewGames}`);
31 dispatch({ type: NEW_GAMES, payload: res.data });
32 };
33
34export const getSearchGames =
35 (params: string) =>
36 async (dispatch: Dispatch): Promise<void> => {
37 const res = await Api.get<Game[]>(`${RequestType.Search}/?${params}`);
38 dispatch({ type: SEARCH_GAMES, payload: res.data });
39 };
40
41export const startLoading =
42 () =>
43 (dispatch: Dispatch): void => {
44 dispatch({ type: START_LOADING, payload: true });
45 };
46
47export const signIn =
48 (login: string, password: string) =>
49 async (dispatch: Dispatch): Promise<void> => {
50 try {
51 const res = await Api.post<User>(`${RequestType.SignIn}`, {
52 login,
53 password,
54 });
55 dispatch({ type: SIGN_IN_SUCCESS, payload: res.data });
56 } catch (error) {
57 dispatch({ type: SIGN_IN_ERROR, payload: JSON.parse(JSON.stringify(error.response.data)) });
58 }
59 };
60
61export const signUp =
62 (login: string, password: string) =>
63 async (dispatch: Dispatch): Promise<void> => {
64 try {
65 const res = await Api.post<User>(`${RequestType.SignUp}`, {
66 login,
67 password,
68 });
69
70 dispatch({ type: SIGN_UP_SUCCESS, payload: res.data });
71 } catch (error) {
72 dispatch({ type: SIGN_UP_ERROR, payload: JSON.parse(JSON.stringify(error.response.data)) });
73 }
74 };
75
76export const logout =
77 () =>
78 async (dispatch: Dispatch): Promise<void> => {
79 const res = await Api.post(`${RequestType.Logout}`);
80 dispatch({ type: LOGOUT, payload: res.data });
81 };
82
83export const changeUserPassword =
84 (_id: string, password: string) =>
85 async (dispatch: Dispatch): Promise<void> => {
86 try {
87 const res = await Api.post<string>(
88 `${RequestType.ChangePassword}`,
89 {
90 _id,
91 password,
92 },
93 { withCredentials: true }
94 );
95
96 dispatch({ type: CHANGE_PASSWORD_SUCCESS, payload: res.data });
97 } catch (error) {
98 dispatch({ type: CHANGE_PASSWORD_ERROR, payload: JSON.parse(JSON.stringify(error.response.data)) });
99 }
100 };
101
102export const changeUserInfo =
103 (user: User) =>
104 async (dispatch: Dispatch): Promise<void> => {
105 try {
106 const res = await Api.post<User>(`${RequestType.SaveProfile}`, {
107 user,
108 });
109
110 dispatch({ type: CHANGE_USER_INFO_SUCCESS, payload: res.data });
111 } catch (error) {
112 dispatch({ type: CHANGE_USER_INFO_ERROR, payload: JSON.parse(JSON.stringify(error.response.data)) });
113 }
114 };
115
116export const changeUserImage =
117 (_id: string, file: string) =>
118 async (dispatch: Dispatch): Promise<void> => {
119 try {
120 const res = await Api.post(
121 `${RequestType.SaveProfileImage}`,
122 {
123 _id,
124 file,
125 },
126 { withCredentials: true }
127 );
128
129 dispatch({ type: CHANGE_USER_IMAGE_SUCCESS, payload: res.data });
130 } catch (error) {
131 dispatch({ type: CHANGE_USER_IMAGE_ERROR, payload: JSON.parse(JSON.stringify(error.response.data)) });
132 }
133 };
134
135export const changeFilter =
136 (key: string, value: string) =>
137 (dispatch: Dispatch): void => {
138 dispatch({ type: CHANGE_FILTERS, payload: { [`${key}`]: value } });
139 };
140
141export const resetFilters =
142 () =>
143 (dispatch: Dispatch): void => {
144 dispatch({ type: RESET_FILTERS, payload: initialState });
145 };
146