Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: #203 Update-customer-for-active-clock-ins
  • Loading branch information
macrisguncay committed May 8, 2020
commit 2b5e1a4b14c781a54726cc939506db40a1c155eb
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class ProjectListHoverComponent implements OnInit {
showButton = '';
keyword = 'name';
nameActiveProject: string;
activeEntry;

constructor(private store: Store<ProjectState>, private toastr: ToastrService) { }

Expand All @@ -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) {
Expand All @@ -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');
}
}
}