reports create, refactoring

This commit is contained in:
kurpfish
2021-11-30 16:00:58 +02:00
parent 76c46067ef
commit 4629bc74a9
61 changed files with 26047 additions and 3865 deletions

View File

@ -1,17 +1,22 @@
export const withAuthRedirect = actionCall => ({link, index, history, role, logout}) => {
return actionCall(link, index)
.then(res => {
if(res.status && res.status == 401) {
localStorage.clear();
logout();
history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth') ;
}
export const withAuthRedirect =
(actionCall) =>
({ link, params, history, role, logout, body }) => {
const linkWithParams = params
? `${link}?${new URLSearchParams(params)}`
: link
return actionCall(linkWithParams, body)
.then((res) => {
if (res.status && res.status == 401) {
localStorage.clear()
logout()
history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth')
}
return res;
})
.catch(err => {
localStorage.clear();
logout();
history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth');
})
}
return res
})
.catch((err) => {
localStorage.clear()
logout()
history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth')
})
}

View File

@ -1,59 +1,4 @@
import { withAuthRedirect } from "./authRedirect"
export const fetchProfile = withAuthRedirect(async (link, index) => {
try {
const response = await fetch(`${link}${index}`, {
method: 'GET',
headers: {
// 'Access-Control-Request-Headers': 'authorization',
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
// 'Origin': `${process.env.REACT_APP_BASE_URL}`,
}
})
let data = await response.json()
return data
} catch (error) {
console.log('Query error', error)
}
})
export const fetchSkills = withAuthRedirect(async (link) => {
try {
const response = await fetch(link, {
method: 'GET',
headers: {
// 'Access-Control-Request-Headers': 'authorization',
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
// 'Origin': `${process.env.REACT_APP_BASE_URL}`,
}
})
let data = await response.json()
return data
} catch (error) {
console.log('Query error', error)
}
})
export const fetchItemsForId = withAuthRedirect(async (link, id) => {
console.log(`Bearer ${localStorage.getItem('auth_token')}`);
try {
const response = await fetch(`${link}${id}`, {
method: 'GET',
headers: {
// 'Access-Control-Request-Headers': 'authorization',
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
// 'Origin': `${process.env.REACT_APP_BASE_URL}`,
}
})
let data = await response.json()
return data
} catch (error) {
console.log('Query error', error)
}
})
import { withAuthRedirect } from './authRedirect'
export const fetchForm = withAuthRedirect(async (link, info) => {
try {
@ -61,9 +6,9 @@ export const fetchForm = withAuthRedirect(async (link, info) => {
method: 'POST',
headers: {
// 'Access-Control-Request-Headers': 'authorization',
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
// 'Origin': `${process.env.REACT_APP_BASE_URL}`,
'Content-Type': 'application/json',
Authorization: `Bearer ${localStorage.getItem('auth_token')}`,
Origin: `${process.env.REACT_APP_BASE_URL}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(info)
})
@ -74,38 +19,98 @@ export const fetchForm = withAuthRedirect(async (link, info) => {
}
})
export const fetchAuth = async ({ username, password, dispatch, catchError }) => {
const baseURL = process.env.REACT_APP_BASE_URL;
const apiURL = process.env.REACT_APP_API_URL;
export const fetchAuth = async ({
username,
password,
dispatch,
catchError
}) => {
const baseURL = process.env.REACT_APP_BASE_URL
const apiURL = process.env.REACT_APP_API_URL
try {
const response = await fetch(
`${apiURL}/api/user/login`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
// 'Origin': `${baseURL}`,
},
body: JSON.stringify({
username,
password
})
}
)
const response = await fetch(`${apiURL}/api/user/login`, {
method: 'POST',
mode: 'cors',
headers: {
'Access-Control-Request-Headers': 'authorization',
'Content-Type': 'application/json',
// Origin: `http://localhost`
},
body: JSON.stringify({
username,
password
})
})
if(!response.ok) {
catchError();
return response.statusText;
if (!response.ok) {
catchError()
return response.statusText
}
response
.json()
.then((resJSON) => {
localStorage.setItem('auth_token', resJSON.access_token)
localStorage.setItem('access_token_expired_at', resJSON.access_token_expired_at)
dispatch();
})
response.json().then((resJSON) => {
localStorage.setItem('auth_token', resJSON.access_token)
localStorage.setItem(
'access_token_expired_at',
resJSON.access_token_expired_at
)
dispatch()
})
} catch (error) {
console.error('Error occured: ', error)
}
}
export const fetchReportList = withAuthRedirect(async (link) => {
try {
const response = await fetch(
`https://guild.loc/api/reports/index?user_id=26&fromDate=2021-10-18`,
// link,
{
method: 'GET',
headers: {
Authorization: `Bearer ${localStorage.getItem('auth_token')}`
}
}
)
let data = await response.json()
return data
} catch (error) {
console.log('Query error', error)
}
})
export const fetchGet = withAuthRedirect(async (link) => {
try {
const response = await fetch(link, {
method: 'GET',
headers: {
Authorization: `Bearer ${localStorage.getItem('auth_token')}`
}
})
let data = await response.json()
return data
} catch (error) {
console.log('Query error', error)
}
})
export const fetchPost = withAuthRedirect(async (link, body) => {
console.log('i',body)
try {
const response = await fetch(link, {
method: 'POST',
headers: {
Authorization: `Bearer ${localStorage.getItem('auth_token')}`,
'Content-Type': 'application/json',
Origin: `http://localhost`
},
body: JSON.stringify(body)
})
return response
} catch (error) {
console.log('Query error', error)
}
})