629 lines
20 KiB
JavaScript
629 lines
20 KiB
JavaScript
// import * as React from 'react';
|
|
// import EP from './endpoints.js';
|
|
// import { callLogs } from '../../utils/logs.js';
|
|
// import { TYPE_OPEN_ALERT } from '../../redux/types.js';
|
|
// import { getNavigationRef } from '../../utils/navigation.js';
|
|
// import DB from '../../components/storage';
|
|
// import store from '../../redux/store.js';
|
|
|
|
// const defaultHeaders = {
|
|
// 'Accept': 'application/json',
|
|
// 'Content-Type': 'application/json',
|
|
// }
|
|
|
|
// const Options = (method, headers, params, body) => {
|
|
// let obj = {}
|
|
// if(method == "post"){
|
|
// obj = {method: method, body: JSON.stringify(body), headers: headers || defaultHeaders, params: params}
|
|
// }else if(method == "get"){
|
|
// obj = {method: method, headers: headers || defaultHeaders, params: params}
|
|
// }
|
|
// return obj
|
|
// }
|
|
|
|
// export default async function API(endpoint, method, headers, params, body, onSuccess, onError, subject, action) {
|
|
// const navigationRef = getNavigationRef();
|
|
|
|
// try {
|
|
// if(method == "post"){
|
|
// let Head = {
|
|
// 'Accept': headers.Accept ? headers.Accept : 'application/json',
|
|
// 'Content-Type': headers['Content-Type'] ? headers['Content-Type'] : 'application/json',
|
|
// 'Authorization': headers.Authorization || '',
|
|
// 'card_number': headers.card_number || ''
|
|
// }
|
|
// let response = await fetch(endpoint.includes(":") ? endpoint : EP[endpoint], {
|
|
// method: method,
|
|
// body: body?._parts ? body : JSON.stringify(body),
|
|
// headers: Head,
|
|
// params: params
|
|
// });
|
|
|
|
// let json = await response.json();
|
|
|
|
// if(json.message === "Unauthenticated.") {
|
|
// let tokens = await DB.get("token");
|
|
|
|
// if(!tokens.length) return;
|
|
|
|
// store.dispatch({
|
|
// type: TYPE_OPEN_ALERT,
|
|
// payload: {
|
|
// open: true,
|
|
// title: "Session Expired",
|
|
// body: "Please log in again.",
|
|
// backdropCancellable: false,
|
|
// yesButtonOnly: true,
|
|
// yesText: "Okay",
|
|
// yesCB: async () => {
|
|
// await DB.reset();
|
|
// navigationRef.reset({
|
|
// index: 0,
|
|
// routes: [{name: 'Login'}],
|
|
// });
|
|
// }
|
|
// }
|
|
// })
|
|
// callLogs(json, "error", "frontend", subject, action)
|
|
// onError(json)
|
|
// return;
|
|
// }
|
|
|
|
// callLogs(json, "success", "frontend", subject, action)
|
|
// onSuccess(json)
|
|
// return json
|
|
// }else if(method == "get"){
|
|
// let url = !params.noID ? (EP[endpoint] + "?" + params) : EP[endpoint] + "/" + params.value
|
|
|
|
// if(headers.Authorization){
|
|
// let response = await fetch(endpoint.includes(":") ? endpoint : url, {
|
|
// method: method,
|
|
// headers: new Headers({
|
|
// 'Accept': 'application/json',
|
|
// 'Authorization': `${headers.Authorization || ''}`,
|
|
// 'card_number': `${headers.card_number}` || ''
|
|
// })
|
|
// });
|
|
|
|
// let json = await response.json();
|
|
|
|
// if(json.message === "Unauthenticated.") {
|
|
// store.dispatch({
|
|
// type: TYPE_OPEN_ALERT,
|
|
// payload: {
|
|
// open: true,
|
|
// title: "Session Expired",
|
|
// body: "Please log in again.",
|
|
// backdropCancellable: false,
|
|
// yesButtonOnly: true,
|
|
// yesText: "Okay",
|
|
// yesCB: async () => {
|
|
// await DB.reset();
|
|
// navigationRef.reset({
|
|
// index: 0,
|
|
// routes: [{name: 'Login'}],
|
|
// });
|
|
// }
|
|
// }
|
|
// })
|
|
// callLogs(json, "error", "frontend", subject, action)
|
|
// onError(json)
|
|
// return;
|
|
// }
|
|
|
|
// callLogs(json, "success", "frontend", subject, action)
|
|
// onSuccess(json)
|
|
// return json
|
|
// }else{
|
|
|
|
// let response = await fetch(endpoint.includes(":") ? endpoint : url);
|
|
// let json = await response.json();
|
|
// callLogs(json, "success", "frontend", subject, action)
|
|
// onSuccess(json)
|
|
// return json
|
|
// }
|
|
// }else if(method == "delete"){
|
|
// let url = !params.noID ? (EP[endpoint] + "?" + params) : EP[endpoint] + "/" + params.value
|
|
// if(headers.Authorization){
|
|
// let response = await fetch(endpoint.includes("/") ? EP[endpoint.split('/')[0]] + `/${endpoint.split('/')[1]}` : endpoint.includes(":") ? endpoint : url, {
|
|
// method: method,
|
|
// headers: new Headers({
|
|
// 'Accept': 'application/json',
|
|
// 'Authorization': `${headers.Authorization || ''}`,
|
|
// 'card_number': `${headers.card_number}` || ''
|
|
// })
|
|
// });
|
|
|
|
// let json = await response.json();
|
|
|
|
// if(json.message === "Unauthenticated.") {
|
|
// let tokens = await DB .get("token");
|
|
|
|
// if(!tokens.length) return;
|
|
|
|
// store.dispatch({
|
|
// type: TYPE_OPEN_ALERT,
|
|
// payload: {
|
|
// open: true,
|
|
// title: "Session Expired",
|
|
// body: "Please log in again.",
|
|
// backdropCancellable: false,
|
|
// yesButtonOnly: true,
|
|
// yesText: "Okay",
|
|
// yesCB: async () => {
|
|
// await DB.reset();
|
|
// navigationRef.reset({
|
|
// index: 0,
|
|
// routes: [{name: 'Login'}],
|
|
// });
|
|
// }
|
|
// }
|
|
// })
|
|
// callLogs(json, "error", "frontend", subject, action)
|
|
// onError(json)
|
|
// return;
|
|
// }
|
|
|
|
// onSuccess(json)
|
|
// callLogs(json, "success", "frontend", subject, action)
|
|
// return json
|
|
// }else{
|
|
// let response = await fetch(endpoint.includes(":") ? endpoint : url);
|
|
// let json = await response.json();
|
|
|
|
// if(json.message === "Unauthenticated.") {
|
|
// let tokens = await DB.get("token");
|
|
|
|
// if(!tokens.length) return;
|
|
|
|
// store.dispatch({
|
|
// type: TYPE_OPEN_ALERT,
|
|
// payload: {
|
|
// open: true,
|
|
// title: "Session Expired",
|
|
// body: "Please log in again.",
|
|
// backdropCancellable: false,
|
|
// yesButtonOnly: true,
|
|
// yesText: "Okay",
|
|
// yesCB: async () => {
|
|
// await DB.reset();
|
|
// navigationRef.reset({
|
|
// index: 0,
|
|
// routes: [{name: 'Login'}],
|
|
// });
|
|
// }
|
|
// }
|
|
// })
|
|
// callLogs(json, "error", "frontend", subject, action)
|
|
// onError(json)
|
|
// return;
|
|
// }
|
|
|
|
// onSuccess(json)
|
|
// callLogs(json, "success", "frontend", subject, action)
|
|
// return json
|
|
// }
|
|
// }else if(method == "put") {
|
|
// let Head = {
|
|
// 'Content-Type': 'application/json'
|
|
// }
|
|
// let response = await fetch(endpoint.includes(":") ? endpoint : EP[endpoint], {
|
|
// method: method,
|
|
// body: body?._parts ? body : JSON.stringify(body),
|
|
// headers: Head,
|
|
// params: params
|
|
// });
|
|
// let json = await response.json();
|
|
|
|
// if(json.message === "Unauthenticated.") {
|
|
// let tokens = await DB.get("token");
|
|
|
|
// if(!tokens.length) return;
|
|
|
|
// store.dispatch({
|
|
// type: TYPE_OPEN_ALERT,
|
|
// payload: {
|
|
// open: true,
|
|
// title: "Session Expired",
|
|
// body: "Please log in again.",
|
|
// backdropCancellable: false,
|
|
// yesButtonOnly: true,
|
|
// yesText: "Okay",
|
|
// yesCB: async () => {
|
|
// await DB.reset();
|
|
// navigationRef.reset({
|
|
// index: 0,
|
|
// routes: [{name: 'Login'}],
|
|
// });
|
|
// }
|
|
// }
|
|
// })
|
|
// callLogs(json, "error", "frontend", subject, action)
|
|
// onError(json)
|
|
// return;
|
|
// }
|
|
|
|
// onSuccess(json)
|
|
// callLogs(json, "success", "frontend", subject, action)
|
|
// return json
|
|
// }else if(method == "patch") {
|
|
// let Head = {
|
|
// 'Accept': headers.Accept ? headers.Accept : 'application/json',
|
|
// 'Content-Type': headers['Content-Type'] ? headers['Content-Type'] : 'application/json',
|
|
// 'Authorization': headers.Authorization || '',
|
|
// }
|
|
|
|
// let response = await fetch(endpoint.includes("/") ? EP[endpoint.split('/')[0]] + `/${endpoint.split('/')[1]}` : endpoint.includes(":") ? endpoint : EP[endpoint], {
|
|
// method: method,
|
|
// body: body?._parts ? body : JSON.stringify(body),
|
|
// headers: Head,
|
|
// params: params
|
|
// })
|
|
|
|
// let json = await response.json();
|
|
|
|
// if(json.message === "Unauthenticated.") {
|
|
// let tokens = await DB.get("token");
|
|
|
|
// if(!tokens.length) return;
|
|
|
|
// store.dispatch({
|
|
// type: TYPE_OPEN_ALERT,
|
|
// payload: {
|
|
// open: true,
|
|
// title: "Session Expired",
|
|
// body: "Please log in again.",
|
|
// backdropCancellable: false,
|
|
// yesButtonOnly: true,
|
|
// yesText: "Okay",
|
|
// yesCB: async () => {
|
|
// await DB.reset();
|
|
// navigationRef.reset({
|
|
// index: 0,
|
|
// routes: [{name: 'Login'}],
|
|
// });
|
|
// }
|
|
// }
|
|
// })
|
|
// callLogs(json, "error", "frontend", subject, action)
|
|
// onError(json)
|
|
// return;
|
|
// }
|
|
|
|
// onSuccess(json)
|
|
// callLogs(json, "success", "frontend", subject, action)
|
|
// return json
|
|
// }
|
|
|
|
// } catch (error) {
|
|
// callLogs(error, "error", "frontend", subject, action);
|
|
// onError(error)
|
|
// return error
|
|
// }
|
|
// }
|
|
|
|
import * as React from 'react';
|
|
import { callLogs } from '../../utils/logs.js';
|
|
import { TYPE_OPEN_ALERT } from '../../redux/types.js';
|
|
import { getNavigationRef } from '../../utils/navigation.js';
|
|
import store from '../../redux/store.js';
|
|
import DB from '../../components/storage';
|
|
import EP from './endpoints.js';
|
|
import axios from 'axios';
|
|
|
|
const defaultHeaders = {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
}
|
|
|
|
export default async function API(endpoint, method, headers, params, body, onSuccess, onError, subject, action) {
|
|
const navigationRef = getNavigationRef();
|
|
|
|
const headerToUse = () => {
|
|
switch(method) {
|
|
case "post":
|
|
return {
|
|
'Accept': headers.Accept ? headers.Accept : 'application/json',
|
|
'Content-Type': headers['Content-Type'] ? headers['Content-Type'] : 'application/json',
|
|
'Authorization': headers.Authorization || '',
|
|
'card_number': headers.card_number || ''
|
|
};
|
|
case "get":
|
|
if(headers.Authorization) {
|
|
return {
|
|
'Accept': 'application/json',
|
|
'Authorization': `${headers.Authorization || ''}`,
|
|
'card_number': `${headers.card_number}` || ''
|
|
}
|
|
} else {
|
|
return {
|
|
'Accept': 'application/json',
|
|
}
|
|
}
|
|
|
|
return {};
|
|
case "delete":
|
|
if(headers.Authorization) {
|
|
return {
|
|
'Accept': 'application/json',
|
|
'Authorization': `${headers.Authorization || ''}`,
|
|
'card_number': `${headers.card_number}` || ''
|
|
}
|
|
}
|
|
|
|
return {};
|
|
case "put":
|
|
return {
|
|
'Content-Type': defaultHeaders['Content-Type']
|
|
};
|
|
case "patch":
|
|
return {
|
|
'Accept': headers.Accept ? headers.Accept : 'application/json',
|
|
'Content-Type': headers['Content-Type'] ? headers['Content-Type'] : 'application/json',
|
|
'Authorization': headers.Authorization || '',
|
|
};
|
|
default:
|
|
return defaultHeaders;
|
|
}
|
|
}
|
|
|
|
switch(method) {
|
|
case "post": {
|
|
axios({
|
|
method: method,
|
|
url: endpoint.includes(":") ? endpoint : EP[endpoint],
|
|
data: body?._parts ? body : JSON.stringify(body),
|
|
params: params,
|
|
headers: headerToUse()
|
|
}).then(response => {
|
|
callLogs(response.data, "success", "frontend", subject, action)
|
|
onSuccess(response.data)
|
|
}).catch(async (err) => {
|
|
const errorToResponse = {
|
|
status: err?.response?.data?.status || err?.response?.status,
|
|
message: err?.response?.data?.message || err?.response?.message || "Something went wrong, please try again."
|
|
}
|
|
|
|
if(errorToResponse.status === 401) {
|
|
let tokens = await DB.get("token");
|
|
|
|
if(!tokens.length) return;
|
|
|
|
store.dispatch({
|
|
type: TYPE_OPEN_ALERT,
|
|
payload: {
|
|
open: true,
|
|
title: "Session Expired",
|
|
body: "Please log in again.",
|
|
backdropCancellable: false,
|
|
yesButtonOnly: true,
|
|
yesText: "Okay",
|
|
yesCB: async () => {
|
|
await DB.reset();
|
|
navigationRef.reset({
|
|
index: 0,
|
|
routes: [{name: 'Login'}],
|
|
});
|
|
}
|
|
}
|
|
})
|
|
callLogs(err?.response, "error", "frontend", subject, action)
|
|
onError(err?.response)
|
|
return;
|
|
}
|
|
|
|
callLogs(err?.response, "error", "frontend", subject, action);
|
|
onError(errorToResponse)
|
|
})
|
|
return;
|
|
}
|
|
|
|
case "get": {
|
|
let url = !params.noID ? (EP[endpoint] + "?" + params) : EP[endpoint] + "/" + params.value;
|
|
|
|
axios({
|
|
method: method,
|
|
url: endpoint.includes(":") ? endpoint : url,
|
|
headers: headerToUse()
|
|
}).then(response => {
|
|
callLogs(response.data, "success", "frontend", subject, action)
|
|
onSuccess(response.data)
|
|
}).catch(async (err) => {
|
|
const errorToResponse = {
|
|
status: err?.response?.data?.status || err?.response?.status,
|
|
message: err?.response?.data?.message || err?.response?.message || "Something went wrong, please try again."
|
|
}
|
|
|
|
if(errorToResponse.status === 401) {
|
|
let tokens = await DB.get("token");
|
|
|
|
if(!tokens.length) return;
|
|
|
|
store.dispatch({
|
|
type: TYPE_OPEN_ALERT,
|
|
payload: {
|
|
open: true,
|
|
title: "Session Expired",
|
|
body: "Please log in again.",
|
|
backdropCancellable: false,
|
|
yesButtonOnly: true,
|
|
yesText: "Okay",
|
|
yesCB: async () => {
|
|
await DB.reset();
|
|
navigationRef.reset({
|
|
index: 0,
|
|
routes: [{name: 'Login'}],
|
|
});
|
|
}
|
|
}
|
|
})
|
|
callLogs(err?.response, "error", "frontend", subject, action)
|
|
onError(err?.response)
|
|
return;
|
|
}
|
|
|
|
callLogs(err?.response, "error", "frontend", subject, action);
|
|
onError(errorToResponse)
|
|
})
|
|
return;
|
|
}
|
|
|
|
case "delete": {
|
|
let url = !params.noID ? (EP[endpoint] + "?" + params) : EP[endpoint] + "/" + params.value;
|
|
let urlToUse = endpoint.includes("/") ? EP[endpoint.split('/')[0]] + `/${endpoint.split('/')[1]}` : endpoint.includes(":") ? endpoint : url;
|
|
|
|
axios({
|
|
method: method,
|
|
url: urlToUse,
|
|
headers: headerToUse()
|
|
}).then(response => {
|
|
console.log(response)
|
|
callLogs(response.data, "success", "frontend", subject, action)
|
|
onSuccess(response.data)
|
|
}).catch(async (err) => {
|
|
console.log(err)
|
|
const errorToResponse = {
|
|
status: err?.response?.data?.status || err?.response?.status,
|
|
message: err?.response?.data?.message || err?.response?.message || "Something went wrong, please try again."
|
|
}
|
|
|
|
if(errorToResponse.status === 401) {
|
|
let tokens = await DB.get("token");
|
|
|
|
if(!tokens.length) return;
|
|
|
|
store.dispatch({
|
|
type: TYPE_OPEN_ALERT,
|
|
payload: {
|
|
open: true,
|
|
title: "Session Expired",
|
|
body: "Please log in again.",
|
|
backdropCancellable: false,
|
|
yesButtonOnly: true,
|
|
yesText: "Okay",
|
|
yesCB: async () => {
|
|
await DB.reset();
|
|
navigationRef.reset({
|
|
index: 0,
|
|
routes: [{name: 'Login'}],
|
|
});
|
|
}
|
|
}
|
|
})
|
|
callLogs(err?.response, "error", "frontend", subject, action)
|
|
onError(err?.response)
|
|
return;
|
|
}
|
|
|
|
callLogs(err?.response, "error", "frontend", subject, action);
|
|
onError(errorToResponse)
|
|
})
|
|
return;
|
|
}
|
|
|
|
case "put": {
|
|
axios({
|
|
method: method,
|
|
url: endpoint.includes(":") ? endpoint : EP[endpoint],
|
|
data: body?._parts ? body : JSON.stringify(body),
|
|
params: params,
|
|
headers: headerToUse()
|
|
}).then(response => {
|
|
callLogs(response.data, "success", "frontend", subject, action)
|
|
onSuccess(response.data)
|
|
}).catch(async (err) => {
|
|
const errorToResponse = {
|
|
status: err?.response?.data?.status || err?.response?.status,
|
|
message: err?.response?.data?.message || err?.response?.message || "Something went wrong, please try again."
|
|
}
|
|
|
|
if(errorToResponse.status === 401) {
|
|
let tokens = await DB.get("token");
|
|
|
|
if(!tokens.length) return;
|
|
|
|
store.dispatch({
|
|
type: TYPE_OPEN_ALERT,
|
|
payload: {
|
|
open: true,
|
|
title: "Session Expired",
|
|
body: "Please log in again.",
|
|
backdropCancellable: false,
|
|
yesButtonOnly: true,
|
|
yesText: "Okay",
|
|
yesCB: async () => {
|
|
await DB.reset();
|
|
navigationRef.reset({
|
|
index: 0,
|
|
routes: [{name: 'Login'}],
|
|
});
|
|
}
|
|
}
|
|
})
|
|
callLogs(err?.response, "error", "frontend", subject, action)
|
|
onError(err?.response)
|
|
return;
|
|
}
|
|
|
|
callLogs(err?.response, "error", "frontend", subject, action);
|
|
onError(errorToResponse)
|
|
})
|
|
return;
|
|
}
|
|
|
|
case "patch":
|
|
let url = !params.noID ? (EP[endpoint] + "?" + params) : EP[endpoint] + "/" + params.value;
|
|
const urlToUse = endpoint.includes("/") ? EP[endpoint.split('/')[0]] + `/${endpoint.split('/')[1]}` : endpoint.includes(":") ? endpoint : url;
|
|
|
|
axios({
|
|
method: method,
|
|
url: urlToUse,
|
|
data: body?._parts ? body : JSON.stringify(body),
|
|
params: params,
|
|
headers: headerToUse()
|
|
}).then(response => {
|
|
callLogs(response.data, "success", "frontend", subject, action)
|
|
onSuccess(response.data)
|
|
}).catch(async (err) => {
|
|
const errorToResponse = {
|
|
status: err?.response?.data?.status || err?.response?.status,
|
|
message: err?.response?.data?.message || err?.response?.message || "Something went wrong, please try again."
|
|
}
|
|
|
|
if(errorToResponse.status === 401) {
|
|
let tokens = await DB.get("token");
|
|
|
|
if(!tokens.length) return;
|
|
|
|
store.dispatch({
|
|
type: TYPE_OPEN_ALERT,
|
|
payload: {
|
|
open: true,
|
|
title: "Session Expired",
|
|
body: "Please log in again.",
|
|
backdropCancellable: false,
|
|
yesButtonOnly: true,
|
|
yesText: "Okay",
|
|
yesCB: async () => {
|
|
await DB.reset();
|
|
navigationRef.reset({
|
|
index: 0,
|
|
routes: [{name: 'Login'}],
|
|
});
|
|
}
|
|
}
|
|
})
|
|
callLogs(err?.response, "error", "frontend", subject, action)
|
|
onError(err?.response)
|
|
return;
|
|
}
|
|
|
|
callLogs(err?.response, "error", "frontend", subject, action);
|
|
onError(errorToResponse)
|
|
})
|
|
return;
|
|
|
|
default:
|
|
return;
|
|
}
|
|
} |