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
feat: preload data on create entry #490
  • Loading branch information
Angeluz-07 committed Oct 1, 2020
commit 7283dfa063de8a3487a7852414f6d588d093ec9a
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { allTechnologies } from '../../store/technology.selectors';
import { EntryActionTypes } from './../../../time-clock/store/entry.actions';
import { TechnologiesComponent } from './../technologies/technologies.component';
import { DetailsFieldsComponent } from './details-fields.component';
import { ProjectSelectedEvent } from './project-selected-event';
import { SaveEntryEvent } from './save-entry-event';


Expand Down Expand Up @@ -316,6 +317,20 @@ describe('DetailsFieldsComponent', () => {
expect(toastrServiceStub.error).toHaveBeenCalled();
});

it('should emit projectSelected event', () => {
spyOn(component.projectSelected, 'emit');
const item = {
id : 'id',
search_field : 'TimeTracker'
};
component.onSelectedProject(item);

const data: ProjectSelectedEvent = {
projectId: 'id'
};
expect(component.projectSelected.emit).toHaveBeenCalledWith(data);
});

/*
TODO As part of https://github.com/ioet/time-tracker-ui/issues/424 a new parameter was added to the details-field-component,
and now these couple of tests are failing. A solution to this error might be generate a Test Wrapper Component. More details here:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Activity, Entry, Project } from '../../models';
import { TechnologyState } from '../../store/technology.reducers';
import { EntryActionTypes } from './../../../time-clock/store/entry.actions';
import { SaveEntryEvent } from './save-entry-event';
import { ProjectSelectedEvent } from './project-selected-event';


type Merged = TechnologyState & ProjectState & ActivityState & EntryState;
Expand All @@ -30,6 +31,7 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
@Input() entryToEdit: Entry;
@Input() canMarkEntryAsWIP: boolean;
@Output() saveEntry = new EventEmitter<SaveEntryEvent>();
@Output() projectSelected = new EventEmitter<ProjectSelectedEvent>();
@ViewChild('closeModal') closeModal: ElementRef;
entryForm: FormGroup;
selectedTechnologies: string[] = [];
Expand Down Expand Up @@ -109,6 +111,7 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
}

onSelectedProject(item) {
this.projectSelected.emit({'projectId': item.id});
this.entryForm.patchValue(
{
project_id: item.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ProjectSelectedEvent {
projectId: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ <h5 class="modal-title">{{ entryId ? 'Edit Entry' : 'New Entry' }}</h5>
<app-details-fields
[entryToEdit]="entry"
(saveEntry)="saveEntry($event)"
(projectSelected)="projectSelected($event)"
[canMarkEntryAsWIP]='canMarkEntryAsWIP'
>
</app-details-fields>
Expand Down
19 changes: 19 additions & 0 deletions src/app/modules/time-entries/pages/time-entries.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,23 @@ describe('TimeEntriesComponent', () => {

expect(store.dispatch).toHaveBeenCalledWith(new entryActions.RestartEntry(entryToSave.entry));
});

it('should preload data of last entry when a project is selected while creating new entry ', async(() => {
component.entry = null;
component.entryId = null;
const lastEntry = {
description : 'testing is fun',
technologies : [],
uri : 'http://testing.is.fun',
activity_id : 'sss',
project_id : 'id',
start_date : new Date(new Date().setHours(0, 0, 0, 0))
};
state.timeEntriesDataSource.data = [ lastEntry ];
mockEntriesSelector = store.overrideSelector(getTimeEntriesDataSource, state.timeEntriesDataSource);

component.projectSelected({'projectId' : 'id'});
expect(component.entry).toEqual(lastEntry);
}));

});
23 changes: 23 additions & 0 deletions src/app/modules/time-entries/pages/time-entries.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ActionsSubject, select, Store } from '@ngrx/store';
import { ToastrService } from 'ngx-toastr';
import { Observable, Subscription } from 'rxjs';
import { delay, filter } from 'rxjs/operators';
import { ProjectSelectedEvent } from '../../shared/components/details-fields/project-selected-event';
import { SaveEntryEvent } from '../../shared/components/details-fields/save-entry-event';
import { Entry } from '../../shared/models';
import { DataSource } from '../../shared/models/data-source.model';
Expand Down Expand Up @@ -94,6 +95,10 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
}
}

private isNewEntry() {
return this.entryId === null;
}

saveEntry(event: SaveEntryEvent): void {
if (this.activeTimeEntry !== null && this.activeTimeEntry !== undefined) {
const entryDateAsIso = new Date(event.entry.start_date).toISOString();
Expand All @@ -110,6 +115,24 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
}
}

projectSelected(event: ProjectSelectedEvent): void {
this.store.pipe(select(getTimeEntriesDataSource)).subscribe(ds => {
const dataToUse = ds.data.find(item => item.project_id === event.projectId);
if (dataToUse && this.isNewEntry()) {
const startDate = new Date(new Date().setHours(0, 0, 0, 0));
const entry = {
description : dataToUse.description ? dataToUse.description : '',
technologies : dataToUse.technologies ? dataToUse.technologies : [],
uri : dataToUse.uri ? dataToUse.uri : '',
activity_id : dataToUse.activity_id,
project_id : dataToUse.project_id,
start_date : startDate
};
this.entry = entry;
}
});
}

doSave(event: SaveEntryEvent) {
if (this.entryId) {
event.entry.id = this.entryId;
Expand Down