-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime-clock.component.ts
More file actions
90 lines (79 loc) · 2.95 KB
/
time-clock.component.ts
File metadata and controls
90 lines (79 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { ActionsSubject, select, Store } from '@ngrx/store';
import { ToastrService } from 'ngx-toastr';
import { Subscription } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { threadId } from 'worker_threads';
import { AzureAdB2CService } from '../../login/services/azure.ad.b2c.service';
import { EntryFieldsComponent } from '../components/entry-fields/entry-fields.component';
import { Entry } from './../../shared/models/entry.model';
import { EntryActionTypes, LoadEntriesSummary, StopTimeEntryRunning } from './../store/entry.actions';
import { getActiveTimeEntry } from './../store/entry.selectors';
import { LoginService } from '../../login/services/login.service';
import { environment } from 'src/environments/environment';
import { EnvironmentType } from 'src/environments/enum';
@Component({
selector: 'app-time-clock',
templateUrl: './time-clock.component.html',
styleUrls: ['./time-clock.component.scss'],
})
export class TimeClockComponent implements OnInit, OnDestroy {
@ViewChild(EntryFieldsComponent)
entryFieldsComponent: EntryFieldsComponent;
username: string;
areFieldsVisible = false;
activeTimeEntry: Entry;
clockOutSubscription: Subscription;
storeSubscription: Subscription;
isProduction = environment.production === EnvironmentType.TT_PROD_LEGACY;
constructor(
private azureAdB2CService: AzureAdB2CService,
private store: Store<Entry>,
private toastrService: ToastrService,
private actionsSubject$: ActionsSubject,
private loginService: LoginService
) {}
ngOnInit(): void {
if (this.isProduction) {
this.username = this.azureAdB2CService.isLogin() ? this.azureAdB2CService.getName() : '';
}else{
this.loginService.isLogin().subscribe(isLogin => {
this.username = isLogin ? this.loginService.getName() : '';
});
}
this.storeSubscription = this.store.pipe(select(getActiveTimeEntry)).subscribe((activeTimeEntry) => {
this.activeTimeEntry = activeTimeEntry;
if (this.activeTimeEntry) {
this.areFieldsVisible = true;
} else {
this.areFieldsVisible = false;
}
});
this.reloadSummariesOnClockOut();
}
reloadSummariesOnClockOut() {
this.clockOutSubscription = this.actionsSubject$.pipe(
filter((action) => (
action.type === EntryActionTypes.STOP_TIME_ENTRY_RUNNING_SUCCESS
)
)
).subscribe( (action) => {
this.store.dispatch(new LoadEntriesSummary());
});
}
stopEntry() {
this.store.dispatch(new StopTimeEntryRunning(this.activeTimeEntry.id));
this.areFieldsVisible = false;
}
clockOut() {
if (this.entryFieldsComponent.entryFormIsValidate()) {
this.stopEntry();
} else {
this.entryFieldsComponent.entryForm.get('activity_id').markAsTouched();
}
}
ngOnDestroy(): void {
this.clockOutSubscription.unsubscribe();
this.storeSubscription.unsubscribe();
}
}