Skip to content

Commit

Permalink
Check if the current time matches the selected interval
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose4gg authored and gu-stav committed Aug 24, 2022
1 parent f30fd3e commit 39b8ece
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
Expand Up @@ -162,7 +162,34 @@ function Inputs({
[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) {
return null;
Expand Down Expand Up @@ -242,7 +269,7 @@ function Inputs({
options={options}
placeholder={placeholder ? { id: placeholder, defaultMessage: placeholder } : null}
required={fieldSchema.required || false}
step={metadataStep || step}
step={inputStep}
type={inputType}
// validations={validations}
value={inputValue}
Expand Down
Expand Up @@ -138,7 +138,7 @@ const ModalForm = ({ onMetaChange, onSizeChange }) => {
onChange={(value) => onMetaChange({ target: { name: 'step', value } })}
label={formatMessage({
id: getTrad('containers.SettingPage.editSettings.step.label'),
defaultMessage: 'Step',
defaultMessage: 'Time interval (minutes)',
})}
>
{TIME_FIELD_OPTIONS.map((value) => (
Expand Down
Expand Up @@ -59,7 +59,7 @@ const createMetadasSchema = (schema) => {
.positive()
.test(
'isDivisibleBy60',
'Step must be divisible by 60',
'Step must be either 1 or divisible by 60',
(value) => !value || value === 1 || (value * 24) % 60 === 0
),
})
Expand Down

0 comments on commit 39b8ece

Please sign in to comment.