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

Commit 8b3f4f7

Browse files
committed
Use "findAndDispatchEntities" in Modulator
1 parent b7d61af commit 8b3f4f7

2 files changed

Lines changed: 41 additions & 8 deletions

File tree

src/modulator/index.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// We need Vue to ensure proper updates in mutations
22
import Vue from 'vue'
33
import utils from '../utils'
4+
import findAndDispatchEntities from '../data_types'
45

56
export default {
67
mutations (singular) {
@@ -16,35 +17,44 @@ export default {
1617
const camelPlural = utils.camelize(plural)
1718
const actions = {}
1819

19-
actions[`load${camelPlural}`] = ({commit}) => {
20+
actions[`dispatchAndCommit${camelSingular}`] = ({commit, dispatch}, entity) => {
21+
findAndDispatchEntities(entity, dispatch, singular)
22+
.then((cleanEntity) => {
23+
// Commit the cleaned entity: all the fields where removed by findAndDispatchEntities
24+
commit(`set_${singular}`, cleanEntity)
25+
})
26+
.catch((error) => {console.log('An error occurred while processing data')})
27+
}
28+
29+
actions[`load${camelPlural}`] = ({dispatch}) => {
2030
api.get(endpoint)
2131
.then((data) => {
2232
for (let i = 0; i < data.length; i++) {
23-
commit(`set_${singular}`, data[i])
33+
dispatch(`dispatchAndCommit${camelSingular}`, data[i])
2434
}
2535
})
2636
.catch((error) => {console.log(`ERROR in load${camelPlural}:`, error)})
2737
}
28-
actions[`load${camelSingular}`] = ({commit}, id) => {
38+
actions[`load${camelSingular}`] = ({dispatch}, id) => {
2939
api.get(`${endpoint}/${id}`)
30-
.then((data) => {commit(`set_${singular}`, data)})
40+
.then((data) => {dispatch(`dispatchAndCommit${camelSingular}`, data)})
3141
.catch((error) => {console.log(`ERROR in load${camelSingular}:`, error)})
3242
}
3343
if (editable) {
34-
actions[`create${camelSingular}`] = ({commit}, payload) => {
44+
actions[`create${camelSingular}`] = ({dispatch}, payload) => {
3545
api.post(endpoint, payload)
3646
.then((data) => {
3747
// Let's assume "data" is the new article, sent back by the API
3848
// We don't want to store the user input in our store :)
39-
commit(`set_${singular}`, data)
49+
dispatch(`dispatchAndCommit${camelSingular}`, data)
4050
})
4151
.catch((error) => {console.log(`ERROR in create${camelSingular}:`, error)})
4252
}
43-
actions[`update${camelSingular}`] = ({commit}, payload) => {
53+
actions[`update${camelSingular}`] = ({dispatch}, payload) => {
4454
api.patch(`${endpoint}/${payload.id}`, payload)
4555
.then((data) => {
4656
// Let's assume "data" is the updated article
47-
commit(`set_${singular}`, data)
57+
dispatch(`dispatchAndCommit${camelSingular}`, data)
4858
})
4959
.catch((error) => {console.log(`ERROR in update${camelSingular}:`, error)})
5060
}
@@ -67,6 +77,9 @@ export default {
6777

6878
getters[lowCamelPlural] = state => state
6979
getters[lowCamelSingular] = state => id => state[id] || undefined
80+
getters[`${lowCamelSingular}Related`] = (state, rootState) => (type, id) => {
81+
return utils.filterObject(rootState[type], (entity) => entity[`${singular}_id`] === id) || undefined
82+
}
7083

7184
return getters
7285
},

src/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,24 @@ export default {
2222
if (this.isObject(object)) { return Object.prototype.hasOwnProperty.call(object, property) }
2323
return false
2424
},
25+
26+
// Filters object keys with a callback testing its value
27+
filterObject (obj, test, first = false) {
28+
if (obj !== null && typeof obj === 'object') {
29+
const results = {}
30+
for (const key in obj) {
31+
if (obj.hasOwnProperty(key)) {
32+
if (test(obj[key], key)) {
33+
if (first) {
34+
return obj[key]
35+
}
36+
results[key] = obj[key]
37+
}
38+
}
39+
}
40+
return results
41+
}
42+
43+
throw new Error('The thing you try to filter is not an object.')
44+
},
2545
}

0 commit comments

Comments
 (0)