Fix Duration widget error-message logic

- It didn't remove the error msg when the input is cleared.
- It didn't update to the latest error msg if one already existed.
This commit is contained in:
infinite-persistence 2021-09-07 16:03:05 +08:00
parent 6a5ea5d3c6
commit 124aa90525
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0

View file

@ -95,16 +95,16 @@ export default function ModalBlockChannel(props: Props) {
// 'timeoutInput' to 'timeoutSec' conversion. // 'timeoutInput' to 'timeoutSec' conversion.
React.useEffect(() => { React.useEffect(() => {
const setInvalid = (errMsg: string) => { const handleInvalidInput = (errMsg: string) => {
if (timeoutSec !== -1) { if (timeoutSec !== -1) {
setTimeoutSec(-1); setTimeoutSec(-1);
} }
if (!timeoutInputErr) { if (timeoutInputErr !== errMsg) {
setTimeoutInputErr(errMsg); setTimeoutInputErr(errMsg);
} }
}; };
const setValid = (seconds) => { const handleValidInput = (seconds) => {
if (seconds !== timeoutSec) { if (seconds !== timeoutSec) {
setTimeoutSec(seconds); setTimeoutSec(seconds);
} }
@ -113,17 +113,22 @@ export default function ModalBlockChannel(props: Props) {
} }
}; };
if (!timeoutInput) {
handleValidInput(-1); // Reset
return;
}
const ONE_HUNDRED_YEARS_IN_SECONDS = 3154000000; const ONE_HUNDRED_YEARS_IN_SECONDS = 3154000000;
const seconds = parseDuration(timeoutInput, 's'); const seconds = parseDuration(timeoutInput, 's');
if (Number.isInteger(seconds) && seconds > 0) { if (Number.isInteger(seconds) && seconds > 0) {
if (seconds > ONE_HUNDRED_YEARS_IN_SECONDS) { if (seconds > ONE_HUNDRED_YEARS_IN_SECONDS) {
setInvalid(__('Wow, banned for more than 100 years?')); handleInvalidInput(__('Wow, banned for more than 100 years?'));
} else { } else {
setValid(seconds); handleValidInput(seconds);
} }
} else { } else {
setInvalid(__('Invalid duration.')); handleInvalidInput(__('Invalid duration.'));
} }
}, [timeoutInput, timeoutInputErr, timeoutSec]); }, [timeoutInput, timeoutInputErr, timeoutSec]);