Skip to content

Commit 0c6a3c8

Browse files
Angeluz-07enriquezrene
authored andcommitted
fix: add subscription to re-load time-entries #390
1 parent 7364605 commit 0c6a3c8

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,21 @@ describe('TimeEntriesComponent', () => {
137137
expect(component.dataByMonth.length).toEqual(1);
138138
}));
139139

140-
it('when saving time entries, the time entries should be queried', () => {
140+
it('when create time entries, the time entries should be queried', () => {
141141
const currentMonth = new Date().getMonth() + 1;
142142
const entryToSave = {
143143
entry: {
144144
project_id: 'project-id',
145-
end_date: new Date(),
146-
start_date: new Date()
145+
end_date: '2010-05-05T10:04',
146+
start_date: null
147147
}, shouldRestartEntry: false
148148
};
149149
component.activeTimeEntry = null;
150150
spyOn(store, 'dispatch');
151+
component.ngOnInit();
151152

152153
component.saveEntry(entryToSave);
153-
154+
expect(store.dispatch).toHaveBeenCalledWith(new entryActions.CreateEntry(entryToSave.entry));
154155
expect(store.dispatch).toHaveBeenCalledWith(new entryActions.LoadEntries(currentMonth));
155156
});
156157

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
1+
import { EntryActionTypes } from './../../time-clock/store/entry.actions';
2+
import { filter } from 'rxjs/operators';
13
import { ToastrService } from 'ngx-toastr';
24
import { getActiveTimeEntry } from './../../time-clock/store/entry.selectors';
3-
import { Component, OnInit } from '@angular/core';
5+
import { Component, OnInit, OnDestroy } from '@angular/core';
46
import { Entry } from '../../shared/models';
57
import { EntryState } from '../../time-clock/store/entry.reducer';
68
import { allEntries } from '../../time-clock/store/entry.selectors';
7-
import { select, Store } from '@ngrx/store';
9+
import { select, Store, ActionsSubject } from '@ngrx/store';
810
import * as entryActions from '../../time-clock/store/entry.actions';
911
import { 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

Comments
 (0)