Skip to content

Commit ddaa0aa

Browse files
authored
Merge pull request #521 from ioet/490_preload_data_on_create_entry
closes #490
2 parents 06c80b7 + 7283dfa commit ddaa0aa

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

src/app/modules/shared/components/details-fields/details-fields.component.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { allTechnologies } from '../../store/technology.selectors';
1616
import { EntryActionTypes } from './../../../time-clock/store/entry.actions';
1717
import { TechnologiesComponent } from './../technologies/technologies.component';
1818
import { DetailsFieldsComponent } from './details-fields.component';
19+
import { ProjectSelectedEvent } from './project-selected-event';
1920
import { SaveEntryEvent } from './save-entry-event';
2021

2122

@@ -316,6 +317,20 @@ describe('DetailsFieldsComponent', () => {
316317
expect(toastrServiceStub.error).toHaveBeenCalled();
317318
});
318319

320+
it('should emit projectSelected event', () => {
321+
spyOn(component.projectSelected, 'emit');
322+
const item = {
323+
id : 'id',
324+
search_field : 'TimeTracker'
325+
};
326+
component.onSelectedProject(item);
327+
328+
const data: ProjectSelectedEvent = {
329+
projectId: 'id'
330+
};
331+
expect(component.projectSelected.emit).toHaveBeenCalledWith(data);
332+
});
333+
319334
/*
320335
TODO As part of https://github.com/ioet/time-tracker-ui/issues/424 a new parameter was added to the details-field-component,
321336
and now these couple of tests are failing. A solution to this error might be generate a Test Wrapper Component. More details here:

src/app/modules/shared/components/details-fields/details-fields.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Activity, Entry, Project } from '../../models';
1616
import { TechnologyState } from '../../store/technology.reducers';
1717
import { EntryActionTypes } from './../../../time-clock/store/entry.actions';
1818
import { SaveEntryEvent } from './save-entry-event';
19+
import { ProjectSelectedEvent } from './project-selected-event';
1920

2021

2122
type Merged = TechnologyState & ProjectState & ActivityState & EntryState;
@@ -30,6 +31,7 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
3031
@Input() entryToEdit: Entry;
3132
@Input() canMarkEntryAsWIP: boolean;
3233
@Output() saveEntry = new EventEmitter<SaveEntryEvent>();
34+
@Output() projectSelected = new EventEmitter<ProjectSelectedEvent>();
3335
@ViewChild('closeModal') closeModal: ElementRef;
3436
entryForm: FormGroup;
3537
selectedTechnologies: string[] = [];
@@ -109,6 +111,7 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
109111
}
110112

111113
onSelectedProject(item) {
114+
this.projectSelected.emit({'projectId': item.id});
112115
this.entryForm.patchValue(
113116
{
114117
project_id: item.id,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface ProjectSelectedEvent {
2+
projectId: string;
3+
}

src/app/modules/time-entries/pages/time-entries.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ <h5 class="modal-title">{{ entryId ? 'Edit Entry' : 'New Entry' }}</h5>
6767
<app-details-fields
6868
[entryToEdit]="entry"
6969
(saveEntry)="saveEntry($event)"
70+
(projectSelected)="projectSelected($event)"
7071
[canMarkEntryAsWIP]='canMarkEntryAsWIP'
7172
>
7273
</app-details-fields>

src/app/modules/time-entries/pages/time-entries.component.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,23 @@ describe('TimeEntriesComponent', () => {
346346

347347
expect(store.dispatch).toHaveBeenCalledWith(new entryActions.RestartEntry(entryToSave.entry));
348348
});
349+
350+
it('should preload data of last entry when a project is selected while creating new entry ', async(() => {
351+
component.entry = null;
352+
component.entryId = null;
353+
const lastEntry = {
354+
description : 'testing is fun',
355+
technologies : [],
356+
uri : 'http://testing.is.fun',
357+
activity_id : 'sss',
358+
project_id : 'id',
359+
start_date : new Date(new Date().setHours(0, 0, 0, 0))
360+
};
361+
state.timeEntriesDataSource.data = [ lastEntry ];
362+
mockEntriesSelector = store.overrideSelector(getTimeEntriesDataSource, state.timeEntriesDataSource);
363+
364+
component.projectSelected({'projectId' : 'id'});
365+
expect(component.entry).toEqual(lastEntry);
366+
}));
367+
349368
});

src/app/modules/time-entries/pages/time-entries.component.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ActionsSubject, select, Store } from '@ngrx/store';
33
import { ToastrService } from 'ngx-toastr';
44
import { Observable, Subscription } from 'rxjs';
55
import { delay, filter } from 'rxjs/operators';
6+
import { ProjectSelectedEvent } from '../../shared/components/details-fields/project-selected-event';
67
import { SaveEntryEvent } from '../../shared/components/details-fields/save-entry-event';
78
import { Entry } from '../../shared/models';
89
import { DataSource } from '../../shared/models/data-source.model';
@@ -94,6 +95,10 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
9495
}
9596
}
9697

98+
private isNewEntry() {
99+
return this.entryId === null;
100+
}
101+
97102
saveEntry(event: SaveEntryEvent): void {
98103
if (this.activeTimeEntry !== null && this.activeTimeEntry !== undefined) {
99104
const entryDateAsIso = new Date(event.entry.start_date).toISOString();
@@ -110,6 +115,24 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
110115
}
111116
}
112117

118+
projectSelected(event: ProjectSelectedEvent): void {
119+
this.store.pipe(select(getTimeEntriesDataSource)).subscribe(ds => {
120+
const dataToUse = ds.data.find(item => item.project_id === event.projectId);
121+
if (dataToUse && this.isNewEntry()) {
122+
const startDate = new Date(new Date().setHours(0, 0, 0, 0));
123+
const entry = {
124+
description : dataToUse.description ? dataToUse.description : '',
125+
technologies : dataToUse.technologies ? dataToUse.technologies : [],
126+
uri : dataToUse.uri ? dataToUse.uri : '',
127+
activity_id : dataToUse.activity_id,
128+
project_id : dataToUse.project_id,
129+
start_date : startDate
130+
};
131+
this.entry = entry;
132+
}
133+
});
134+
}
135+
113136
doSave(event: SaveEntryEvent) {
114137
if (this.entryId) {
115138
event.entry.id = this.entryId;

0 commit comments

Comments
 (0)