Client: refactor
This commit is contained in:
parent
9f9bda005a
commit
8b2bbb505f
@ -1,44 +1,32 @@
|
||||
import { apiUrl } from '../utils'
|
||||
import { apiUrl, createRequest } from '../utils'
|
||||
|
||||
export default class MpwoApi {
|
||||
|
||||
static addActivity(formData) {
|
||||
const request = new Request(`${apiUrl}activities`, {
|
||||
const params = {
|
||||
url: `${apiUrl}activities`,
|
||||
method: 'POST',
|
||||
headers: new Headers({
|
||||
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
|
||||
}),
|
||||
body: formData,
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
|
||||
static addActivityWithoutGpx(data) {
|
||||
const request = new Request(`${apiUrl}activities/no_gpx`, {
|
||||
const params = {
|
||||
url: `${apiUrl}activities/no_gpx`,
|
||||
method: 'POST',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
|
||||
}),
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
body: data,
|
||||
type: 'application/json',
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
|
||||
static getActivityGpx(activityId) {
|
||||
const request = new Request(`${apiUrl}activities/${activityId}/gpx`, {
|
||||
const params = {
|
||||
url: `${apiUrl}activities/${activityId}/gpx`,
|
||||
method: 'GET',
|
||||
headers: new Headers({
|
||||
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
|
||||
}),
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { apiUrl } from '../utils'
|
||||
import { apiUrl, createRequest } from '../utils'
|
||||
|
||||
export default class MpwoApi {
|
||||
|
||||
@ -9,56 +9,40 @@ export default class MpwoApi {
|
||||
} else if (page) {
|
||||
url = `${url}?page=${page}`
|
||||
}
|
||||
const request = new Request(url, {
|
||||
const params = {
|
||||
url: url,
|
||||
method: 'GET',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
|
||||
}),
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
type: 'application/json',
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
|
||||
static addData(target, data) {
|
||||
const request = new Request(`${apiUrl}${target}`, {
|
||||
const params = {
|
||||
url: `${apiUrl}${target}`,
|
||||
method: 'POST',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
|
||||
}),
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
body: data,
|
||||
type: 'application/json',
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
|
||||
static updateData(target, data) {
|
||||
const request = new Request(`${apiUrl}${target}/${data.id}`, {
|
||||
const params = {
|
||||
url: `${apiUrl}${target}/${data.id}`,
|
||||
method: 'PATCH',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
|
||||
}),
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
body: data,
|
||||
type: 'application/json',
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
|
||||
static deleteData(target, id) {
|
||||
const request = new Request(`${apiUrl}${target}/${id}`, {
|
||||
const params = {
|
||||
url: `${apiUrl}${target}/${id}`,
|
||||
method: 'DELETE',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
|
||||
}),
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response)
|
||||
.catch(error => error)
|
||||
type: 'application/json',
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +1,51 @@
|
||||
import { apiUrl } from '../utils'
|
||||
import { apiUrl, createRequest } from '../utils'
|
||||
|
||||
export default class MpwoApiUser {
|
||||
|
||||
static login(email, password) {
|
||||
const request = new Request(`${apiUrl}auth/login`, {
|
||||
const params = {
|
||||
url: `${apiUrl}auth/login`,
|
||||
method: 'POST',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
}),
|
||||
body: JSON.stringify({
|
||||
noAuthorization: true,
|
||||
body: {
|
||||
email: email,
|
||||
password: password,
|
||||
}),
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
},
|
||||
type: 'application/json',
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
|
||||
static register(username, email, password, passwordConf) {
|
||||
const request = new Request(`${apiUrl}auth/register`, {
|
||||
const params = {
|
||||
url: `${apiUrl}auth/register`,
|
||||
method: 'POST',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
}),
|
||||
body: JSON.stringify({
|
||||
noAuthorization: true,
|
||||
body: {
|
||||
username: username,
|
||||
email: email,
|
||||
password: password,
|
||||
password_conf: passwordConf,
|
||||
}),
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
},
|
||||
type: 'application/json',
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
|
||||
static getProfile() {
|
||||
const request = new Request(`${apiUrl}auth/profile`, {
|
||||
const params = {
|
||||
url: `${apiUrl}auth/profile`,
|
||||
method: 'GET',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
|
||||
}),
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
type: 'application/json',
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
|
||||
static updateProfile(form) {
|
||||
const request = new Request(`${apiUrl}auth/profile/edit`, {
|
||||
const params = {
|
||||
url: `${apiUrl}auth/profile/edit`,
|
||||
method: 'POST',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
|
||||
}),
|
||||
body: JSON.stringify({
|
||||
body: {
|
||||
first_name: form.firstName,
|
||||
last_name: form.lastName,
|
||||
bio: form.bio,
|
||||
@ -64,35 +53,26 @@ export default class MpwoApiUser {
|
||||
birth_date: form.birthDate,
|
||||
password: form.password,
|
||||
password_conf: form.passwordConf,
|
||||
}),
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
},
|
||||
type: 'application/json',
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
|
||||
static updatePicture(form) {
|
||||
const request = new Request(`${apiUrl}auth/picture`, {
|
||||
const params = {
|
||||
url: `${apiUrl}auth/picture`,
|
||||
method: 'POST',
|
||||
headers: new Headers({
|
||||
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
|
||||
}),
|
||||
body: form,
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
|
||||
static deletePicture() {
|
||||
const request = new Request(`${apiUrl}auth/picture`, {
|
||||
const params = {
|
||||
url: `${apiUrl}auth/picture`,
|
||||
method: 'DELETE',
|
||||
headers: new Headers({
|
||||
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
|
||||
}),
|
||||
})
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
}
|
||||
return createRequest(params)
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,32 @@ export const generateIds = arr => {
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
export const createRequest = params => {
|
||||
const headers = {}
|
||||
if (!params.noAuthorization) {
|
||||
headers.Authorization = `Bearer ${
|
||||
window.localStorage.getItem('authToken')}`
|
||||
}
|
||||
if (params.type) {
|
||||
headers['Content-Type'] = params.type
|
||||
}
|
||||
const requestParams = {
|
||||
method: params.method,
|
||||
headers: headers,
|
||||
}
|
||||
if (params.type === 'application/json' && params.body) {
|
||||
requestParams.body = JSON.stringify(params.body)
|
||||
} else if (params.body) {
|
||||
requestParams.body = params.body
|
||||
}
|
||||
const request = new Request(params.url, requestParams)
|
||||
return fetch(request)
|
||||
.then(response => response.json())
|
||||
.catch(error => error)
|
||||
}
|
||||
|
||||
|
||||
export const getGeoJson = gpxContent => {
|
||||
let jsonData
|
||||
if (gpxContent) {
|
||||
|
Loading…
Reference in New Issue
Block a user