Skip to content

Commit 05865ce

Browse files
authored
Merge pull request #421 from ioet/320-update-summaries-data
fix: #320 reload time worked summary after clocking-out
2 parents 5ef5e28 + 4070736 commit 05865ce

File tree

5 files changed

+73
-10
lines changed

5 files changed

+73
-10
lines changed

src/app/modules/shared/pipes/substract-date/substract-date.pipe.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ describe('SubstractDatePipe', () => {
1515
expect(diff).toBe('02:20');
1616
});
1717

18+
it('returns the date diff using hh:mm:ss for a diff < 1 min when displaySeconds is true', () => {
19+
const fromDate = new Date('2011-04-11T10:22:40Z');
20+
const substractDate = new Date('2011-04-11T10:20:30Z');
21+
22+
const diff = new SubstractDatePipe().transform(fromDate, substractDate, true);
23+
24+
expect(diff).toBe('00:02:10');
25+
});
26+
1827
it('returns the date diff including seconds if difference is less than a minute', () => {
1928
const fromDate = new Date('2011-04-11T10:20:40Z');
2029
const substractDate = new Date('2011-04-11T10:20:30Z');

src/app/modules/shared/pipes/substract-date/substract-date.pipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as moment from 'moment';
66
})
77
export class SubstractDatePipe implements PipeTransform {
88

9-
transform(fromDate: Date, substractDate: Date): string {
9+
transform(fromDate: Date, substractDate: Date, displaySeconds: boolean = false): string {
1010

1111
if (fromDate === null || substractDate === null ) {
1212
return '--:--';
@@ -16,7 +16,7 @@ export class SubstractDatePipe implements PipeTransform {
1616
let endDate = moment(fromDate, 'YYYY-MM-DD HH:mm:ss');
1717
let duration: moment.Duration = moment.duration(endDate.diff(startDate));
1818

19-
if (duration.asSeconds() > 60) {
19+
if (duration.asSeconds() > 60 && !displaySeconds) {
2020
endDate = endDate.add(1, 'minute').startOf('minute');
2121
duration = moment.duration(endDate.diff(startDate));
2222
return `${ this.formatTime(duration.hours())}:${this.formatTime(duration.minutes()) }`;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ 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));
94+
this.currentWorkingTime = new SubstractDatePipe().transform(new Date(), new Date(entry.start_date), true);
9595
});
9696
}
9797
}

src/app/modules/time-clock/pages/time-clock.component.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FormBuilder } from '@angular/forms';
2-
import { StopTimeEntryRunning } from './../store/entry.actions';
2+
import { StopTimeEntryRunning, EntryActionTypes, LoadEntriesSummary } from './../store/entry.actions';
33
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
44
import { HttpClientTestingModule } from '@angular/common/http/testing';
55
import { provideMockStore, MockStore } from '@ngrx/store/testing';
@@ -8,6 +8,7 @@ import { ProjectState } from '../../customer-management/components/projects/comp
88
import { ProjectListHoverComponent } from '../components';
99
import { FilterProjectPipe } from '../../shared/pipes';
1010
import { AzureAdB2CService } from '../../login/services/azure.ad.b2c.service';
11+
import { ActionsSubject } from '@ngrx/store';
1112
import { EntryFieldsComponent } from '../components/entry-fields/entry-fields.component';
1213
import { ToastrService } from 'ngx-toastr';
1314

@@ -16,10 +17,13 @@ describe('TimeClockComponent', () => {
1617
let fixture: ComponentFixture<TimeClockComponent>;
1718
let store: MockStore<ProjectState>;
1819
let azureAdB2CService: AzureAdB2CService;
20+
const actionSub: ActionsSubject = new ActionsSubject();
21+
1922
let injectedToastrService;
2023
const toastrService = {
2124
error: () => {},
2225
};
26+
2327
const state = {
2428
projects: {
2529
projects: [{ id: 'id', name: 'name', project_type_id: '' }],
@@ -52,6 +56,7 @@ describe('TimeClockComponent', () => {
5256
FormBuilder,
5357
AzureAdB2CService,
5458
provideMockStore({ initialState: state }),
59+
{ provide: ActionsSubject, useValue: actionSub },
5560
{ provide: ToastrService, useValue: toastrService },
5661
],
5762
}).compileComponents();
@@ -70,6 +75,34 @@ describe('TimeClockComponent', () => {
7075
expect(component).toBeTruthy();
7176
});
7277

78+
it('on STOP_TIME_ENTRY_RUNNING_SUCCESS summaries are reloaded', () => {
79+
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
80+
const action = {
81+
type: EntryActionTypes.STOP_TIME_ENTRY_RUNNING_SUCCESS
82+
};
83+
spyOn(store, 'dispatch');
84+
85+
actionSubject.next(action);
86+
87+
expect(store.dispatch).toHaveBeenCalledWith(new LoadEntriesSummary());
88+
});
89+
90+
it('register reloadSummaries on ngOnInit', () => {
91+
spyOn(component, 'reloadSummariesOnClockOut');
92+
93+
component.ngOnInit();
94+
95+
expect(component.reloadSummariesOnClockOut).toHaveBeenCalled();
96+
});
97+
98+
it('unsubscribe clockOutSubscription onDestroy', () => {
99+
spyOn(component.clockOutSubscription, 'unsubscribe');
100+
101+
component.ngOnDestroy();
102+
103+
expect(component.clockOutSubscription.unsubscribe).toHaveBeenCalled();
104+
});
105+
73106
it('onInit checks if isLogin and gets the userName', () => {
74107
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
75108
spyOn(azureAdB2CService, 'getName').and.returnValue('Name');

src/app/modules/time-clock/pages/time-clock.component.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { filter } from 'rxjs/operators';
12
import { getActiveTimeEntry } from './../store/entry.selectors';
2-
import { StopTimeEntryRunning } from './../store/entry.actions';
3+
import { StopTimeEntryRunning, EntryActionTypes, LoadEntriesSummary } from './../store/entry.actions';
34
import { Entry } from './../../shared/models/entry.model';
4-
import { Store, select } from '@ngrx/store';
5-
import { Component, OnInit, ViewChild } from '@angular/core';
5+
import { Store, select, ActionsSubject } from '@ngrx/store';
6+
import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
67
import { AzureAdB2CService } from '../../login/services/azure.ad.b2c.service';
78
import { Subscription } from 'rxjs';
89
import { EntryFieldsComponent } from '../components/entry-fields/entry-fields.component';
@@ -12,20 +13,26 @@ import { ToastrService } from 'ngx-toastr';
1213
templateUrl: './time-clock.component.html',
1314
styleUrls: ['./time-clock.component.scss'],
1415
})
15-
export class TimeClockComponent implements OnInit {
16+
export class TimeClockComponent implements OnInit, OnDestroy {
1617
@ViewChild(EntryFieldsComponent)
1718
entryFieldsComponent: EntryFieldsComponent;
1819
username: string;
1920
areFieldsVisible = false;
2021
activeTimeEntry: Entry;
21-
actionsSubscription: Subscription;
22+
clockOutSubscription: Subscription;
23+
2224

2325
constructor(
2426
private azureAdB2CService: AzureAdB2CService,
2527
private store: Store<Entry>,
26-
private toastrService: ToastrService
28+
private toastrService: ToastrService,
29+
private actionsSubject$: ActionsSubject
2730
) {}
2831

32+
ngOnDestroy(): void {
33+
this.clockOutSubscription.unsubscribe();
34+
}
35+
2936
ngOnInit() {
3037
this.username = this.azureAdB2CService.isLogin() ? this.azureAdB2CService.getName() : '';
3138
this.store.pipe(select(getActiveTimeEntry)).subscribe((activeTimeEntry) => {
@@ -36,6 +43,20 @@ export class TimeClockComponent implements OnInit {
3643
this.areFieldsVisible = false;
3744
}
3845
});
46+
47+
this.reloadSummariesOnClockOut();
48+
49+
}
50+
51+
reloadSummariesOnClockOut() {
52+
this.clockOutSubscription = this.actionsSubject$.pipe(
53+
filter((action) => (
54+
action.type === EntryActionTypes.STOP_TIME_ENTRY_RUNNING_SUCCESS
55+
)
56+
)
57+
).subscribe( (action) => {
58+
this.store.dispatch(new LoadEntriesSummary());
59+
});
3960
}
4061

4162
stopEntry() {

0 commit comments

Comments
 (0)