Skip to content

Commit de05b1d

Browse files
committed
Added Loading and Error States to each model and model field
1 parent 1e1a76a commit de05b1d

7 files changed

Lines changed: 223 additions & 37 deletions

File tree

dist/index.js

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions/Action.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ export default class Action {
2020
ModelConfig.http = _.merge({}, ModelConfig.http, context.options.http);
2121
model.methodConf = _.merge({}, ModelConfig, model.methodConf);
2222
model.methodConf.http.url = (model.methodConf.http.url === '/') ? `/${model.entity}` : model.methodConf.http.url;
23+
24+
/**
25+
* Add Model Interface to each model
26+
*/
27+
model.getFields = () => {
28+
if (!model.cachedFields) {
29+
model.cachedFields = _.merge({}, {
30+
$id: model.attr(undefined),
31+
$isUpdating: model.boolean(false),
32+
$updateErrors: model.attr([]),
33+
$isDeleting: model.boolean(false),
34+
$deleteErrors: model.attr([]),
35+
}, model.fields())
36+
}
37+
38+
return model.cachedFields;
39+
};
40+
2341
return model;
2442
}
2543

src/actions/Create.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,41 @@ export default class Create extends Action {
1919
const axios = new Axios(model.methodConf.http);
2020
const request = axios.post(endpoint, params.data);
2121

22-
commit('onRequest');
22+
this.onRequest(commit);
2323
request
24-
.then(data => {
25-
commit('onSuccess')
26-
model.insertOrUpdate({
27-
data,
28-
});
29-
})
30-
.catch(error => commit('onError', error))
24+
.then(data => this.onSuccess(commit, model, data))
25+
.catch(error => this.onError(commit, error))
3126

3227
return request;
3328
}
29+
30+
/**
31+
* On Request Method
32+
* @param {object} commit
33+
*/
34+
static onRequest(commit) {
35+
commit('onRequest');
36+
}
37+
38+
/**
39+
* On Successful Request Method
40+
* @param {object} commit
41+
* @param {object} model
42+
* @param {object} data
43+
*/
44+
static onSuccess(commit, model, data) {
45+
commit('onSuccess')
46+
model.insertOrUpdate({
47+
data,
48+
});
49+
}
50+
51+
/**
52+
* On Failed Request Method
53+
* @param {object} commit
54+
* @param {object} error
55+
*/
56+
static onError(commit, error) {
57+
commit('onError', error)
58+
}
3459
}

src/actions/Delete.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,54 @@ export default class Delete extends Action {
1515
const axios = new Axios(model.methodConf.http);
1616
const request = axios.delete(endpoint);
1717

18+
this.onRequest(model, params);
1819
request
19-
.then(data => {
20-
model.delete({
21-
where: (params.params.id) ? params.params.id : data.id,
22-
})
23-
})
20+
.then(data => this.onSuccess(model, params, data))
21+
.catch(error => this.onError(model, params, error))
2422

2523
return request;
2624
}
25+
26+
/**
27+
* On Request Method
28+
* @param {object} model
29+
* @param {object} params
30+
*/
31+
static onRequest(model, params) {
32+
model.update({
33+
where: params.params.id,
34+
data: {
35+
$isDeleting: true,
36+
$deleteErrors: []
37+
}
38+
})
39+
}
40+
41+
/**
42+
* On Successful Request Method
43+
* @param {object} model
44+
* @param {object} params
45+
* @param {object} data
46+
*/
47+
static onSuccess(model, params, data) {
48+
model.delete({
49+
where: params.params.id || data.id,
50+
})
51+
}
52+
53+
/**
54+
* On Failed Request Method
55+
* @param {object} model
56+
* @param {object} params
57+
* @param {object} error
58+
*/
59+
static onError(model, params, error) {
60+
model.update({
61+
where: params.params.id,
62+
data: {
63+
$isDeleting: false,
64+
$deleteErrors: error
65+
}
66+
})
67+
}
2768
}

src/actions/Fetch.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,41 @@ export default class Fetch extends Action {
1515
const axios = new Axios(model.methodConf.http);
1616
const request = axios.get(endpoint);
1717

18-
commit('onRequest');
18+
this.onRequest(commit);
1919
request
20-
.then(data => {
21-
commit('onSuccess')
22-
model.insertOrUpdate({
23-
data,
24-
});
25-
})
26-
.catch(error => commit('onError', error))
20+
.then(data => this.onSuccess(commit, model, data))
21+
.catch(error => this.onError(commit, error))
2722

2823
return request;
2924
}
25+
26+
/**
27+
* On Request Method
28+
* @param {object} commit
29+
*/
30+
static onRequest(commit) {
31+
commit('onRequest');
32+
}
33+
34+
/**
35+
* On Successful Request Method
36+
* @param {object} commit
37+
* @param {object} model
38+
* @param {object} data
39+
*/
40+
static onSuccess(commit, model, data) {
41+
commit('onSuccess')
42+
model.insertOrUpdate({
43+
data,
44+
});
45+
}
46+
47+
/**
48+
* On Failed Request Method
49+
* @param {object} commit
50+
* @param {object} error
51+
*/
52+
static onError(commit, error) {
53+
commit('onError', error)
54+
}
3055
}

src/actions/Get.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,41 @@ export default class Get extends Action {
1515
const axios = new Axios(model.methodConf.http);
1616
const request = axios.get(endpoint);
1717

18-
commit('onRequest');
18+
this.onRequest(commit);
1919
request
20-
.then(data => {
21-
commit('onSuccess')
22-
model.insertOrUpdate({
23-
data,
24-
});
25-
})
26-
.catch(error => commit('onError', error))
20+
.then(data => this.onSuccess(commit, model, data))
21+
.catch(error => this.onError(commit, error))
2722

2823
return request;
2924
}
25+
26+
/**
27+
* On Request Method
28+
* @param {object} commit
29+
*/
30+
static onRequest(commit) {
31+
commit('onRequest');
32+
}
33+
34+
/**
35+
* On Successful Request Method
36+
* @param {object} commit
37+
* @param {object} model
38+
* @param {object} data
39+
*/
40+
static onSuccess(commit, model, data) {
41+
commit('onSuccess')
42+
model.insertOrUpdate({
43+
data,
44+
});
45+
}
46+
47+
/**
48+
* On Failed Request Method
49+
* @param {object} commit
50+
* @param {object} error
51+
*/
52+
static onError(commit, error) {
53+
commit('onError', error)
54+
}
3055
}

src/actions/Update.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash';
12
import Axios from '../orm/axios';
23
import Action from './Action'
34
import Context from '../common/context'
@@ -19,14 +20,58 @@ export default class Update extends Action {
1920
const axios = new Axios(model.methodConf.http);
2021
const request = axios.put(endpoint, params.data);
2122

23+
this.onRequest(model, params);
2224
request
23-
.then(data => {
24-
model.update({
25-
where: data.id,
26-
data
27-
})
28-
})
25+
.then(data => this.onSuccess(model, params, data))
26+
.catch(error => this.onError(model, params, error))
2927

3028
return request;
3129
}
30+
31+
/**
32+
* On Request Method
33+
* @param {object} model
34+
* @param {object} params
35+
*/
36+
static onRequest(model, params) {
37+
model.update({
38+
where: params.params.id,
39+
data: {
40+
$isUpdating: true,
41+
$updateErrors: []
42+
}
43+
})
44+
}
45+
46+
/**
47+
* On Successful Request Method
48+
* @param {object} model
49+
* @param {object} params
50+
* @param {object} data
51+
*/
52+
static onSuccess(model, params, data) {
53+
model.update({
54+
where: params.params.id || data.id,
55+
data: _.merge({}, data, {
56+
$isUpdating: false,
57+
$updateErrors: []
58+
})
59+
})
60+
}
61+
62+
/**
63+
* On Failed Request Method
64+
* @param {object} model
65+
* @param {object} params
66+
* @param {object} error
67+
*/
68+
static onError(model, params, error) {
69+
model.update({
70+
where: params.params.id,
71+
data: {
72+
$isUpdating: false,
73+
$updateErrors: error
74+
}
75+
})
76+
}
3277
}

0 commit comments

Comments
 (0)