@@ -4,7 +4,7 @@ import { Actions, Effect, ofType } from '@ngrx/effects';
44import { Action } from '@ngrx/store' ;
55import { Observable , of } from 'rxjs' ;
66import { ToastrService } from 'ngx-toastr' ;
7- import { catchError , map , mergeMap } from 'rxjs/operators' ;
7+ import { catchError , map , mergeMap , switchMap } from 'rxjs/operators' ;
88import { EntryService } from '../services/entry.service' ;
99import * as actions from './entry.actions' ;
1010
@@ -13,6 +13,22 @@ export class EntryEffects {
1313 constructor ( private actions$ : Actions , private entryService : EntryService , private toastrService : ToastrService ) {
1414 }
1515
16+ @Effect ( )
17+ switchEntryRunning$ : Observable < Action > = this . actions$ . pipe (
18+ ofType ( actions . EntryActionTypes . SWITCH_TIME_ENTRY ) ,
19+ switchMap ( ( action : actions . SwitchTimeEntry ) =>
20+ this . entryService . stopEntryRunning ( action . idEntrySwitching ) . pipe (
21+ map ( ( ) => {
22+ return new actions . CreateEntry ( { project_id : action . idProjectSwitching , start_date : new Date ( ) . toISOString ( ) } ) ;
23+ } ) ,
24+ catchError ( ( error ) => {
25+ this . toastrService . warning ( error . error . message ) ;
26+ return of ( new actions . StopTimeEntryRunningFail ( error . error . message ) ) ;
27+ } )
28+ )
29+ )
30+ ) ;
31+
1632 @Effect ( )
1733 loadEntriesSummary$ : Observable < Action > = this . actions$ . pipe (
1834 ofType ( actions . EntryActionTypes . LOAD_ENTRIES_SUMMARY ) ,
@@ -46,7 +62,7 @@ export class EntryEffects {
4662 } else {
4763 const endDate = new Date ( activeEntry . start_date ) ;
4864 endDate . setHours ( 23 , 59 , 59 ) ;
49- return new actions . UpdateEntry ( { id : activeEntry . id , end_date : endDate . toISOString ( ) } ) ;
65+ return new actions . UpdateEntry ( { id : activeEntry . id , end_date : endDate . toISOString ( ) } ) ;
5066 }
5167 }
5268 } ) ,
0 commit comments