Skip to content

Commit 153980c

Browse files
committed
Refactored mutations syntax
1 parent 163eee8 commit 153980c

File tree

5 files changed

+148
-155
lines changed

5 files changed

+148
-155
lines changed

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ const state = {
1414
violations: null
1515
};
1616

17-
function error(commit, error) {
18-
return commit({{{ uc }}}_CREATE_ERROR, error);
17+
function error(error) {
18+
return {type: {{{ uc }}}_CREATE_ERROR, error};
1919
}
2020

21-
function loading(commit, loading) {
22-
return commit({{{ uc }}}_CREATE_LOADING, loading);
21+
function loading(loading) {
22+
return {type: {{{ uc }}}_CREATE_LOADING, loading};
2323
}
2424

25-
function success(commit, created) {
26-
return commit({{{ uc }}}_CREATE_SUCCESS, created);
25+
function success(created) {
26+
return {type: {{{ uc }}}_CREATE_SUCCESS, created};
2727
}
2828

29-
function violations(commit, violations) {
30-
return commit({{{ uc }}}_CREATE_VIOLATIONS, violations);
29+
function violations(violations) {
30+
return {type: {{{ uc }}}_CREATE_VIOLATIONS, violations};
3131
}
3232

33-
function reset(commit) {
34-
return commit({{{ uc }}}_CREATE_RESET);
33+
function reset() {
34+
return {type: {{{ uc }}}_CREATE_RESET};
3535
}
3636

3737
const getters = {
@@ -42,47 +42,47 @@ const getters = {
4242
};
4343

4444
const actions = {
45-
create({ commit }, values) {
46-
loading(commit, true);
45+
create({ dispatch }, values) {
46+
dispatch(loading(true));
4747

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

5252
return response.json();
5353
})
5454
.then(data => {
55-
success(commit, data);
55+
dispatch(success(data));
5656
})
5757
.catch(e => {
58-
loading(commit, false);
58+
dispatch(loading(false));
5959

6060
if (e instanceof SubmissionError) {
61-
violations(commit, e.errors);
62-
error(commit, e.errors._error);
61+
dispatch(violations(e.errors));
62+
dispatch(error(e.errors._error));
6363
return;
6464
}
6565

66-
error(commit, e.message);
66+
dispatch(error(e.message));
6767
});
6868
},
69-
reset({ commit }) {
70-
reset(commit);
69+
reset({ dispatch }) {
70+
dispatch(reset());
7171
}
7272
};
7373

7474
const mutations = {
75-
[{{{ uc }}}_CREATE_ERROR] (state, error) {
76-
state.error = error;
75+
[{{{ uc }}}_CREATE_ERROR] (state, payload) {
76+
state.error = payload.error;
7777
},
78-
[{{{ uc }}}_CREATE_LOADING] (state, loading) {
79-
state.loading = loading;
78+
[{{{ uc }}}_CREATE_LOADING] (state, payload) {
79+
state.loading = payload.loading;
8080
},
81-
[{{{ uc }}}_CREATE_SUCCESS] (state, created) {
82-
state.created = created;
81+
[{{{ uc }}}_CREATE_SUCCESS] (state, payload) {
82+
state.created = payload.created;
8383
},
84-
[{{{ uc }}}_CREATE_VIOLATIONS] (state, violations) {
85-
state.violations = violations;
84+
[{{{ uc }}}_CREATE_VIOLATIONS] (state, payload) {
85+
state.violations = payload.violations;
8686
},
8787
[{{{ uc }}}_CREATE_RESET] (state) {
8888
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)