[FIX] mrp: make the timer editable

This commit makes the ongoing timer editable

Task: 2991906
X-original-commit: b2ce5adeead3255de765a6136116ffc7f36ea49e
Part-of: odoo/odoo#103872
This commit is contained in:
William Henrotin
2022-10-24 09:16:55 +02:00
parent 1ed423a181
commit 3bc9d3dabe
3 changed files with 45 additions and 16 deletions
+40 -12
View File
@@ -1,37 +1,47 @@
/** @odoo-module **/
import { registry } from "@web/core/registry";
import { formatFloatTime } from "@web/views/fields/formatters";
import { parseFloatTime } from "@web/views/fields/parsers";
import { useInputField } from "@web/views/fields/input_field_hook";
const { Component, useState, onWillUpdateProps, onWillStart, onWillDestroy } = owl;
export class MrpTimer extends Component {
setup() {
super.setup();
this.state = useState({
// duration is expected to be given in minutes
duration:
this.props.duration !== undefined
? this.props.duration
: this.props.record.data.duration,
this.props.value !== undefined ? this.props.value : this.props.record.data.duration,
});
useInputField({
getValue: () => this.durationFormatted,
refName: "numpadDecimal",
parse: (v) => parseFloatTime(v),
});
const newLocal = this;
this.ongoing =
this.props.ongoing !== undefined
? newLocal.props.ongoing
? this.props.ongoing
: this.props.record.data.is_user_working;
onWillStart(() => this._runTimer());
onWillUpdateProps((nextProps) => {
this.ongoing = nextProps.ongoing;
this._runTimer();
this.state.duration = nextProps.value;
const newOngoing =
"ongoing" in nextProps
? nextProps.ongoing
: "record" in nextProps && nextProps.record.data.is_user_working;
const rerun = !this.ongoing && newOngoing;
this.ongoing = newOngoing;
if (rerun) {
this._runTimer();
}
});
onWillDestroy(() => clearTimeout(this.timer));
}
get duration() {
// formatFloatTime except 1,5 = 1h30min but in mrp case 1,5 = 1min30
return formatFloatTime(this.state.duration / 60, { displaySeconds: true });
get durationFormatted() {
return this._formatMinutes();
}
_runTimer() {
@@ -42,6 +52,24 @@ export class MrpTimer extends Component {
}, 1000);
}
}
_formatMinutes() {
let value = this.state.duration;
if (value === false) {
return "";
}
const isNegative = value < 0;
if (isNegative) {
value = Math.abs(value);
}
let min = Math.floor(value);
// Although looking quite overkill, the following line ensures that we do
// not have float issues while still considering that 59s is 00:00.
let sec = Math.floor(Math.round((value % 1) * 60));
sec = `${sec}`.padStart(2, "0");
min = `${min}`.padStart(2, "0");
return `${isNegative ? "-" : ""}${min}:${sec}`;
}
}
MrpTimer.supportedTypes = ["float"];
+2 -1
View File
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" xml:space="preserve">
<t t-name="mrp.MrpTimer" owl="1">
<span><t t-esc="duration"/></span>
<span t-if="props.readonly" t-esc="durationFormatted"/>
<input t-else="" t-att-id="props.id" t-ref="numpadDecimal" t-att-placeholder="props.placeholder" inputmode="numeric" class="o_input" />
</t>
</templates>
+3 -3
View File
@@ -30,13 +30,13 @@ QUnit.module("Mrp", (hooks) => {
serverData,
resModel: "foo",
resId: 1,
arch: '<form><field name="duration" widget="mrp_timer"/></form>',
arch: '<form><field name="duration" widget="mrp_timer" readonly="1"/></form>',
});
assert.strictEqual(
target.querySelector(".o_field_mrp_timer").textContent,
"02:30:30",
"should contain hours and be correctly set base on minutes seconds"
"150:30",
"should not contain hours and be correctly set base on minutes seconds"
);
});
});