diff --git a/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.spec.ts b/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.spec.ts index 791bb6c81..218b9ea65 100644 --- a/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.spec.ts +++ b/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.spec.ts @@ -7,6 +7,7 @@ import { ProjectListHoverComponent } from './project-list-hover.component'; import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer'; import { getCustomerProjects } from '../../../customer-management/components/projects/components/store/project.selectors'; import { FilterProjectPipe } from '../../../shared/pipes'; +import { CreateEntry, UpdateActiveEntry } from '../../store/entry.actions'; describe('ProjectListHoverComponent', () => { let component: ProjectListHoverComponent; @@ -58,12 +59,37 @@ describe('ProjectListHoverComponent', () => { expect(component).toBeTruthy(); }); - it('clock-in dispatchs a new action', () => { + it('clock-in dispatchs a CreateEntry action', () => { + const entry = { + project_id: '2b87372b-3d0d-4dc0-832b-ae5863cd39e5', + start_date: new Date().toISOString(), + }; + + component.activeEntry = null; + spyOn(store, 'dispatch'); + + component.clockIn('2b87372b-3d0d-4dc0-832b-ae5863cd39e5'); + + expect(store.dispatch).toHaveBeenCalledWith(new CreateEntry(entry)); + }); + + it('clock-in dispatchs a UpdateActiveEntry action', () => { + const entry = { + id: '123', + project_id: '2b87372b-3d0d-4dc0-832b-ae5863cd39e5', + start_date: new Date().toISOString(), + }; + const updatedEntry = { + id: '123', + project_id: '123372b-3d0d-4dc0-832b-ae5863cd39e5', + }; + + component.activeEntry = entry; spyOn(store, 'dispatch'); - component.clockIn('id'); + component.clockIn('123372b-3d0d-4dc0-832b-ae5863cd39e5'); - expect(store.dispatch).toHaveBeenCalled(); + expect(store.dispatch).toHaveBeenCalledWith(new UpdateActiveEntry(updatedEntry)); }); }); diff --git a/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.ts b/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.ts index 708f63c23..341dd2a37 100644 --- a/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.ts +++ b/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.ts @@ -22,6 +22,7 @@ export class ProjectListHoverComponent implements OnInit { showButton = ''; keyword = 'name'; nameActiveProject: string; + activeEntry; constructor(private store: Store, private toastr: ToastrService) { } @@ -33,13 +34,13 @@ export class ProjectListHoverComponent implements OnInit { this.listProjects = projects; this.loadActiveTimeEntry(); }); - } private loadActiveTimeEntry() { this.store.dispatch(new entryActions.LoadActiveEntry()); const activeEntry$ = this.store.pipe(select(getActiveTimeEntry)); activeEntry$.subscribe((activeEntry) => { + this.activeEntry = activeEntry; if (activeEntry) { for (const project of this.listProjects) { if (project.id === activeEntry.project_id) { @@ -54,8 +55,13 @@ export class ProjectListHoverComponent implements OnInit { } clockIn(id: string) { - const newEntry = { project_id: id, start_date: new Date().toISOString() }; - this.store.dispatch(new entryActions.CreateEntry(newEntry)); - this.toastr.success('You just clocked-in'); + if (this.activeEntry) { + const entry = {id: this.activeEntry.id, project_id: id}; + this.store.dispatch(new entryActions.UpdateActiveEntry(entry)); + } else { + const newEntry = { project_id: id, start_date: new Date().toISOString() }; + this.store.dispatch(new entryActions.CreateEntry(newEntry)); + this.toastr.success('You just clocked-in'); + } } }