@@ -143,22 +143,59 @@ const mutations = {
143143 } ,
144144 deleteUser ( state , userid ) {
145145 const userIndex = state . users . findIndex ( user => user . id === userid )
146+ this . commit ( 'updateUserCounts' , { user : state . users [ userIndex ] , actionType : 'remove' } )
146147 state . users . splice ( userIndex , 1 )
147148 } ,
148149 addUserData ( state , response ) {
149- state . users . push ( response . data . ocs . data )
150+ const user = response . data . ocs . data
151+ state . users . push ( user )
152+ this . commit ( 'updateUserCounts' , { user, actionType : 'create' } )
150153 } ,
151154 enableDisableUser ( state , { userid, enabled } ) {
152155 const user = state . users . find ( user => user . id === userid )
153156 user . enabled = enabled
154- // increment or not
155- if ( state . userCount > 0 ) {
156- state . groups . find ( group => group . id === 'disabled' ) . usercount += enabled ? - 1 : 1
157- state . userCount += enabled ? 1 : - 1
158- user . groups . forEach ( group => {
159- // Increment disabled count
160- state . groups . find ( groupSearch => groupSearch . id === group ) . disabled += enabled ? - 1 : 1
157+ // this.commit('updateUserCounts', { user, actionType: enabled ? 'enable' : 'disable' })
158+ this . commit ( 'updateUserCounts' , { user, actionType : 'update' } )
159+ } ,
160+ // update active/disabled counts, groups counts
161+ updateUserCounts ( state , { user, actionType } ) {
162+ const disabledGroup = state . groups . find ( group => group . id === 'disabled' )
163+ switch ( actionType ) {
164+ case 'update' :
165+ disabledGroup . usercount += user . enabled ? - 1 : 1 // update Disabled Users count
166+ state . userCount += user . enabled ? 1 : - 1 // update Active Users count
167+ user . groups . forEach ( userGroup => {
168+ const group = state . groups . find ( groupSearch => groupSearch . id === userGroup )
169+ group . disabled += user . enabled ? - 1 : 1 // update group disabled count
161170 } )
171+ break
172+ case 'create' :
173+ state . userCount ++ // increment Active Users count
174+
175+ user . groups . forEach ( userGroup => {
176+ state . groups
177+ . find ( groupSearch => groupSearch . id === userGroup )
178+ . usercount ++ // increment group total count
179+ } )
180+ break
181+ case 'remove' :
182+ if ( user . enabled ) {
183+ state . userCount -- // decrement Active Users count
184+ user . groups . forEach ( userGroup => {
185+ const group = state . groups . find ( groupSearch => groupSearch . id === userGroup )
186+ group . usercount -- // decrement group total count
187+ } )
188+ } else {
189+ disabledGroup . usercount -- // decrement Disabled Users count
190+ user . groups . forEach ( userGroup => {
191+ const group = state . groups . find ( groupSearch => groupSearch . id === userGroup )
192+ group . disabled -- // decrement group disabled count
193+ } )
194+ }
195+ break
196+ default :
197+ console . error ( `Unknown action type in updateUserCounts: ${ actionType } ` )
198+ // not throwing error to interupt execution as this is not fatal
162199 }
163200 } ,
164201 setUserData ( state , { userid, key, value } ) {
0 commit comments