1+ import { EntryActionTypes } from './../../time-clock/store/entry.actions' ;
2+ import { filter } from 'rxjs/operators' ;
13import { ToastrService } from 'ngx-toastr' ;
24import { getActiveTimeEntry } from './../../time-clock/store/entry.selectors' ;
3- import { Component , OnInit } from '@angular/core' ;
5+ import { Component , OnInit , OnDestroy } from '@angular/core' ;
46import { Entry } from '../../shared/models' ;
57import { EntryState } from '../../time-clock/store/entry.reducer' ;
68import { allEntries } from '../../time-clock/store/entry.selectors' ;
7- import { select , Store } from '@ngrx/store' ;
9+ import { select , Store , ActionsSubject } from '@ngrx/store' ;
810import * as entryActions from '../../time-clock/store/entry.actions' ;
911import { SaveEntryEvent } from '../../shared/components/details-fields/save-entry-event' ;
12+ import { Subscription } from 'rxjs' ;
1013
1114@Component ( {
1215 selector : 'app-time-entries' ,
1316 templateUrl : './time-entries.component.html' ,
1417 styleUrls : [ './time-entries.component.scss' ] ,
1518} )
16- export class TimeEntriesComponent implements OnInit {
19+ export class TimeEntriesComponent implements OnInit , OnDestroy {
1720 entryId : string ;
1821 entry : Entry ;
1922 dataByMonth = [ ] ;
2023 activeTimeEntry : Entry ;
2124 showModal = false ;
2225 message : string ;
2326 idToDelete : string ;
27+ entriesSubscription : Subscription ;
2428
25- constructor ( private store : Store < EntryState > , private toastrService : ToastrService ) {
29+ constructor ( private store : Store < EntryState > , private toastrService : ToastrService , private actionsSubject$ : ActionsSubject ) {
30+ }
31+
32+ ngOnDestroy ( ) : void {
33+ this . entriesSubscription . unsubscribe ( ) ;
2634 }
2735
2836 ngOnInit ( ) : void {
@@ -32,6 +40,18 @@ export class TimeEntriesComponent implements OnInit {
3240 this . dataByMonth = response ;
3341 } ) ;
3442 this . loadActiveEntry ( ) ;
43+
44+ this . entriesSubscription = this . actionsSubject$ . pipe (
45+ filter ( ( action : any ) => (
46+ action . type === EntryActionTypes . CREATE_ENTRY_SUCCESS ||
47+ action . type === EntryActionTypes . UPDATE_ENTRY_SUCCESS ||
48+ action . type === EntryActionTypes . DELETE_ENTRY_SUCCESS
49+ )
50+ )
51+ ) . subscribe ( ( action ) => {
52+ this . store . dispatch ( new entryActions . LoadEntries ( new Date ( ) . getMonth ( ) + 1 ) ) ;
53+ } ) ;
54+
3555 }
3656
3757 newEntry ( ) {
@@ -58,7 +78,6 @@ export class TimeEntriesComponent implements OnInit {
5878 } else {
5979 this . doSave ( event ) ;
6080 }
61- this . store . dispatch ( new entryActions . LoadEntries ( new Date ( ) . getMonth ( ) + 1 ) ) ;
6281 }
6382
6483 doSave ( event : SaveEntryEvent ) {
0 commit comments