diff --git a/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts b/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts index 8d744c848..1f343cc70 100644 --- a/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts +++ b/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts @@ -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'; @@ -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: diff --git a/src/app/modules/shared/components/details-fields/details-fields.component.ts b/src/app/modules/shared/components/details-fields/details-fields.component.ts index cc2269ccc..65179cfd8 100644 --- a/src/app/modules/shared/components/details-fields/details-fields.component.ts +++ b/src/app/modules/shared/components/details-fields/details-fields.component.ts @@ -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; @@ -30,6 +31,7 @@ export class DetailsFieldsComponent implements OnChanges, OnInit { @Input() entryToEdit: Entry; @Input() canMarkEntryAsWIP: boolean; @Output() saveEntry = new EventEmitter(); + @Output() projectSelected = new EventEmitter(); @ViewChild('closeModal') closeModal: ElementRef; entryForm: FormGroup; selectedTechnologies: string[] = []; @@ -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, diff --git a/src/app/modules/shared/components/details-fields/project-selected-event.ts b/src/app/modules/shared/components/details-fields/project-selected-event.ts new file mode 100644 index 000000000..ada3ffa03 --- /dev/null +++ b/src/app/modules/shared/components/details-fields/project-selected-event.ts @@ -0,0 +1,3 @@ +export interface ProjectSelectedEvent { + projectId: string; +} diff --git a/src/app/modules/time-entries/pages/time-entries.component.html b/src/app/modules/time-entries/pages/time-entries.component.html index da0553fcf..2fecfd3ea 100644 --- a/src/app/modules/time-entries/pages/time-entries.component.html +++ b/src/app/modules/time-entries/pages/time-entries.component.html @@ -67,6 +67,7 @@ diff --git a/src/app/modules/time-entries/pages/time-entries.component.spec.ts b/src/app/modules/time-entries/pages/time-entries.component.spec.ts index b24466d23..6e4cc0d20 100644 --- a/src/app/modules/time-entries/pages/time-entries.component.spec.ts +++ b/src/app/modules/time-entries/pages/time-entries.component.spec.ts @@ -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); + })); + }); diff --git a/src/app/modules/time-entries/pages/time-entries.component.ts b/src/app/modules/time-entries/pages/time-entries.component.ts index cb8c984ca..3ab5eaf51 100644 --- a/src/app/modules/time-entries/pages/time-entries.component.ts +++ b/src/app/modules/time-entries/pages/time-entries.component.ts @@ -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'; @@ -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(); @@ -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;