Skip to content

Commit 30c4a18

Browse files
authored
Merge pull request #440 from ioet/406-update-summary-data
fix: update summary each minute closes #440
2 parents 08fd08b + 6eeb799 commit 30c4a18

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/app/modules/time-clock/components/time-entries-summary/time-entries-summary.component.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { filter, takeUntil } from 'rxjs/operators';
22
import { SubstractDatePipe } from './../../../shared/pipes/substract-date/substract-date.pipe';
33
import { interval, Subscription, Subject } from 'rxjs';
44
import { Entry } from './../../../shared/models/entry.model';
5-
import { getEntriesSummary } from './../../store/entry.selectors';
65
import { TimeEntriesSummary } from '../../models/time.entry.summary';
76
import { LoadEntriesSummary, LoadActiveEntry, EntryActionTypes } from './../../store/entry.actions';
87
import { EntryState } from './../../store/entry.reducer';
9-
import { Store, select, ActionsSubject } from '@ngrx/store';
8+
import { Store, ActionsSubject } from '@ngrx/store';
109
import { Component, OnInit, OnDestroy } from '@angular/core';
10+
import * as moment from 'moment';
1111

1212
@Component({
1313
selector: 'app-time-entries-summary',
@@ -19,51 +19,48 @@ export class TimeEntriesSummaryComponent implements OnInit, OnDestroy {
1919
timeEntriesSummary: TimeEntriesSummary;
2020
currentWorkingTime: string;
2121
destroyed$ = new Subject<boolean>();
22-
23-
loadActiveEntry: Subscription;
24-
loadActiveEntryLost: Subscription;
25-
stopEntry: Subscription;
26-
startEntry: Subscription;
22+
loadActiveEntry$: Subscription;
23+
loadActiveEntryLost$: Subscription;
24+
stopEntry$: Subscription;
25+
startEntry$: Subscription;
26+
timeEntriesSummary$: Subscription;
2727
timeInterval;
2828

2929
constructor(private store: Store<EntryState>, private actionsSubject$: ActionsSubject) { }
3030

3131
ngOnDestroy(): void {
32-
this.loadActiveEntry.unsubscribe();
33-
this.loadActiveEntryLost.unsubscribe();
34-
this.stopEntry.unsubscribe();
35-
this.startEntry.unsubscribe();
32+
this.loadActiveEntry$.unsubscribe();
33+
this.loadActiveEntryLost$.unsubscribe();
34+
this.stopEntry$.unsubscribe();
35+
this.startEntry$.unsubscribe();
36+
this.timeEntriesSummary$.unsubscribe();
3637
}
3738

3839
ngOnInit(): void {
3940
this.store.dispatch(new LoadActiveEntry());
40-
41-
this.loadActiveEntryLost = this.actionsSubject$.pipe(
41+
this.loadActiveEntryLost$ = this.actionsSubject$.pipe(
4242
filter((action: any) => (
4343
action.type === EntryActionTypes.LOAD_ACTIVE_ENTRY_FAIL
4444
))
4545
).subscribe(() => {
4646
this.blankCurrentWorkingTime();
4747
});
48-
49-
this.loadActiveEntry = this.actionsSubject$.pipe(
48+
this.loadActiveEntry$ = this.actionsSubject$.pipe(
5049
filter((action: any) => (
5150
action.type === EntryActionTypes.LOAD_ACTIVE_ENTRY_SUCCESS
5251
))
5352
).subscribe((action) => {
5453
this.updateCurrentWorkingHours(action.payload);
5554
});
56-
57-
this.stopEntry = this.actionsSubject$.pipe(
55+
this.stopEntry$ = this.actionsSubject$.pipe(
5856
filter((action: any) => (
5957
action.type === EntryActionTypes.STOP_TIME_ENTRY_RUNNING_SUCCESS
6058
))
6159
).subscribe(() => {
6260
this.destroyed$.next(true);
6361
this.blankCurrentWorkingTime();
6462
});
65-
66-
this.startEntry = this.actionsSubject$.pipe(
63+
this.startEntry$ = this.actionsSubject$.pipe(
6764
filter((action: any) => (
6865
action.type === EntryActionTypes.CREATE_ENTRY_SUCCESS
6966
))
@@ -73,9 +70,12 @@ export class TimeEntriesSummaryComponent implements OnInit, OnDestroy {
7370
});
7471

7572
this.store.dispatch(new LoadEntriesSummary());
76-
const timeEntriesSummary$ = this.store.pipe(select(getEntriesSummary));
77-
timeEntriesSummary$.subscribe((response) => {
78-
this.timeEntriesSummary = response;
73+
this.timeEntriesSummary$ = this.actionsSubject$.pipe(
74+
filter((action: any) => (
75+
action.type === EntryActionTypes.LOAD_ENTRIES_SUMMARY_SUCCESS
76+
))
77+
).subscribe((response) => {
78+
this.timeEntriesSummary = response.payload;
7979
});
8080
}
8181

@@ -91,7 +91,15 @@ export class TimeEntriesSummaryComponent implements OnInit, OnDestroy {
9191
this.timeInterval = interval(1000).pipe(
9292
takeUntil(this.destroyed$)
9393
).subscribe(() => {
94-
this.currentWorkingTime = new SubstractDatePipe().transform(new Date(), new Date(entry.start_date), true);
94+
const endDate = new Date();
95+
const startDate = new Date(entry.start_date);
96+
this.currentWorkingTime = new SubstractDatePipe().transform(endDate, startDate, true);
97+
98+
const aMinuteHasElapsed = moment(startDate).diff(endDate, 'seconds') % 60 === 0 ;
99+
if (aMinuteHasElapsed) {
100+
this.store.dispatch(new LoadEntriesSummary());
101+
}
102+
95103
});
96104
}
97105
}

0 commit comments

Comments
 (0)