@@ -3,6 +3,21 @@ import { getField, updateField } from 'vuex-map-fields';
33import remove from 'lodash/remove' ;
44import SubmissionError from '../../error/SubmissionError' ;
55
6+ const initialState = {
7+ allIds : [ ] ,
8+ byId : { } ,
9+ created : null ,
10+ deleted : null ,
11+ error : '' ,
12+ isLoading : false ,
13+ resetList : false ,
14+ selectItems : null ,
15+ totalItems : 0 ,
16+ updated : null ,
17+ view : null ,
18+ violations : null
19+ } ;
20+
621const handleError = ( commit , e ) => {
722 commit ( ACTIONS . TOGGLE_LOADING ) ;
823
@@ -18,12 +33,13 @@ const handleError = (commit, e) => {
1833 commit ( ACTIONS . SET_ERROR , e . message ) ;
1934} ;
2035
21- const ACTIONS = {
36+ export const ACTIONS = {
2237 ADD : 'ADD' ,
23- MERCURE_DELETED : 'MERCURE_DELETED' ,
24- MERCURE_MESSAGE : 'MERCURE_MESSAGE' ,
25- MERCURE_OPEN : 'MERCURE_OPEN' ,
38+ RESET_CREATE : 'RESET_CREATE' ,
39+ RESET_DELETE : 'RESET_DELETE' ,
2640 RESET_LIST : 'RESET_LIST' ,
41+ RESET_SHOW : 'RESET_SHOW' ,
42+ RESET_UPDATE : 'RESET_UPDATE' ,
2743 SET_CREATED : 'SET_CREATED' ,
2844 SET_DELETED : 'SET_DELETED' ,
2945 SET_ERROR : 'SET_ERROR' ,
@@ -71,6 +87,8 @@ export default function makeCrudModule({
7187 fetchAll : ( { commit, state } , params ) => {
7288 if ( ! service ) throw new Error ( 'No service specified!' ) ;
7389
90+ commit ( ACTIONS . TOGGLE_LOADING ) ;
91+
7492 service
7593 . findAll ( { params } )
7694 . then ( response => response . json ( ) )
@@ -123,6 +141,18 @@ export default function makeCrudModule({
123141 } )
124142 . catch ( e => handleError ( commit , e ) ) ;
125143 } ,
144+ resetCreate : ( { commit } ) => {
145+ commit ( ACTIONS . RESET_CREATE ) ;
146+ } ,
147+ resetDelete : ( { commit } ) => {
148+ commit ( ACTIONS . RESET_DELETE ) ;
149+ } ,
150+ resetShow : ( { commit } ) => {
151+ commit ( ACTIONS . RESET_SHOW ) ;
152+ } ,
153+ resetUpdate : ( { commit } ) => {
154+ commit ( ACTIONS . RESET_UPDATE ) ;
155+ } ,
126156 update : ( { commit } , item ) => {
127157 commit ( ACTIONS . SET_ERROR , '' ) ;
128158 commit ( ACTIONS . TOGGLE_LOADING ) ;
@@ -150,16 +180,48 @@ export default function makeCrudModule({
150180 updateField,
151181 [ ACTIONS . ADD ] : ( state , item ) => {
152182 Vue . set ( state . byId , item [ '@id' ] , item ) ;
183+ Vue . set ( state , 'isLoading' , false ) ;
153184 if ( state . allIds . includes ( item [ '@id' ] ) ) return ;
154185 state . allIds . push ( item [ '@id' ] ) ;
155186 } ,
187+ [ ACTIONS . RESET_CREATE ] : state => {
188+ Object . assign ( state , {
189+ isLoading : false ,
190+ error : '' ,
191+ created : null ,
192+ violations : null
193+ } ) ;
194+ } ,
195+ [ ACTIONS . RESET_DELETE ] : state => {
196+ Object . assign ( state , {
197+ isLoading : false ,
198+ error : '' ,
199+ deleted : null
200+ } ) ;
201+ } ,
156202 [ ACTIONS . RESET_LIST ] : state => {
157203 Object . assign ( state , {
158204 allIds : [ ] ,
159205 byId : { } ,
206+ error : '' ,
207+ isLoading : false ,
160208 resetList : false
161209 } ) ;
162210 } ,
211+ [ ACTIONS . RESET_SHOW ] : state => {
212+ Object . assign ( state , {
213+ error : '' ,
214+ isLoading : false
215+ } ) ;
216+ } ,
217+ [ ACTIONS . RESET_UPDATE ] : state => {
218+ Object . assign ( state , {
219+ error : '' ,
220+ isLoading : false ,
221+ updated : null ,
222+ violations : null
223+ } ) ;
224+ } ,
163225 [ ACTIONS . SET_CREATED ] : ( state , created ) => {
164226 Object . assign ( state , { created } ) ;
165227 } ,
@@ -203,19 +265,6 @@ export default function makeCrudModule({
203265 }
204266 } ,
205267 namespaced : true ,
206- state : {
207- allIds : [ ] ,
208- byId : { } ,
209- created : null ,
210- deleted : null ,
211- error : '' ,
212- isLoading : false ,
213- resetList : false ,
214- selectItems : null ,
215- totalItems : 0 ,
216- updated : null ,
217- view : null ,
218- violations : null
219- }
268+ state : initialState
220269 } ;
221270}
0 commit comments