2018-04-29 18:23:46 +02:00
|
|
|
import { apiUrl } from '../utils'
|
|
|
|
|
|
|
|
export default class MpwoApi {
|
|
|
|
|
2018-05-10 23:39:59 +02:00
|
|
|
static getData(target, id = null, page = null) {
|
|
|
|
let url = `${apiUrl}${target}`
|
|
|
|
if (id) {
|
|
|
|
url = `${url}/${id}`
|
|
|
|
} else if (page) {
|
|
|
|
url = `${url}?page=${page}`
|
|
|
|
}
|
|
|
|
const request = new Request(url, {
|
2018-04-29 18:23:46 +02:00
|
|
|
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)
|
|
|
|
}
|
2018-04-30 20:08:18 +02:00
|
|
|
|
2018-04-30 21:38:09 +02:00
|
|
|
static addData(target, data) {
|
|
|
|
const request = new Request(`${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)
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:08:18 +02:00
|
|
|
static updateData(target, data) {
|
|
|
|
const request = new Request(`${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)
|
|
|
|
}
|
2018-04-30 21:38:09 +02:00
|
|
|
|
|
|
|
static deleteData(target, id) {
|
|
|
|
const request = new Request(`${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)
|
|
|
|
}
|
2018-04-29 18:23:46 +02:00
|
|
|
}
|