Skip to content
This repository was archived by the owner on Feb 13, 2020. It is now read-only.

Commit 3f06702

Browse files
committed
Finish part2 - recomposition
1 parent c1b0403 commit 3f06702

File tree

2 files changed

+72
-74
lines changed

2 files changed

+72
-74
lines changed

src/modulator/index.js

Lines changed: 68 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,86 +16,84 @@ const camelize = function (snakeText, capitalizeFirstLetter = true) {
1616
return out
1717
}
1818

19-
const mutations = function (singular) {
20-
const mutations = {}
19+
export default {
20+
mutations (singular) {
21+
const mutations = {}
2122

22-
mutations[`set_${singular}`] = (state, entity) => { Vue.set(state, entity.id, entity) }
23-
mutations[`remove_${singular}`] = (state, id) => {Vue.delete(state, id)}
23+
mutations[`set_${singular}`] = (state, entity) => { Vue.set(state, entity.id, entity) }
24+
mutations[`remove_${singular}`] = (state, id) => {Vue.delete(state, id)}
2425

25-
return mutations
26-
}
27-
28-
const actions = function (endpoint, singular, plural, editable = true, destroyable = true) {
29-
const camelSingular = camelize(singular)
30-
const camelPlural = camelize(plural)
31-
const actions = {}
26+
return mutations
27+
},
28+
actions (endpoint, singular, plural, editable = true, destroyable = true) {
29+
const camelSingular = camelize(singular)
30+
const camelPlural = camelize(plural)
31+
const actions = {}
3232

33-
actions[`load${camelPlural}`] = ({commit}) => {
34-
api.get(endpoint)
35-
.then((data) => {
36-
for (let i = 0; i < data.length; i++) {
37-
commit(`set_${singular}`, data[i])
38-
}
39-
})
40-
.catch((error) => {console.log(`ERROR in load${camelPlural}:`, error)})
41-
}
42-
actions[`load${camelSingular}`] = ({commit}, id) => {
43-
api.get(`${endpoint}/${id}`)
44-
.then((data) => {commit(`set_${singular}`, data)})
45-
.catch((error) => {console.log(`ERROR in load${camelSingular}:`, error)})
46-
}
47-
if (editable) {
48-
actions[`create${camelSingular}`] = ({commit}, payload) => {
49-
api.post(endpoint, payload)
33+
actions[`load${camelPlural}`] = ({commit}) => {
34+
api.get(endpoint)
5035
.then((data) => {
51-
// Let's assume "data" is the new article, sent back by the API
52-
// We don't want to store the user input in our store :)
53-
commit(`set_${singular}`, data)
36+
for (let i = 0; i < data.length; i++) {
37+
commit(`set_${singular}`, data[i])
38+
}
5439
})
55-
.catch((error) => {console.log(`ERROR in create${camelSingular}:`, error)})
40+
.catch((error) => {console.log(`ERROR in load${camelPlural}:`, error)})
5641
}
57-
actions[`update${camelSingular}`] = ({commit}, payload) => {
58-
api.patch(`${endpoint}/${payload.id}`, payload)
59-
.then((data) => {
60-
// Let's assume "data" is the updated article
61-
commit(`set_${singular}`, data)
62-
})
63-
.catch((error) => {console.log(`ERROR in update${camelSingular}:`, error)})
42+
actions[`load${camelSingular}`] = ({commit}, id) => {
43+
api.get(`${endpoint}/${id}`)
44+
.then((data) => {commit(`set_${singular}`, data)})
45+
.catch((error) => {console.log(`ERROR in load${camelSingular}:`, error)})
6446
}
65-
}
66-
if (destroyable) {
67-
actions[`destroy${camelSingular}`] = ({commit}, id) => {
68-
api.delete(`${endpoint}/${id}`)
69-
.then(() => { commit(`remove_${singular}`, id) })
70-
.catch((error) => {console.log('ERROR in DESTROY_ARTICLE:', error)})
47+
if (editable) {
48+
actions[`create${camelSingular}`] = ({commit}, payload) => {
49+
api.post(endpoint, payload)
50+
.then((data) => {
51+
// Let's assume "data" is the new article, sent back by the API
52+
// We don't want to store the user input in our store :)
53+
commit(`set_${singular}`, data)
54+
})
55+
.catch((error) => {console.log(`ERROR in create${camelSingular}:`, error)})
56+
}
57+
actions[`update${camelSingular}`] = ({commit}, payload) => {
58+
api.patch(`${endpoint}/${payload.id}`, payload)
59+
.then((data) => {
60+
// Let's assume "data" is the updated article
61+
commit(`set_${singular}`, data)
62+
})
63+
.catch((error) => {console.log(`ERROR in update${camelSingular}:`, error)})
64+
}
65+
}
66+
if (destroyable) {
67+
actions[`destroy${camelSingular}`] = ({commit}, id) => {
68+
api.delete(`${endpoint}/${id}`)
69+
.then(() => { commit(`remove_${singular}`, id) })
70+
.catch((error) => {console.log('ERROR in DESTROY_ARTICLE:', error)})
71+
}
7172
}
72-
}
73-
74-
return actions
75-
}
76-
77-
const getters = function (singular, plural) {
78-
const lowCamelSingular = camelize(singular, false)
79-
const lowCamelPlural = camelize(plural, false)
80-
81-
const getters = {}
8273

83-
getters[lowCamelPlural] = state => state
84-
getters[lowCamelSingular] = state => id => state[id] || undefined
74+
return actions
75+
},
76+
getters (singular, plural) {
77+
const lowCamelSingular = camelize(singular, false)
78+
const lowCamelPlural = camelize(plural, false)
8579

86-
return getters
87-
}
80+
const getters = {}
8881

89-
export default function (endpoint, singular, plural, editable = true, destroyable = true) {
90-
const module = {
91-
state: {}, mutations: {
92-
...mutations(singular),
93-
}, actions: {
94-
...actions(endpoint, singular, plural, editable, destroyable),
95-
}, getters: {
96-
...getters(singular, plural),
97-
},
98-
}
82+
getters[lowCamelPlural] = state => state
83+
getters[lowCamelSingular] = state => id => state[id] || undefined
9984

100-
return module
85+
return getters
86+
},
87+
generateModule (endpoint, singular, plural, editable = true, destroyable = true) {
88+
return {
89+
state: {},
90+
mutations: {
91+
...this.mutations(singular),
92+
}, actions: {
93+
...this.actions(endpoint, singular, plural, editable, destroyable),
94+
}, getters: {
95+
...this.getters(singular, plural),
96+
},
97+
}
98+
},
10199
}

src/store.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex'
3-
import moduleGenerator from './modulator'
3+
import Modulator from './modulator'
44

55
Vue.use(Vuex)
66

@@ -9,8 +9,8 @@ export default new Vuex.Store({
99
mutations: {},
1010
actions: {},
1111
modules: {
12-
users: moduleGenerator('users', 'user', 'users', true, false),
13-
posts: moduleGenerator('posts', 'post', 'posts'),
14-
postTypes: moduleGenerator('post_types', 'post_type', 'post_types'),
12+
users: Modulator.generateModule('users', 'user', 'users', true, false),
13+
posts: Modulator.generateModule('posts', 'post', 'posts'),
14+
postTypes: Modulator.generateModule('post_types', 'post_type', 'post_types'),
1515
}
1616
})

0 commit comments

Comments
 (0)