Skip to content

Commit 4cd0b02

Browse files
[O2B-1140] Fix time range input when granularity is minutes (#1454)
1 parent 2860239 commit 4cd0b02

4 files changed

Lines changed: 21 additions & 13 deletions

File tree

lib/public/components/common/form/inputs/DateTimeInputComponent.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,15 @@ export class DateTimeInputComponent extends StatefulComponent {
174174

175175
const minDateDayAfter = new Date(min + MILLISECONDS_IN_ONE_DAY);
176176

177-
const minDateAndTime = formatTimestampForDateTimeInput(min);
177+
const minDateAndTime = formatTimestampForDateTimeInput(min, this._seconds);
178178
const ret = {};
179179

180180
if (raw.date !== null && raw.date === minDateAndTime.date) {
181181
ret.time = minDateAndTime.time;
182182
}
183183

184184
if (raw.time !== null && raw.time < minDateAndTime.time) {
185-
ret.date = formatTimestampForDateTimeInput(minDateDayAfter.getTime()).date;
185+
ret.date = formatTimestampForDateTimeInput(minDateDayAfter.getTime(), this._seconds).date;
186186
} else {
187187
ret.date = minDateAndTime.date;
188188
}
@@ -204,14 +204,14 @@ export class DateTimeInputComponent extends StatefulComponent {
204204

205205
const maxDateDayBefore = new Date(max - MILLISECONDS_IN_ONE_DAY);
206206

207-
const maxDateAndTime = formatTimestampForDateTimeInput(max);
207+
const maxDateAndTime = formatTimestampForDateTimeInput(max, this._seconds);
208208
const ret = {};
209209

210210
if (raw.date !== null && raw.date === maxDateAndTime.date) {
211211
ret.time = maxDateAndTime.time;
212212
}
213213
if (raw.time !== null && raw.time > maxDateAndTime.time) {
214-
ret.date = formatTimestampForDateTimeInput(maxDateDayBefore.getTime()).date;
214+
ret.date = formatTimestampForDateTimeInput(maxDateDayBefore.getTime(), this._seconds).date;
215215
} else {
216216
ret.date = maxDateAndTime.date;
217217
}

lib/public/components/common/form/inputs/DateTimeInputModel.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ import { Observable } from '/js/src/index.js';
3535
export class DateTimeInputModel extends Observable {
3636
/**
3737
* Constructor
38+
* @param {object} [configuration] the model configuration
39+
* @param {boolean} [configuration.seconds=false] states if the date time input granularity is seconds (else, it is minutes)
3840
*/
39-
constructor() {
41+
constructor(configuration) {
4042
super();
4143

44+
const { seconds } = configuration || {};
45+
this._seconds = seconds;
46+
4247
/**
4348
* @type {DateTimeInputRawData}
4449
* @private
@@ -61,7 +66,7 @@ export class DateTimeInputModel extends Observable {
6166
update(raw) {
6267
this._raw = raw;
6368
try {
64-
this._value = raw.date && raw.time ? extractTimestampFromDateTimeInput(raw) : null;
69+
this._value = raw.date && raw.time ? extractTimestampFromDateTimeInput(raw, { seconds: this._seconds }) : null;
6570
} catch (_) {
6671
this._value = null;
6772
}
@@ -111,7 +116,7 @@ export class DateTimeInputModel extends Observable {
111116

112117
this._value = value;
113118
this._raw = value !== null
114-
? formatTimestampForDateTimeInput(value)
119+
? formatTimestampForDateTimeInput(value, this._seconds)
115120
: { date: '', time: '' };
116121

117122
!silent && this.notify();

lib/public/utilities/formatting/dateTimeInputFormatters.mjs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
* Returns the given date formatted in two parts, YYYY-MM-DD and HH:MM to fill in HTML date and time inputs (in the user's timezone)
1616
*
1717
* @param {number} timestamp the timestamp (ms) to format
18-
*
18+
* @param {boolean} enableSeconds states if the time input granularity is seconds (else, it is minutes)
1919
* @return {DateTimeInputRawData} the date expression to use as HTML input values
2020
*/
21-
export const formatTimestampForDateTimeInput = (timestamp) => {
21+
export const formatTimestampForDateTimeInput = (timestamp, enableSeconds) => {
2222
const date = new Date(timestamp);
2323

2424
/**
@@ -35,8 +35,11 @@ export const formatTimestampForDateTimeInput = (timestamp) => {
3535

3636
const hours = pad(date.getHours());
3737
const minutes = pad(date.getMinutes());
38-
const seconds = pad(date.getSeconds());
39-
const timeExpression = `${hours}:${minutes}:${seconds}`;
38+
let timeExpression = `${hours}:${minutes}`;
39+
if (enableSeconds) {
40+
const seconds = pad(date.getSeconds());
41+
timeExpression = `${timeExpression}:${seconds}`;
42+
}
4043

4144
return { date: dateExpression, time: timeExpression };
4245
};

test/public/eosReport/shift-leader-creation.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ module.exports = () => {
8383
const { formatTimestampForDateTimeInput } = await import('../../../lib/public/utilities/formatting/dateTimeInputFormatters.mjs');
8484

8585
const magnet1Timestamp = currentShift.start + 3600 * 4 * 1000;
86-
const { date: magnet1Date, time: magnet1Time } = formatTimestampForDateTimeInput(magnet1Timestamp);
86+
const { date: magnet1Date, time: magnet1Time } = formatTimestampForDateTimeInput(magnet1Timestamp, true);
8787

8888
const magnet2Timestamp = currentShift.start + 3600 * 2 * 1000;
89-
const { date: magnet2Date, time: magnet2Time } = formatTimestampForDateTimeInput(magnet2Timestamp);
89+
const { date: magnet2Date, time: magnet2Time } = formatTimestampForDateTimeInput(magnet2Timestamp, true);
9090

9191
const magnetEnd = formatShiftDate(currentShift.end, { time: true });
9292

0 commit comments

Comments
 (0)