-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.tsx
119 lines (98 loc) · 4.04 KB
/
api.tsx
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
import Axios, { AxiosResponse } from 'axios';
import { BASE_PATH } from '../constants';
import { store } from '../store';
// import { AlertsInterface, HideProgressBar, ShowErrorAlert } from '../store/alerts';
// import { adminInfoInterface } from '../services/dataType/reduxActionsInterfaces/adminStoreInterfaces';
export const postRequest = (endpoint: string, body: any) => {
// const { dispatch } = store;
// const stateValues = store.getState()
// const alertsInfo: AlertsInterface = stateValues.alerts;
// const currentLanguage = alertsInfo ? alertsInfo.currentLanguage : "en"
// body['x_language'] = currentLanguage
return Axios.post(getApiPath(endpoint), JSON.stringify(body), getConfigSetting())
.then(response => {
// dispatch(HideProgressBar());
if (isApiCodeSucess(response)) {
return response.data;
}
}).catch(function (error: any) {
// dispatch(HideProgressBar());
handleErrorResponse(error);
});
};
export const patchRequest = (endpoint: string, body: any) => {
// const { dispatch } = store;
// const stateValues = store.getState()
// const alertsInfo: AlertsInterface = stateValues.alerts;
// const currentLanguage = alertsInfo ? alertsInfo.currentLanguage : "en"
// body['x_language'] = currentLanguage
return Axios.patch(getApiPath(endpoint), JSON.stringify(body), getConfigSetting()).then(response => {
// dispatch(HideProgressBar());
if (isApiCodeSucess(response)) {
return response.data;
}
}).catch(function (error: any) {
handleErrorResponse(error);
});
};
export const getRequest = (endpoint: string) => {
// const { dispatch } = store;
return Axios.get(getApiPath(endpoint), getConfigSetting()).then(response => {
// dispatch(HideProgressBar());
if (isApiCodeSucess(response)) {
return response.data;
}
})
.catch(function (error: any) {
// dispatch(HideProgressBar());
handleErrorResponse(error);
});
};
const getApiPath = (endpoint: string) => BASE_PATH + '/api/admin/' + endpoint;
const getConfigSetting = () => {
// const stateValues = store.getState()
// const adminInfo: adminInfoInterface = stateValues.adminInfo;
// const token = adminInfo ? adminInfo.accessToken : ""
// const alertsInfo: AlertsInterface = stateValues.alerts;
// const currentLanguage = alertsInfo ? alertsInfo.currentLanguage : "en"
let headers: any = {
'content-type': 'application/json',
};
// if (token) {
// headers['Authorization'] = `Bearer ${token}`;
// // headers['Accept-Language'] = currentLanguage
// }
return {
headers: headers,
};
};
const handleErrorResponse = (error: any) => {
const { dispatch } = store;
if (error.code === "ERR_NETWORK") {
// dispatch(ShowErrorAlert({ visible: true, message: translateLanguage("NO_INTERNET") }));
}
else if (error.response.status === 401) {
// dispatch(ShowErrorAlert({ visible: true, message: translateLanguage("SESSION_EXPIRED") }));
// dispatch(ResetAdminStore())
window.location.reload();
}
else if (error.response.status === 403) {
// dispatch(ShowErrorAlert({ visible: true, message: translateLanguage("WENT_WRONG_ERROR") }));
}
else if (error.response.data.code === 400 && error.response.data.message !== '') {
// dispatch(ShowErrorAlert({ visible: true, message: error.response.data.message }));
} else {
// dispatch(ShowErrorAlert({ visible: true, message: error.message }));
}
};
export const isApiCodeSucess = (resp: AxiosResponse) => {
const { dispatch } = store;
if (resp.data.code === 400) {
// dispatch(ShowErrorAlert({ visible: true, message: resp.data.message }));
return false
} else if (resp?.data?.code === 500) {
// dispatch(ShowErrorAlert({ visible: true, message: "Backend : " + resp.data.message }));
return false
}
return true;
};