@@ -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
@@ -11,19 +26,22 @@ const handleError = (commit, e) => {
1126 // eslint-disable-next-line
1227 commit ( ACTIONS . SET_ERROR , e . errors . _error ) ;
1328
14- return ;
29+ return Promise . reject ( e ) ;
1530 }
1631
1732 // eslint-disable-next-line
1833 commit ( ACTIONS . SET_ERROR , e . message ) ;
34+
35+ return Promise . reject ( e ) ;
1936} ;
2037
21- const ACTIONS = {
38+ export const ACTIONS = {
2239 ADD : 'ADD' ,
23- MERCURE_DELETED : 'MERCURE_DELETED' ,
24- MERCURE_MESSAGE : 'MERCURE_MESSAGE' ,
25- MERCURE_OPEN : 'MERCURE_OPEN' ,
40+ RESET_CREATE : 'RESET_CREATE' ,
41+ RESET_DELETE : 'RESET_DELETE' ,
2642 RESET_LIST : 'RESET_LIST' ,
43+ RESET_SHOW : 'RESET_SHOW' ,
44+ RESET_UPDATE : 'RESET_UPDATE' ,
2745 SET_CREATED : 'SET_CREATED' ,
2846 SET_DELETED : 'SET_DELETED' ,
2947 SET_ERROR : 'SET_ERROR' ,
@@ -61,7 +79,6 @@ export default function makeCrudModule({
6179
6280 service
6381 . del ( item )
64- . then ( response => response . json ( ) )
6582 . then ( ( ) => {
6683 commit ( ACTIONS . TOGGLE_LOADING ) ;
6784 commit ( ACTIONS . SET_DELETED , item ) ;
@@ -71,10 +88,14 @@ export default function makeCrudModule({
7188 fetchAll : ( { commit, state } , params ) => {
7289 if ( ! service ) throw new Error ( 'No service specified!' ) ;
7390
91+ commit ( ACTIONS . TOGGLE_LOADING ) ;
92+
7493 service
7594 . findAll ( { params } )
7695 . then ( response => response . json ( ) )
7796 . then ( retrieved => {
97+ commit ( ACTIONS . TOGGLE_LOADING ) ;
98+
7899 commit (
79100 ACTIONS . SET_TOTAL_ITEMS ,
80101 retrieved [ '{{{hydraPrefix}}}totalItems' ]
@@ -123,6 +144,18 @@ export default function makeCrudModule({
123144 } )
124145 . catch ( e => handleError ( commit , e ) ) ;
125146 } ,
147+ resetCreate : ( { commit } ) => {
148+ commit ( ACTIONS . RESET_CREATE ) ;
149+ } ,
150+ resetDelete : ( { commit } ) => {
151+ commit ( ACTIONS . RESET_DELETE ) ;
152+ } ,
153+ resetShow : ( { commit } ) => {
154+ commit ( ACTIONS . RESET_SHOW ) ;
155+ } ,
156+ resetUpdate : ( { commit } ) => {
157+ commit ( ACTIONS . RESET_UPDATE ) ;
158+ } ,
126159 update : ( { commit } , item ) => {
127160 commit ( ACTIONS . SET_ERROR , '' ) ;
128161 commit ( ACTIONS . TOGGLE_LOADING ) ;
@@ -150,16 +183,48 @@ export default function makeCrudModule({
150183 updateField,
151184 [ ACTIONS . ADD ] : ( state , item ) => {
152185 Vue . set ( state . byId , item [ '@id' ] , item ) ;
186+ Vue . set ( state , 'isLoading' , false ) ;
153187 if ( state . allIds . includes ( item [ '@id' ] ) ) return ;
154188 state . allIds . push ( item [ '@id' ] ) ;
155189 } ,
190+ [ ACTIONS . RESET_CREATE ] : state => {
191+ Object . assign ( state , {
192+ isLoading : false ,
193+ error : '' ,
194+ created : null ,
195+ violations : null
196+ } ) ;
197+ } ,
198+ [ ACTIONS . RESET_DELETE ] : state => {
199+ Object . assign ( state , {
200+ isLoading : false ,
201+ error : '' ,
202+ deleted : null
203+ } ) ;
204+ } ,
156205 [ ACTIONS . RESET_LIST ] : state => {
157206 Object . assign ( state , {
158207 allIds : [ ] ,
159208 byId : { } ,
209+ error : '' ,
210+ isLoading : false ,
160211 resetList : false
161212 } ) ;
162213 } ,
214+ [ ACTIONS . RESET_SHOW ] : state => {
215+ Object . assign ( state , {
216+ error : '' ,
217+ isLoading : false
218+ } ) ;
219+ } ,
220+ [ ACTIONS . RESET_UPDATE ] : state => {
221+ Object . assign ( state , {
222+ error : '' ,
223+ isLoading : false ,
224+ updated : null ,
225+ violations : null
226+ } ) ;
227+ } ,
163228 [ ACTIONS . SET_CREATED ] : ( state , created ) => {
164229 Object . assign ( state , { created } ) ;
165230 } ,
@@ -203,19 +268,6 @@ export default function makeCrudModule({
203268 }
204269 } ,
205270 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- }
271+ state : initialState
220272 } ;
221273}
0 commit comments