Check if the current time matches the selected interval

This commit is contained in:
Jose Gomez 2022-08-23 09:11:14 -04:00 committed by Gustav Hansen
parent f30fd3e812
commit 39b8ece5c9
3 changed files with 31 additions and 4 deletions

View File

@ -162,7 +162,34 @@ function Inputs({
[fieldSchema, isRequired] [fieldSchema, isRequired]
); );
const { label, description, placeholder, visible, step: metadataStep } = metadatas; const { label, description, placeholder, visible } = metadatas;
/**
* It decides whether using the default `step` accoding to its `inputType` or the one
* obtained from `metadatas`.
*
* The `metadatas.step` is returned when the `inputValue` is divisible by it or when the
* `inputValue` is empty, otherwise the default `step` is returned.
*/
const inputStep = useMemo(() => {
if (!metadatas.step || (inputType !== 'datetime' && inputType !== 'time')) {
return step;
}
if (!inputValue) {
return metadatas.step;
}
let minutes;
if (inputType === 'datetime') {
minutes = parseInt(inputValue.substr(14, 2), 10);
} else if (inputType === 'time') {
minutes = parseInt(inputValue.slice(-2), 10);
}
return minutes % metadatas.step === 0 ? metadatas.step : step;
}, [inputType, inputValue, metadatas.step, step]);
if (visible === false) { if (visible === false) {
return null; return null;
@ -242,7 +269,7 @@ function Inputs({
options={options} options={options}
placeholder={placeholder ? { id: placeholder, defaultMessage: placeholder } : null} placeholder={placeholder ? { id: placeholder, defaultMessage: placeholder } : null}
required={fieldSchema.required || false} required={fieldSchema.required || false}
step={metadataStep || step} step={inputStep}
type={inputType} type={inputType}
// validations={validations} // validations={validations}
value={inputValue} value={inputValue}

View File

@ -138,7 +138,7 @@ const ModalForm = ({ onMetaChange, onSizeChange }) => {
onChange={(value) => onMetaChange({ target: { name: 'step', value } })} onChange={(value) => onMetaChange({ target: { name: 'step', value } })}
label={formatMessage({ label={formatMessage({
id: getTrad('containers.SettingPage.editSettings.step.label'), id: getTrad('containers.SettingPage.editSettings.step.label'),
defaultMessage: 'Step', defaultMessage: 'Time interval (minutes)',
})} })}
> >
{TIME_FIELD_OPTIONS.map((value) => ( {TIME_FIELD_OPTIONS.map((value) => (

View File

@ -59,7 +59,7 @@ const createMetadasSchema = (schema) => {
.positive() .positive()
.test( .test(
'isDivisibleBy60', 'isDivisibleBy60',
'Step must be divisible by 60', 'Step must be either 1 or divisible by 60',
(value) => !value || value === 1 || (value * 24) % 60 === 0 (value) => !value || value === 1 || (value * 24) % 60 === 0
), ),
}) })