Skip to content

Commit 9e57632

Browse files
author
Guido Quezada
committed
fix: #566 Datepicker for departure date in entry form
1 parent 4aa0c14 commit 9e57632

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,6 @@
8686
/>
8787
</div>
8888

89-
<label class="col-12 col-sm-2" *ngIf="!goingToWorkOnThis">Date out:</label>
90-
<div class="col-12 col-sm-4" *ngIf="!goingToWorkOnThis">
91-
<input
92-
formControlName="departure_date"
93-
id="departure_date"
94-
type="date"
95-
class="form-control"
96-
aria-label="Small"
97-
aria-describedby="inputGroup-sizing-sm"
98-
[class.is-invalid]="departure_date.invalid && departure_date.touched"
99-
required
100-
/>
101-
</div>
102-
</div>
103-
104-
105-
<div class="form-group row">
10689
<label class="col-12 col-sm-2">Time in:</label>
10790
<div class="col-12 col-sm-4">
10891
<input
@@ -122,11 +105,27 @@
122105
aria-describedby="inputGroup-sizing-sm"
123106
/>
124107
</div>
108+
</div>
125109

126-
<label class="col-12 col-sm-2" *ngIf="!goingToWorkOnThis">Time out:</label>
127-
<div class="col-12 col-sm-4" *ngIf="!goingToWorkOnThis">
110+
111+
<div class="form-group row" *ngIf="!goingToWorkOnThis">
112+
<label class="col-12 col-sm-2">Date out:</label>
113+
<div class="col-12 col-sm-4">
114+
<input
115+
formControlName="departure_date"
116+
id="departure_date"
117+
type="date"
118+
class="form-control"
119+
aria-label="Small"
120+
aria-describedby="inputGroup-sizing-sm"
121+
[class.is-invalid]="departure_date.invalid && departure_date.touched"
122+
required
123+
/>
124+
</div>
125+
126+
<label class="col-12 col-sm-2">Time out:</label>
127+
<div class="col-12 col-sm-4">
128128
<input
129-
*ngIf="!goingToWorkOnThis"
130129
[clearIfNotMatch]="true"
131130
[showMaskTyped]="true"
132131
[dropSpecialCharacters]="false"

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,28 @@ describe('DetailsFieldsComponent', () => {
321321
expect(toastrServiceStub.error).toHaveBeenCalled();
322322
});
323323

324+
it('when entry_date is in the future and departure_date is OK then throws an error', () => {
325+
spyOn(toastrServiceStub, 'error');
326+
327+
const futureDate = moment().add(1, 'days').format('YYYY-MM-DD');
328+
const currentDate = moment().format('YYYY-MM-DD');
329+
component.entryForm.setValue({ ...formValues, entry_date: futureDate, departure_date: currentDate });
330+
component.onSubmit();
331+
332+
expect(toastrServiceStub.error).toHaveBeenCalled();
333+
});
334+
335+
it('when entry_date is OK and departure_date is in the future then throws an error future', () => {
336+
spyOn(toastrServiceStub, 'error');
337+
338+
const futureDate = moment().add(1, 'days').format('YYYY-MM-DD');
339+
const currentDate = moment().format('YYYY-MM-DD');
340+
component.entryForm.setValue({ ...formValues, entry_date: currentDate, departure_date: futureDate });
341+
component.onSubmit();
342+
343+
expect(toastrServiceStub.error).toHaveBeenCalled();
344+
});
345+
324346
it('should emit projectSelected event', () => {
325347
spyOn(component.projectSelected, 'emit');
326348
const item = {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { TechnologyState } from '../../store/technology.reducers';
1717
import { EntryActionTypes } from './../../../time-clock/store/entry.actions';
1818
import { SaveEntryEvent } from './save-entry-event';
1919
import { ProjectSelectedEvent } from './project-selected-event';
20+
import { get } from 'lodash';
2021

2122

2223
type Merged = TechnologyState & ProjectState & ActivityState & EntryState;
@@ -130,10 +131,10 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
130131
project_id: this.entryToEdit.project_id,
131132
activity_id: this.entryToEdit.activity_id,
132133
description: this.entryToEdit.description,
133-
entry_date: this.entryToEdit.start_date ? formatDate(this.entryToEdit.start_date, 'yyyy-MM-dd', 'en') : '',
134-
departure_date: formatDate(this.entryToEdit.end_date ? this.entryToEdit.end_date : new Date(), 'yyyy-MM-dd', 'en'),
135-
start_hour: this.entryToEdit.start_date ? formatDate(this.entryToEdit.start_date, 'HH:mm:ss', 'en') : '00:00:00',
136-
end_hour: this.entryToEdit.end_date ? formatDate(this.entryToEdit.end_date, 'HH:mm:ss', 'en') : '00:00:00',
134+
entry_date: formatDate(get(this.entryToEdit, 'start_date', '') , 'yyyy-MM-dd', 'en'),
135+
departure_date: formatDate(get(this.entryToEdit, 'end_date'), 'yyyy-MM-dd', 'en'),
136+
start_hour: formatDate(get(this.entryToEdit, 'start_date', '00:00:00'), 'HH:mm:ss', 'en'),
137+
end_hour: formatDate(get(this.entryToEdit, 'end_date', '00:00:00'), 'HH:mm:ss', 'en'),
137138
uri: this.entryToEdit.uri,
138139
technology: '',
139140
});
@@ -200,7 +201,6 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
200201
this.toastrService.warning('Make sure to select a project and activity');
201202
return;
202203
}
203-
// start&end date same for now
204204
const entryDate = this.entryForm.value.entry_date;
205205
const departureDate = this.entryForm.value.departure_date;
206206
const entry = {
@@ -217,7 +217,8 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
217217
delete entry.end_date;
218218
}
219219
const isEntryDateInTheFuture = moment(entryDate).isAfter(moment());
220-
if (isEntryDateInTheFuture) {
220+
const isDepartureDateInTheFuture = moment(departureDate).isAfter(moment());
221+
if (isEntryDateInTheFuture || isDepartureDateInTheFuture) {
221222
this.toastrService.error('You cannot start a time-entry in the future');
222223
return;
223224
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ describe('TimeEntriesComponent', () => {
356356
uri : 'http://testing.is.fun',
357357
activity_id : 'sss',
358358
project_id : 'id',
359-
start_date : new Date(new Date().setHours(0, 0, 0, 0))
359+
start_date : new Date(new Date().setHours(0, 0, 0, 0)),
360+
end_date : new Date(new Date().setHours(0, 0, 0, 0))
360361
};
361362
state.timeEntriesDataSource.data = [ lastEntry ];
362363
mockEntriesSelector = store.overrideSelector(getTimeEntriesDataSource, state.timeEntriesDataSource);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
126126
uri : dataToUse.uri ? dataToUse.uri : '',
127127
activity_id : dataToUse.activity_id,
128128
project_id : dataToUse.project_id,
129-
start_date : startDate
129+
start_date : startDate,
130+
end_date : startDate
130131
};
131132
this.entry = entry;
132133
}

0 commit comments

Comments
 (0)