Skip to content

Commit cf27239

Browse files
committed
Refactored mutations syntax
1 parent a028642 commit cf27239

File tree

5 files changed

+153
-153
lines changed

5 files changed

+153
-153
lines changed

templates/vue/store/modules/foo/create.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ const state = {
1212
created: null
1313
};
1414

15-
function error(commit, error) {
16-
return commit({{{ uc }}}_CREATE_ERROR, error);
15+
function error(error) {
16+
return {type: {{{ uc }}}_CREATE_ERROR, error};
1717
}
1818

19-
function loading(commit, loading) {
20-
return commit({{{ uc }}}_CREATE_LOADING, loading);
19+
function loading(loading) {
20+
return {type: {{{ uc }}}_CREATE_LOADING, loading};
2121
}
2222

23-
function success(commit, created) {
24-
return commit({{{ uc }}}_CREATE_SUCCESS, created);
23+
function success(created) {
24+
return {type: {{{ uc }}}_CREATE_SUCCESS, created};
2525
}
2626

27-
function violations(commit, violations) {
28-
return commit({{{ uc }}}_CREATE_VIOLATIONS, violations);
27+
function violations(violations) {
28+
return {type: {{{ uc }}}_CREATE_VIOLATIONS, violations};
2929
}
3030

31-
function reset(commit) {
32-
return commit({{{ uc }}}_CREATE_RESET);
31+
function reset() {
32+
return {type: {{{ uc }}}_CREATE_RESET};
3333
}
3434

3535
const getters = {
@@ -40,40 +40,47 @@ const getters = {
4040
};
4141

4242
const actions = {
43-
create({ commit }) {
44-
loading(commit, true);
43+
create({ dispatch }, values) {
44+
dispatch(loading(true));
4545

4646
return {{{ lc }}}Fetch('/{{{ name }}}', {method: 'POST', body: JSON.stringify(values)})
4747
.then(response => {
48-
loading(commit, false);
48+
dispatch(loading(false));
4949

5050
return response.json();
5151
})
5252
.then(data => {
53-
success(commit, data);
53+
dispatch(success(data));
5454
})
5555
.catch(e => {
56-
loading(commit, false);
57-
error(commit, e.message);
56+
dispatch(loading(false));
57+
58+
if (e instanceof SubmissionError) {
59+
dispatch(violations(e.errors));
60+
dispatch(error(e.errors._error));
61+
return;
62+
}
63+
64+
dispatch(error(e.message));
5865
});
5966
},
60-
reset({ commit }) {
61-
reset(commit);
67+
reset({ dispatch }) {
68+
dispatch(reset());
6269
}
6370
};
6471

6572
const mutations = {
66-
[{{{ uc }}}_CREATE_ERROR] (state, error) {
67-
state.error = error;
73+
[{{{ uc }}}_CREATE_ERROR] (state, payload) {
74+
state.error = payload.error;
6875
},
69-
[{{{ uc }}}_CREATE_LOADING] (state, loading) {
70-
state.loading = loading;
76+
[{{{ uc }}}_CREATE_LOADING] (state, payload) {
77+
state.loading = payload.loading;
7178
},
72-
[{{{ uc }}}_CREATE_SUCCESS] (state, created) {
73-
state.created = created;
79+
[{{{ uc }}}_CREATE_SUCCESS] (state, payload) {
80+
state.created = payload.created;
7481
},
75-
[{{{ uc }}}_CREATE_VIOLATIONS] (state, violations) {
76-
state.violations = violations;
82+
[{{{ uc }}}_CREATE_VIOLATIONS] (state, payload) {
83+
state.violations = payload.violations;
7784
},
7885
[{{{ uc }}}_CREATE_RESET] (state) {
7986
state.loading = false;

templates/vue/store/modules/foo/delete.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ const state = {
1111
deleted: null
1212
};
1313

14-
function error(commit, error) {
15-
return commit({{{ uc }}}_DELETE_ERROR, error);
14+
function error(error) {
15+
return {type: {{{ uc }}}_DELETE_ERROR, error};
1616
}
1717

18-
function loading(commit, loading) {
19-
return commit({{{ uc }}}_DELETE_LOADING, loading);
18+
function loading(loading) {
19+
return {type: {{{ uc }}}_DELETE_LOADING, loading};
2020
}
2121

22-
function success(commit, deleted) {
23-
return commit({{{ uc }}}_DELETE_SUCCESS, deleted);
22+
function success(deleted) {
23+
return {type: {{{ uc }}}_DELETE_SUCCESS, deleted};
2424
}
2525

26-
function reset(commit) {
27-
return commit({{{ uc }}}_DELETE_RESET);
26+
function reset() {
27+
return {type: {{{ uc }}}_DELETE_RESET};
2828
}
2929

3030
const getters = {
@@ -34,33 +34,33 @@ const getters = {
3434
};
3535

3636
const actions = {
37-
delete({ commit }, item) {
38-
loading(commit, true);
37+
delete({ dispatch }, item) {
38+
dispatch(loading(true));
3939

4040
return {{{ lc }}}Fetch(item['@id'], {method: 'DELETE'})
4141
.then(() => {
42-
loading(commit, false);
43-
success(commit, item);
42+
dispatch(loading(false));
43+
dispatch(success(item));
4444
})
4545
.catch(e => {
46-
loading(commit, false);
47-
error(commit, e.message);
46+
dispatch(loading(false));
47+
dispatch(error(e.message));
4848
});
4949
},
50-
reset({ commit }) {
51-
reset(commit);
50+
reset({ dispatch }) {
51+
dispatch(reset());
5252
}
5353
};
5454

5555
const mutations = {
56-
[{{{ uc }}}_DELETE_ERROR] (state, error) {
57-
state.error = error;
56+
[{{{ uc }}}_DELETE_ERROR] (state, payload) {
57+
state.error = payload.error;
5858
},
59-
[{{{ uc }}}_DELETE_LOADING] (state, loading) {
60-
state.loading = loading;
59+
[{{{ uc }}}_DELETE_LOADING] (state, payload) {
60+
state.loading = payload.loading;
6161
},
62-
[{{{ uc }}}_DELETE_SUCCESS] (state, deleted) {
63-
state.deleted = deleted;
62+
[{{{ uc }}}_DELETE_SUCCESS] (state, payload) {
63+
state.deleted = payload.deleted;
6464
},
6565
[{{{ uc }}}_DELETE_RESET] (state) {
6666
state.error = '';

templates/vue/store/modules/foo/list.js

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,23 @@ const state = {
1010
loading: false,
1111
error: '',
1212
items: [],
13-
view: [],
14-
filters: {
15-
page: 1
16-
}
13+
view: []
1714
};
1815

19-
function error(commit, error) {
20-
return commit({{{ uc }}}_LIST_ERROR, error);
16+
function error(error) {
17+
return {type: {{{ uc }}}_LIST_ERROR, error};
2118
}
2219

23-
function loading(commit, loading) {
24-
return commit({{{ uc }}}_LIST_LOADING, loading);
20+
function loading(loading) {
21+
return {type: {{{ uc }}}_LIST_LOADING, loading};
2522
}
2623

27-
function success(commit, items) {
28-
return commit({{{ uc }}}_LIST_SUCCESS, items);
24+
function success(items) {
25+
return {type: {{{ uc }}}_LIST_SUCCESS, items};
2926
}
3027

31-
function view(commit, items) {
32-
return commit({{{ uc }}}_LIST_VIEW, items);
33-
}
34-
35-
function reset(commit) {
36-
return commit({{{ uc }}}_LIST_RESET);
28+
function view(items) {
29+
return { type: {{{ uc }}}_LIST_VIEW, items};
3730
}
3831

3932
const getters = {
@@ -42,38 +35,38 @@ const getters = {
4235
};
4336

4437
const actions = {
45-
getItems({ commit, state }) {
46-
loading(commit, true);
38+
getItems({ dispatch }) {
39+
dispatch(loading(true));
4740

48-
{{{ lc }}}Fetch('/{{{ name }}}', state.filters)
41+
{{{ lc }}}Fetch('/{{{ name }}}')
4942
.then(response => response.json())
5043
.then(data => {
51-
loading(commit, false);
52-
success(commit, data['{{{ hydraPrefix }}}member']);
53-
view(commit, data['{{{ hydraPrefix }}}view']);
44+
dispatch(loading(false));
45+
dispatch(success(data['{{{ hydraPrefix }}}member']));
46+
dispatch(view(data['{{{ hydraPrefix }}}view']));
5447
})
5548
.catch(e => {
56-
loading(commit, false);
57-
error(commit, e.message);
49+
dispatch(loading(false));
50+
dispatch(error(e.message));
5851
});
5952
}
6053
};
6154

6255
const mutations = {
63-
[{{{ uc }}}_LIST_ERROR] (state, error) {
64-
state.error = error;
56+
[{{{ uc }}}_LIST_ERROR] (state, payload) {
57+
state.error = payload.error;
6558
},
66-
[{{{ uc }}}_LIST_LOADING] (state, loading) {
67-
state.loading = loading;
59+
[{{{ uc }}}_LIST_LOADING] (state, payload) {
60+
state.loading = payload.loading;
6861
},
69-
[{{{ uc }}}_LIST_RESET] (state) {
70-
state.items = [];
62+
[{{{ uc }}}_LIST_VIEW] (state, payload) {
63+
state.view = payload.items;
7164
},
72-
[{{{ uc }}}_LIST_VIEW] (state, items) {
73-
state.view = items;
65+
[{{{ uc }}}_LIST_SUCCESS] (state, payload) {
66+
state.items = payload.items;
7467
},
75-
[{{{ uc }}}_LIST_SUCCESS] (state, items) {
76-
state.items = items;
68+
[{{{ uc }}}_LIST_RESET] (state) {
69+
state.items = [];
7770
}
7871
};
7972

templates/vue/store/modules/foo/show.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ const state = {
1111
retrieved: null
1212
};
1313

14-
function error(commit, error) {
15-
return commit({{{ uc }}}_SHOW_ERROR, error);
14+
function error(error) {
15+
return {type: {{{ uc }}}_SHOW_ERROR, error};
1616
}
1717

18-
function loading(commit, loading) {
19-
return commit({{{ uc }}}_SHOW_LOADING, loading);
18+
function loading(loading) {
19+
return {type: {{{ uc }}}_SHOW_LOADING, loading};
2020
}
2121

22-
function retrieved(commit, retrieved) {
23-
return commit({{{ uc }}}_SHOW_RETRIEVED_SUCCESS, retrieved);
22+
function retrieved(retrieved) {
23+
return {type: {{{ uc }}}_SHOW_RETRIEVED_SUCCESS, retrieved};
2424
}
2525

26-
function reset(commit) {
27-
return commit({{{ uc }}}_SHOW_RESET);
26+
function reset() {
27+
return {type: {{{ uc }}}_SHOW_RESET};
2828
}
2929

3030
const getters = {
@@ -34,34 +34,34 @@ const getters = {
3434
};
3535

3636
const actions = {
37-
retrieve({ commit }, id) {
38-
loading(commit, true);
37+
retrieve({ dispatch }, id) {
38+
dispatch(loading(true));
3939

4040
return {{{ lc }}}Fetch(id)
4141
.then(response => response.json())
4242
.then(data => {
43-
loading(commit, false);
44-
retrieved(commit, data);
43+
dispatch(loading(false));
44+
dispatch(retrieved(data));
4545
})
4646
.catch(e => {
47-
loading(commit, false);
48-
error(commit, e.message);
47+
dispatch(loading(false));
48+
dispatch(error(e.message));
4949
});
5050
},
51-
reset({ commit }) {
52-
reset(commit);
51+
reset({ dispatch }) {
52+
dispatch(reset());
5353
}
5454
};
5555

5656
const mutations = {
57-
[{{{ uc }}}_SHOW_ERROR] (state, error) {
58-
state.error = error;
57+
[{{{ uc }}}_SHOW_ERROR] (state, payload) {
58+
state.error = payload.error;
5959
},
60-
[{{{ uc }}}_SHOW_LOADING] (state, loading) {
61-
state.loading = loading;
60+
[{{{ uc }}}_SHOW_LOADING] (state, payload) {
61+
state.loading = payload.loading;
6262
},
63-
[{{{ uc }}}_SHOW_RETRIEVED_SUCCESS] (state, retrieved) {
64-
state.retrieved = retrieved;
63+
[{{{ uc }}}_SHOW_RETRIEVED_SUCCESS] (state, payload) {
64+
state.retrieved = payload.retrieved;
6565
},
6666
[{{{ uc }}}_SHOW_RESET] (state) {
6767
state.retrieved = null;

0 commit comments

Comments
 (0)