Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Extract logic for formatting and parsing

Klaus Zanders990812ad43a2
diff --git a/frontend/src/stimulus/controllers/dynamic/chronic-duration.controller.ts b/frontend/src/stimulus/controllers/dynamic/chronic-duration.controller.ts
index fc0d69c0429..e3d94ff3d00 100644
--- a/frontend/src/stimulus/controllers/dynamic/chronic-duration.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/chronic-duration.controller.ts
@@ -33,8 +33,8 @@ import { Controller } from '@hotwired/stimulus';
import { outputChronicDuration, parseChronicDuration } from 'core-app/shared/helpers/chronic_duration';
export default class ChronicDurationController extends Controller<HTMLInputElement> {
- private processChangeFn = () => this.onBlur();
- private keyPressedFn = (evt:KeyboardEvent) => this.onKeyPress(evt);
+ private processChangeFn = () => { this.onBlur(); };
+ private keyPressedFn = (evt:KeyboardEvent) => { this.onKeyPress(evt); };
connect() {
this.element.addEventListener('blur', this.processChangeFn);
@@ -57,7 +57,7 @@ export default class ChronicDurationController extends Controller<HTMLInputEleme
},
);
- this.element.value = outputChronicDuration(hours, { format: 'hours_only' }) || '';
+ this.element.value = outputChronicDuration(hours, { format: 'hours_only' }) ?? '';
}
private onKeyPress(evt:KeyboardEvent) {
diff --git a/frontend/src/stimulus/controllers/dynamic/time-entry.controller.ts b/frontend/src/stimulus/controllers/dynamic/time-entry.controller.ts
index 4da7656f0cf..b82dbd4e9d8 100644
--- a/frontend/src/stimulus/controllers/dynamic/time-entry.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/time-entry.controller.ts
@@ -29,13 +29,10 @@
*/
import { Controller } from '@hotwired/stimulus';
-import {
- parseChronicDuration,
- outputChronicDuration,
-} from 'core-app/shared/helpers/chronic_duration';
import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service';
import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';
import { useMeta } from 'stimulus-use';
+import { durationStringToSeconds, formattedHour } from 'core-stimulus/helpers/chronic-duration-helper';
export default class TimeEntryController extends Controller {
static targets = ['startTimeInput', 'endTimeInput', 'hoursInput', 'form'];
@@ -83,7 +80,7 @@ export default class TimeEntryController extends Controller {
if (this.oldWorkPackageId !== newValue) {
this.oldWorkPackageId = newValue;
- const url = this.formTarget.dataset.refreshFormUrl as string;
+ const url = this.formTarget.dataset.refreshFormUrl!;
const formData = new FormData(this.formTarget);
formData.delete('_method'); // remove the override method as this will submit to the wrong action
void this.turboRequests.request(url, {
@@ -100,14 +97,6 @@ export default class TimeEntryController extends Controller {
this.datesChanged(event.currentTarget as HTMLInputElement);
}
- parsedHourInput():number {
- const normalizedValue = this.hoursInputTarget.value.replace(',', '.');
- return parseChronicDuration(normalizedValue, {
- defaultUnit: 'hours',
- ignoreSecondsWhenColonSeperated: true,
- }) || 0;
- }
-
datesChanged(initiatedBy:HTMLInputElement) {
if (!this.hasStartTimeInputTarget || !this.hasEndTimeInputTarget) {
return;
@@ -132,7 +121,7 @@ export default class TimeEntryController extends Controller {
const startTimeInMinutes = parseInt(startTimeParts[0], 10) * 60 + parseInt(startTimeParts[1], 10);
const endTimeInMinutes = parseInt(endTimeParts[0], 10) * 60 + parseInt(endTimeParts[1], 10);
- let hoursInMinutes = Math.round(this.parsedHourInput() / 60);
+ let hoursInMinutes = Math.round(durationStringToSeconds(this.hoursInputTarget.value) / 60);
// We calculate the hours field if:
// - We have start & end time and no hours
@@ -152,8 +141,7 @@ export default class TimeEntryController extends Controller {
hoursInMinutes += exisitingDayGap;
- const formattedHours = outputChronicDuration(hoursInMinutes * 60, { format: 'hours_only' }) || '';
- this.hoursInputTarget.value = formattedHours;
+ this.hoursInputTarget.value = formattedHour(hoursInMinutes * 60);
} else if (startTimeInMinutes && hoursInMinutes) {
const newEndTime = (startTimeInMinutes + hoursInMinutes) % (24 * 60);
@@ -176,16 +164,12 @@ export default class TimeEntryController extends Controller {
hoursChanged() {
// Parse input through our chronic duration parser and then reformat as hours that can be nicely parsed on the
// backend
- const hours = this.parsedHourInput();
+ const duration = durationStringToSeconds(this.hoursInputTarget.value);
+ this.hoursInputTarget.value = formattedHour(duration);
- if (hours === 0) {
- this.hoursInputTarget.value = '';
- return;
+ if (duration !== 0) {
+ this.datesChanged(this.hoursInputTarget);
}
-
- this.hoursInputTarget.value = outputChronicDuration(hours, { format: 'hours_only' }) || '';
-
- this.datesChanged(this.hoursInputTarget);
}
hoursKeyEnterPress(event:KeyboardEvent) {
@@ -195,10 +179,10 @@ export default class TimeEntryController extends Controller {
}
toggleEndTimePlusCaption(startTimeInMinutes:number, hoursInMinutes:number) {
- const formControl = this.endTimeInputTarget.closest('.FormControl') as HTMLElement;
+ const formControl = this.endTimeInputTarget.closest('.FormControl')!;
formControl
.querySelectorAll('.FormControl-caption')
- .forEach((caption) => caption.remove());
+ .forEach((caption) => { caption.remove(); });
if (startTimeInMinutes + hoursInMinutes >= 24 * 60) {
const diffInDays = Math.floor((startTimeInMinutes + hoursInMinutes) / (60 * 24));
diff --git a/frontend/src/stimulus/helpers/chronic-duration-helper.ts b/frontend/src/stimulus/helpers/chronic-duration-helper.ts
new file mode 100644
index 00000000000..8f032e7fd05
--- /dev/null
+++ b/frontend/src/stimulus/helpers/chronic-duration-helper.ts
@@ -0,0 +1,50 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) the OpenProject GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version 3.
+ *
+ * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+ * Copyright (C) 2006-2013 Jean-Philippe Lang
+ * Copyright (C) 2010-2013 the ChiliProject Team
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * See COPYRIGHT and LICENSE files for more details.
+ * ++
+ */
+
+import {
+ parseChronicDuration,
+ outputChronicDuration,
+} from 'core-app/shared/helpers/chronic_duration';
+
+export function durationStringToSeconds(value:string):number {
+ const normalizedValue = value.replace(',', '.');
+ return parseChronicDuration(normalizedValue, {
+ defaultUnit: 'hours',
+ ignoreSecondsWhenColonSeperated: true,
+ }) || 0;
+ }
+
+export function formattedHour(seconds:number):string {
+ if (isNaN(seconds) || seconds <= 0) {
+ return '';
+ }
+
+return outputChronicDuration(seconds, { format: 'hours_only' }) ?? '';
+}