Client: refactor

This commit is contained in:
Sam 2018-05-28 12:35:17 +02:00
parent 9f9bda005a
commit 8b2bbb505f
4 changed files with 101 additions and 123 deletions

View File

@ -1,44 +1,32 @@
import { apiUrl } from '../utils' import { apiUrl, createRequest } from '../utils'
export default class MpwoApi { export default class MpwoApi {
static addActivity(formData) { static addActivity(formData) {
const request = new Request(`${apiUrl}activities`, { const params = {
url: `${apiUrl}activities`,
method: 'POST', method: 'POST',
headers: new Headers({
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
}),
body: formData, body: formData,
}) }
return fetch(request) return createRequest(params)
.then(response => response.json())
.catch(error => error)
} }
static addActivityWithoutGpx(data) { static addActivityWithoutGpx(data) {
const request = new Request(`${apiUrl}activities/no_gpx`, { const params = {
url: `${apiUrl}activities/no_gpx`,
method: 'POST', method: 'POST',
headers: new Headers({ body: data,
'Content-Type': 'application/json', type: 'application/json',
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`, }
}), return createRequest(params)
body: JSON.stringify(data)
})
return fetch(request)
.then(response => response.json())
.catch(error => error)
} }
static getActivityGpx(activityId) { static getActivityGpx(activityId) {
const request = new Request(`${apiUrl}activities/${activityId}/gpx`, { const params = {
url: `${apiUrl}activities/${activityId}/gpx`,
method: 'GET', method: 'GET',
headers: new Headers({ }
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`, return createRequest(params)
}),
})
return fetch(request)
.then(response => response.json())
.catch(error => error)
} }
} }

View File

@ -1,4 +1,4 @@
import { apiUrl } from '../utils' import { apiUrl, createRequest } from '../utils'
export default class MpwoApi { export default class MpwoApi {
@ -9,56 +9,40 @@ export default class MpwoApi {
} else if (page) { } else if (page) {
url = `${url}?page=${page}` url = `${url}?page=${page}`
} }
const request = new Request(url, { const params = {
url: url,
method: 'GET', method: 'GET',
headers: new Headers({ type: 'application/json',
'Content-Type': 'application/json', }
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`, return createRequest(params)
}),
})
return fetch(request)
.then(response => response.json())
.catch(error => error)
} }
static addData(target, data) { static addData(target, data) {
const request = new Request(`${apiUrl}${target}`, { const params = {
url: `${apiUrl}${target}`,
method: 'POST', method: 'POST',
headers: new Headers({ body: data,
'Content-Type': 'application/json', type: 'application/json',
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`, }
}), return createRequest(params)
body: JSON.stringify(data)
})
return fetch(request)
.then(response => response.json())
.catch(error => error)
} }
static updateData(target, data) { static updateData(target, data) {
const request = new Request(`${apiUrl}${target}/${data.id}`, { const params = {
url: `${apiUrl}${target}/${data.id}`,
method: 'PATCH', method: 'PATCH',
headers: new Headers({ body: data,
'Content-Type': 'application/json', type: 'application/json',
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`, }
}), return createRequest(params)
body: JSON.stringify(data)
})
return fetch(request)
.then(response => response.json())
.catch(error => error)
} }
static deleteData(target, id) { static deleteData(target, id) {
const request = new Request(`${apiUrl}${target}/${id}`, { const params = {
url: `${apiUrl}${target}/${id}`,
method: 'DELETE', method: 'DELETE',
headers: new Headers({ type: 'application/json',
'Content-Type': 'application/json', }
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`, return createRequest(params)
}),
})
return fetch(request)
.then(response => response)
.catch(error => error)
} }
} }

View File

@ -1,62 +1,51 @@
import { apiUrl } from '../utils' import { apiUrl, createRequest } from '../utils'
export default class MpwoApiUser { export default class MpwoApiUser {
static login(email, password) { static login(email, password) {
const request = new Request(`${apiUrl}auth/login`, { const params = {
url: `${apiUrl}auth/login`,
method: 'POST', method: 'POST',
headers: new Headers({ noAuthorization: true,
'Content-Type': 'application/json', body: {
}),
body: JSON.stringify({
email: email, email: email,
password: password, password: password,
}), },
}) type: 'application/json',
return fetch(request) }
.then(response => response.json()) return createRequest(params)
.catch(error => error)
} }
static register(username, email, password, passwordConf) { static register(username, email, password, passwordConf) {
const request = new Request(`${apiUrl}auth/register`, { const params = {
url: `${apiUrl}auth/register`,
method: 'POST', method: 'POST',
headers: new Headers({ noAuthorization: true,
'Content-Type': 'application/json', body: {
}),
body: JSON.stringify({
username: username, username: username,
email: email, email: email,
password: password, password: password,
password_conf: passwordConf, password_conf: passwordConf,
}), },
}) type: 'application/json',
return fetch(request) }
.then(response => response.json()) return createRequest(params)
.catch(error => error)
} }
static getProfile() { static getProfile() {
const request = new Request(`${apiUrl}auth/profile`, { const params = {
url: `${apiUrl}auth/profile`,
method: 'GET', method: 'GET',
headers: new Headers({ type: 'application/json',
'Content-Type': 'application/json', }
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`, return createRequest(params)
}),
})
return fetch(request)
.then(response => response.json())
.catch(error => error)
} }
static updateProfile(form) { static updateProfile(form) {
const request = new Request(`${apiUrl}auth/profile/edit`, { const params = {
url: `${apiUrl}auth/profile/edit`,
method: 'POST', method: 'POST',
headers: new Headers({ body: {
'Content-Type': 'application/json',
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
}),
body: JSON.stringify({
first_name: form.firstName, first_name: form.firstName,
last_name: form.lastName, last_name: form.lastName,
bio: form.bio, bio: form.bio,
@ -64,35 +53,26 @@ export default class MpwoApiUser {
birth_date: form.birthDate, birth_date: form.birthDate,
password: form.password, password: form.password,
password_conf: form.passwordConf, password_conf: form.passwordConf,
}), },
}) type: 'application/json',
return fetch(request) }
.then(response => response.json()) return createRequest(params)
.catch(error => error)
} }
static updatePicture(form) { static updatePicture(form) {
const request = new Request(`${apiUrl}auth/picture`, { const params = {
url: `${apiUrl}auth/picture`,
method: 'POST', method: 'POST',
headers: new Headers({
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`,
}),
body: form, body: form,
}) }
return fetch(request) return createRequest(params)
.then(response => response.json())
.catch(error => error)
} }
static deletePicture() { static deletePicture() {
const request = new Request(`${apiUrl}auth/picture`, { const params = {
url: `${apiUrl}auth/picture`,
method: 'DELETE', method: 'DELETE',
headers: new Headers({ }
Authorization: `Bearer ${window.localStorage.getItem('authToken')}`, return createRequest(params)
}),
})
return fetch(request)
.then(response => response.json())
.catch(error => error)
} }
} }

View File

@ -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 => { export const getGeoJson = gpxContent => {
let jsonData let jsonData
if (gpxContent) { if (gpxContent) {