refactor
This commit is contained in:
parent
012c83121d
commit
f14d5a1dbf
@ -27,7 +27,7 @@ export const getOrUpdateData = (action, target, data) => dispatch => {
|
|||||||
if (ret.status === 'success') {
|
if (ret.status === 'success') {
|
||||||
dispatch(setData(target, ret.data))
|
dispatch(setData(target, ret.data))
|
||||||
} else {
|
} else {
|
||||||
dispatch(setError(`${target}: ${ret.message}`))
|
dispatch(setError(`${target}: ${ret.message || ret.status}`))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => dispatch(setError(`${target}: ${error}`)))
|
.catch(error => dispatch(setError(`${target}: ${error}`)))
|
||||||
@ -54,7 +54,7 @@ export const deleteData = (target, id) => dispatch => {
|
|||||||
if (ret.status === 204) {
|
if (ret.status === 204) {
|
||||||
history.push(`/admin/${target}`)
|
history.push(`/admin/${target}`)
|
||||||
} else {
|
} else {
|
||||||
dispatch(setError(`${target}: ${ret.message}`))
|
dispatch(setError(`${target}: ${ret.message || ret.status}`))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => dispatch(setError(`${target}: ${error}`)))
|
.catch(error => dispatch(setError(`${target}: ${error}`)))
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
import { apiUrl, createRequest } from '../utils'
|
import { createApiRequest } from '../utils'
|
||||||
|
|
||||||
export default class FitTrackeeApi {
|
export default class FitTrackeeApi {
|
||||||
|
|
||||||
static getData(target,
|
static getData(target, data = {}) {
|
||||||
data = {}) {
|
let url = target
|
||||||
let url = `${apiUrl}${target}`
|
|
||||||
if (data.id) {
|
if (data.id) {
|
||||||
url = `${url}/${data.id}`
|
url = `${url}/${data.id}`
|
||||||
} else if (Object.keys(data).length > 0) {
|
} else if (Object.keys(data).length > 0) {
|
||||||
@ -16,54 +15,54 @@ export default class FitTrackeeApi {
|
|||||||
method: 'GET',
|
method: 'GET',
|
||||||
type: 'application/json',
|
type: 'application/json',
|
||||||
}
|
}
|
||||||
return createRequest(params)
|
return createApiRequest(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
static addData(target, data) {
|
static addData(target, data) {
|
||||||
const params = {
|
const params = {
|
||||||
url: `${apiUrl}${target}`,
|
url: target,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: data,
|
body: data,
|
||||||
type: 'application/json',
|
type: 'application/json',
|
||||||
}
|
}
|
||||||
return createRequest(params)
|
return createApiRequest(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
static addDataWithFile(target, data) {
|
static addDataWithFile(target, data) {
|
||||||
const params = {
|
const params = {
|
||||||
url: `${apiUrl}${target}`,
|
url: target,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: data,
|
body: data,
|
||||||
}
|
}
|
||||||
return createRequest(params)
|
return createApiRequest(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
static postData(target, data) {
|
static postData(target, data) {
|
||||||
const params = {
|
const params = {
|
||||||
url: `${apiUrl}${target}${data.id ? `/${data.id}` : '' }`,
|
url: `${target}${data.id ? `/${data.id}` : '' }`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: data,
|
body: data,
|
||||||
type: 'application/json',
|
type: 'application/json',
|
||||||
}
|
}
|
||||||
return createRequest(params)
|
return createApiRequest(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
static updateData(target, data) {
|
static updateData(target, data) {
|
||||||
const params = {
|
const params = {
|
||||||
url: `${apiUrl}${target}/${data.id}`,
|
url: `${target}/${data.id}`,
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
body: data,
|
body: data,
|
||||||
type: 'application/json',
|
type: 'application/json',
|
||||||
}
|
}
|
||||||
return createRequest(params)
|
return createApiRequest(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
static deleteData(target, id) {
|
static deleteData(target, id) {
|
||||||
const params = {
|
const params = {
|
||||||
url: `${apiUrl}${target}/${id}`,
|
url: `${target}/${id}`,
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
type: 'application/json',
|
type: 'application/json',
|
||||||
}
|
}
|
||||||
return createRequest(params)
|
return createApiRequest(params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,23 @@
|
|||||||
import { apiUrl, createRequest } from '../utils'
|
import { createApiRequest } from '../utils'
|
||||||
|
|
||||||
export default class FitTrackeeApi {
|
export default class FitTrackeeApi {
|
||||||
|
|
||||||
static loginOrRegister(target, data) {
|
static loginOrRegister(target, data) {
|
||||||
const params = {
|
const params = {
|
||||||
url: `${apiUrl}auth/${target}`,
|
url: `auth/${target}`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
noAuthorization: true,
|
noAuthorization: true,
|
||||||
body: data,
|
body: data,
|
||||||
type: 'application/json',
|
type: 'application/json',
|
||||||
}
|
}
|
||||||
return createRequest(params)
|
return createApiRequest(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
static deletePicture() {
|
static deletePicture() {
|
||||||
const params = {
|
const params = {
|
||||||
url: `${apiUrl}auth/picture`,
|
url: 'auth/picture',
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
}
|
}
|
||||||
return createRequest(params)
|
return createApiRequest(params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ export const generateIds = arr => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const createRequest = params => {
|
export const createApiRequest = params => {
|
||||||
const headers = {}
|
const headers = {}
|
||||||
if (!params.noAuthorization) {
|
if (!params.noAuthorization) {
|
||||||
headers.Authorization = `Bearer ${
|
headers.Authorization = `Bearer ${
|
||||||
@ -48,12 +48,15 @@ export const createRequest = params => {
|
|||||||
} else if (params.body) {
|
} else if (params.body) {
|
||||||
requestParams.body = params.body
|
requestParams.body = params.body
|
||||||
}
|
}
|
||||||
const request = new Request(params.url, requestParams)
|
const request = new Request(`${apiUrl}${params.url}`, requestParams)
|
||||||
return fetch(request)
|
return fetch(request)
|
||||||
.then(response => params.method === 'DELETE'
|
.then(response => params.method === 'DELETE'
|
||||||
? response
|
? response
|
||||||
: response.json())
|
: response.json())
|
||||||
.catch(error => error)
|
.catch(error => {
|
||||||
|
console.error(error)
|
||||||
|
return new Error('An error occurred. Please contact the administrator.')
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user