Move everything to models and services #17
|
@ -3,10 +3,6 @@ import {reduce, replace} from 'lodash'
|
|||
|
||||
let config = require('../../public/config.json')
|
||||
|
||||
// TODO: Define before{get|create|update} functions to be able to modify the model before sending
|
||||
// it, after send (=recieving) is handled by the constructor.
|
||||
// Needed to handle stuff like making unix timestamps from js dates
|
||||
|
||||
export default class AbstractService {
|
||||
|
||||
/////////////////////////////
|
||||
|
@ -112,7 +108,7 @@ export default class AbstractService {
|
|||
* setLoading is a method which sets the loading variable to true, after a timeout of 100ms.
|
||||
* It has the timeout to prevent the loading indicator from showing for only a blink of an eye in the
|
||||
* case the api returns a response in < 100ms.
|
||||
* But because the timeout is created using setTimeout, it will still triggerm even if the request is
|
||||
* But because the timeout is created using setTimeout, it will still trigger even if the request is
|
||||
* already finished, so we return a method to call in that case.
|
||||
* @returns {Function}
|
||||
*/
|
||||
|
@ -136,6 +132,46 @@ export default class AbstractService {
|
|||
return data
|
||||
}
|
||||
|
||||
//////////////
|
||||
// Preprocessors
|
||||
////////////
|
||||
|
||||
/**
|
||||
* Default preprocessor for get requests
|
||||
* @param model
|
||||
* @return {*}
|
||||
*/
|
||||
beforeGet(model) {
|
||||
return model
|
||||
}
|
||||
|
||||
/**
|
||||
* Default preprocessor for create requests
|
||||
* @param model
|
||||
* @return {*}
|
||||
*/
|
||||
beforeCreate(model) {
|
||||
return model
|
||||
}
|
||||
|
||||
/**
|
||||
* Default preprocessor for update requests
|
||||
* @param model
|
||||
* @return {*}
|
||||
*/
|
||||
beforeUpdate(model) {
|
||||
return model
|
||||
}
|
||||
|
||||
/**
|
||||
* Default preprocessor for delete requests
|
||||
* @param model
|
||||
* @return {*}
|
||||
*/
|
||||
beforeDelete(model) {
|
||||
return model
|
||||
}
|
||||
|
||||
///////////////
|
||||
// Global actions
|
||||
/////////////
|
||||
|
@ -151,10 +187,8 @@ export default class AbstractService {
|
|||
return Promise.reject({message: 'This model is not able to get data.'})
|
||||
}
|
||||
|
||||
// We later will be able to cancel the loading indicator using this.
|
||||
const cancel = this.setLoading()
|
||||
|
||||
// Finally make the request and get our data.
|
||||
model = this.beforeGet(model)
|
||||
return this.http.get(this.getReplacedRoute(this.paths.get, model), {params: params})
|
||||
.catch(error => {
|
||||
return this.errorHandler(error)
|
||||
|
@ -178,8 +212,7 @@ export default class AbstractService {
|
|||
}
|
||||
|
||||
const cancel = this.setLoading()
|
||||
|
||||
// Finally make the request and get our data.
|
||||
model = this.beforeCreate(model)
|
||||
return this.http.put(this.getReplacedRoute(this.paths.create, model), model)
|
||||
.catch(error => {
|
||||
return this.errorHandler(error)
|
||||
|
@ -201,12 +234,9 @@ export default class AbstractService {
|
|||
if (this.paths.update === '') {
|
||||
return Promise.reject({message: 'This model is not able to update data.'})
|
||||
}
|
||||
|
||||
const cancel = this.setLoading()
|
||||
|
||||
// Finally make the request and get our data.
|
||||
let url = this.getReplacedRoute(this.paths.update, model)
|
||||
// eslint-disable-next-line
|
||||
console.log(url, model)
|
||||
model = this.beforeUpdate(model)
|
||||
return this.http.post(this.getReplacedRoute(this.paths.update, model), model)
|
||||
.catch(error => {
|
||||
return this.errorHandler(error)
|
||||
|
|
Reference in New Issue
Block a user