@@ -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}
0 commit comments