diff --git a/src/app/modules/time-clock/components/time-entries-summary/time-entries-summary.component.ts b/src/app/modules/time-clock/components/time-entries-summary/time-entries-summary.component.ts index 9150d0128..9a225d605 100644 --- a/src/app/modules/time-clock/components/time-entries-summary/time-entries-summary.component.ts +++ b/src/app/modules/time-clock/components/time-entries-summary/time-entries-summary.component.ts @@ -2,12 +2,12 @@ import { filter, takeUntil } from 'rxjs/operators'; import { SubstractDatePipe } from './../../../shared/pipes/substract-date/substract-date.pipe'; import { interval, Subscription, Subject } from 'rxjs'; import { Entry } from './../../../shared/models/entry.model'; -import { getEntriesSummary } from './../../store/entry.selectors'; import { TimeEntriesSummary } from '../../models/time.entry.summary'; import { LoadEntriesSummary, LoadActiveEntry, EntryActionTypes } from './../../store/entry.actions'; import { EntryState } from './../../store/entry.reducer'; -import { Store, select, ActionsSubject } from '@ngrx/store'; +import { Store, ActionsSubject } from '@ngrx/store'; import { Component, OnInit, OnDestroy } from '@angular/core'; +import * as moment from 'moment'; @Component({ selector: 'app-time-entries-summary', @@ -19,42 +19,40 @@ export class TimeEntriesSummaryComponent implements OnInit, OnDestroy { timeEntriesSummary: TimeEntriesSummary; currentWorkingTime: string; destroyed$ = new Subject(); - - loadActiveEntry: Subscription; - loadActiveEntryLost: Subscription; - stopEntry: Subscription; - startEntry: Subscription; + loadActiveEntry$: Subscription; + loadActiveEntryLost$: Subscription; + stopEntry$: Subscription; + startEntry$: Subscription; + timeEntriesSummary$: Subscription; timeInterval; constructor(private store: Store, private actionsSubject$: ActionsSubject) { } ngOnDestroy(): void { - this.loadActiveEntry.unsubscribe(); - this.loadActiveEntryLost.unsubscribe(); - this.stopEntry.unsubscribe(); - this.startEntry.unsubscribe(); + this.loadActiveEntry$.unsubscribe(); + this.loadActiveEntryLost$.unsubscribe(); + this.stopEntry$.unsubscribe(); + this.startEntry$.unsubscribe(); + this.timeEntriesSummary$.unsubscribe(); } ngOnInit(): void { this.store.dispatch(new LoadActiveEntry()); - - this.loadActiveEntryLost = this.actionsSubject$.pipe( + this.loadActiveEntryLost$ = this.actionsSubject$.pipe( filter((action: any) => ( action.type === EntryActionTypes.LOAD_ACTIVE_ENTRY_FAIL )) ).subscribe(() => { this.blankCurrentWorkingTime(); }); - - this.loadActiveEntry = this.actionsSubject$.pipe( + this.loadActiveEntry$ = this.actionsSubject$.pipe( filter((action: any) => ( action.type === EntryActionTypes.LOAD_ACTIVE_ENTRY_SUCCESS )) ).subscribe((action) => { this.updateCurrentWorkingHours(action.payload); }); - - this.stopEntry = this.actionsSubject$.pipe( + this.stopEntry$ = this.actionsSubject$.pipe( filter((action: any) => ( action.type === EntryActionTypes.STOP_TIME_ENTRY_RUNNING_SUCCESS )) @@ -62,8 +60,7 @@ export class TimeEntriesSummaryComponent implements OnInit, OnDestroy { this.destroyed$.next(true); this.blankCurrentWorkingTime(); }); - - this.startEntry = this.actionsSubject$.pipe( + this.startEntry$ = this.actionsSubject$.pipe( filter((action: any) => ( action.type === EntryActionTypes.CREATE_ENTRY_SUCCESS )) @@ -73,9 +70,12 @@ export class TimeEntriesSummaryComponent implements OnInit, OnDestroy { }); this.store.dispatch(new LoadEntriesSummary()); - const timeEntriesSummary$ = this.store.pipe(select(getEntriesSummary)); - timeEntriesSummary$.subscribe((response) => { - this.timeEntriesSummary = response; + this.timeEntriesSummary$ = this.actionsSubject$.pipe( + filter((action: any) => ( + action.type === EntryActionTypes.LOAD_ENTRIES_SUMMARY_SUCCESS + )) + ).subscribe((response) => { + this.timeEntriesSummary = response.payload; }); } @@ -91,7 +91,15 @@ export class TimeEntriesSummaryComponent implements OnInit, OnDestroy { this.timeInterval = interval(1000).pipe( takeUntil(this.destroyed$) ).subscribe(() => { - this.currentWorkingTime = new SubstractDatePipe().transform(new Date(), new Date(entry.start_date), true); + const endDate = new Date(); + const startDate = new Date(entry.start_date); + this.currentWorkingTime = new SubstractDatePipe().transform(endDate, startDate, true); + + const aMinuteHasElapsed = moment(startDate).diff(endDate, 'seconds') % 60 === 0 ; + if (aMinuteHasElapsed) { + this.store.dispatch(new LoadEntriesSummary()); + } + }); } }