11// We need Vue to ensure proper updates in mutations
22import Vue from 'vue'
33import utils from '../utils'
4+ import findAndDispatchEntities from '../data_types'
45
56export 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 } ,
0 commit comments