Permalink
Browse files

Use "findAndDispatchEntities" in Modulator

  • Loading branch information...
mtancoigne committed Sep 28, 2018
1 parent b7d61af commit 8b3f4f70afcdcea3399ef115a3e77772ca90d96b
Showing with 41 additions and 8 deletions.
  1. +21 −8 src/modulator/index.js
  2. +20 −0 src/utils.js
@@ -1,6 +1,7 @@
// We need Vue to ensure proper updates in mutations
import Vue from 'vue'
import utils from '../utils'
import findAndDispatchEntities from '../data_types'

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

actions[`load${camelPlural}`] = ({commit}) => {
actions[`dispatchAndCommit${camelSingular}`] = ({commit, dispatch}, entity) => {
findAndDispatchEntities(entity, dispatch, singular)
.then((cleanEntity) => {
// Commit the cleaned entity: all the fields where removed by findAndDispatchEntities
commit(`set_${singular}`, cleanEntity)
})
.catch((error) => {console.log('An error occurred while processing data')})
}

actions[`load${camelPlural}`] = ({dispatch}) => {
api.get(endpoint)
.then((data) => {
for (let i = 0; i < data.length; i++) {
commit(`set_${singular}`, data[i])
dispatch(`dispatchAndCommit${camelSingular}`, data[i])
}
})
.catch((error) => {console.log(`ERROR in load${camelPlural}:`, error)})
}
actions[`load${camelSingular}`] = ({commit}, id) => {
actions[`load${camelSingular}`] = ({dispatch}, id) => {
api.get(`${endpoint}/${id}`)
.then((data) => {commit(`set_${singular}`, data)})
.then((data) => {dispatch(`dispatchAndCommit${camelSingular}`, data)})
.catch((error) => {console.log(`ERROR in load${camelSingular}:`, error)})
}
if (editable) {
actions[`create${camelSingular}`] = ({commit}, payload) => {
actions[`create${camelSingular}`] = ({dispatch}, payload) => {
api.post(endpoint, payload)
.then((data) => {
// Let's assume "data" is the new article, sent back by the API
// We don't want to store the user input in our store :)
commit(`set_${singular}`, data)
dispatch(`dispatchAndCommit${camelSingular}`, data)
})
.catch((error) => {console.log(`ERROR in create${camelSingular}:`, error)})
}
actions[`update${camelSingular}`] = ({commit}, payload) => {
actions[`update${camelSingular}`] = ({dispatch}, payload) => {
api.patch(`${endpoint}/${payload.id}`, payload)
.then((data) => {
// Let's assume "data" is the updated article
commit(`set_${singular}`, data)
dispatch(`dispatchAndCommit${camelSingular}`, data)
})
.catch((error) => {console.log(`ERROR in update${camelSingular}:`, error)})
}
@@ -67,6 +77,9 @@ export default {

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

return getters
},
@@ -22,4 +22,24 @@ export default {
if (this.isObject(object)) { return Object.prototype.hasOwnProperty.call(object, property) }
return false
},

// Filters object keys with a callback testing its value
filterObject (obj, test, first = false) {
if (obj !== null && typeof obj === 'object') {
const results = {}
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (test(obj[key], key)) {
if (first) {
return obj[key]
}
results[key] = obj[key]
}
}
}
return results
}

throw new Error('The thing you try to filter is not an object.')
},
}

0 comments on commit 8b3f4f7

Please sign in to comment.