Skip to content

Commit c56231a

Browse files
committed
fix: #203 Update-customer-for-active-clock-ins
1 parent c18a02f commit c56231a

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.spec.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ProjectListHoverComponent } from './project-list-hover.component';
66
import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer';
77
import { getCustomerProjects } from '../../../customer-management/components/projects/components/store/project.selectors';
88
import { FilterProjectPipe } from '../../../shared/pipes';
9+
import { CreateEntry, UpdateActiveEntry } from '../../store/entry.actions';
910

1011
describe('ProjectListHoverComponent', () => {
1112
let component: ProjectListHoverComponent;
@@ -53,11 +54,36 @@ describe('ProjectListHoverComponent', () => {
5354
expect(component).toBeTruthy();
5455
});
5556

56-
it('clock-in dispatchs a new action', () => {
57+
it('clock-in dispatchs a CreateEntry action', () => {
58+
const entry = {
59+
project_id: '2b87372b-3d0d-4dc0-832b-ae5863cd39e5',
60+
start_date: new Date().toISOString(),
61+
};
62+
63+
component.activeEntry = null;
64+
spyOn(store, 'dispatch');
65+
66+
component.clockIn('2b87372b-3d0d-4dc0-832b-ae5863cd39e5');
67+
68+
expect(store.dispatch).toHaveBeenCalledWith(new CreateEntry(entry));
69+
});
70+
71+
it('clock-in dispatchs a UpdateActiveEntry action', () => {
72+
const entry = {
73+
id: '123',
74+
project_id: '2b87372b-3d0d-4dc0-832b-ae5863cd39e5',
75+
start_date: new Date().toISOString(),
76+
};
77+
const updatedEntry = {
78+
id: '123',
79+
project_id: '123372b-3d0d-4dc0-832b-ae5863cd39e5',
80+
};
81+
82+
component.activeEntry = entry;
5783
spyOn(store, 'dispatch');
5884

59-
component.clockIn('id');
85+
component.clockIn('123372b-3d0d-4dc0-832b-ae5863cd39e5');
6086

61-
expect(store.dispatch).toHaveBeenCalled();
87+
expect(store.dispatch).toHaveBeenCalledWith(new UpdateActiveEntry(updatedEntry));
6288
});
6389
});

src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class ProjectListHoverComponent implements OnInit {
2121
showButton = '';
2222
keyword = 'name';
2323
nameActiveProject: string;
24+
activeEntry;
2425

2526
constructor(private store: Store<ProjectState>) { }
2627

@@ -32,13 +33,13 @@ export class ProjectListHoverComponent implements OnInit {
3233
this.listProjects = projects;
3334
this.loadActiveTimeEntry();
3435
});
35-
3636
}
3737

3838
private loadActiveTimeEntry() {
3939
this.store.dispatch(new entryActions.LoadActiveEntry());
4040
const activeEntry$ = this.store.pipe(select(getActiveTimeEntry));
4141
activeEntry$.subscribe((activeEntry) => {
42+
this.activeEntry = activeEntry;
4243
if (activeEntry) {
4344
for (const project of this.listProjects) {
4445
if (project.id === activeEntry.project_id) {
@@ -53,7 +54,12 @@ export class ProjectListHoverComponent implements OnInit {
5354
}
5455

5556
clockIn(id: string) {
56-
const newEntry = { project_id: id, start_date: new Date().toISOString() };
57-
this.store.dispatch(new entryActions.CreateEntry(newEntry));
57+
if (this.activeEntry) {
58+
const entry = {id: this.activeEntry.id, project_id: id};
59+
this.store.dispatch(new entryActions.UpdateActiveEntry(entry));
60+
} else {
61+
const newEntry = { project_id: id, start_date: new Date().toISOString() };
62+
this.store.dispatch(new entryActions.CreateEntry(newEntry));
63+
}
5864
}
5965
}

0 commit comments

Comments
 (0)