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

Commit 866fd14

Browse files
committed
Finish part 1
1 parent 8446bbd commit 866fd14

File tree

2 files changed

+87
-8
lines changed

2 files changed

+87
-8
lines changed

src/modulator/index.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// We need Vue to ensure proper updates in mutations
2+
import Vue from 'vue'
3+
4+
const camelize = function (snakeText, capitalizeFirstLetter = true) {
5+
let regexp = /(_\w)/g
6+
if (capitalizeFirstLetter) {
7+
regexp = /(^\w|_\w)/g
8+
}
9+
const out = snakeText.replace(regexp, (match) => {
10+
if (match.length > 1) {
11+
return match[1].toUpperCase()
12+
} else {
13+
return match.toUpperCase()
14+
}
15+
})
16+
return out
17+
}
18+
19+
export default function (endpoint, singular, plural, editable = true, destroyable = true) {
20+
const lowCamelSingular = camelize(singular, false)
21+
const lowCamelPlural = camelize(plural, false)
22+
const camelSingular = camelize(singular)
23+
const camelPlural = camelize(plural)
24+
25+
// Empty module
26+
const module = {state: {}, mutations: {}, actions: {}, getters: {}}
27+
28+
// Mutators:
29+
module.mutations[`set_${singular}`] = (state, entity) => { Vue.set(state, entity.id, entity) }
30+
module.mutations[`remove_${singular}`] = (state, id) => {Vue.delete(state, id)}
31+
32+
// Actions:
33+
module.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+
module.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+
module.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+
module.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+
module.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+
}
72+
}
73+
74+
// Getters
75+
module.getters[lowCamelPlural] = state => state
76+
module.getters[lowCamelSingular] = state => id => state[id] || undefined
77+
78+
return module
79+
}

src/store.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex'
3+
import moduleGenerator from './modulator'
34

45
Vue.use(Vuex)
56

67
export default new Vuex.Store({
7-
state: {
8-
9-
},
10-
mutations: {
11-
12-
},
13-
actions: {
14-
8+
state: {},
9+
mutations: {},
10+
actions: {},
11+
modules: {
12+
users: moduleGenerator('users', 'user', 'users', true, false),
13+
posts: moduleGenerator('posts', 'post', 'posts'),
14+
postTypes: moduleGenerator('post_types', 'post_type', 'post_types'),
1515
}
1616
})

0 commit comments

Comments
 (0)