mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-19 22:24:11 +00:00
millis + validation
This commit is contained in:
@@ -4,17 +4,33 @@ import DelayValue from '../../input/DelayValue';
|
|||||||
export default function EventTimes(props) {
|
export default function EventTimes(props) {
|
||||||
const { updateValues, delay, timeStart, timeEnd } = props;
|
const { updateValues, delay, timeStart, timeEnd } = props;
|
||||||
|
|
||||||
|
// TODO: how to handle midnight rollover
|
||||||
|
const handleValidate = (entry, v) => {
|
||||||
|
// if one of the fields is not set, all good
|
||||||
|
|
||||||
|
if (v == null || timeStart == null || timeEnd == null) return true;
|
||||||
|
|
||||||
|
if (entry === 'timeStart') return v < timeEnd;
|
||||||
|
else if (entry === 'timeEnd') return v > timeStart;
|
||||||
|
|
||||||
|
// shouldnt come to this
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<DelayValue delay={delay} />
|
<DelayValue delay={delay} />
|
||||||
<EditableTimer
|
<EditableTimer
|
||||||
name='timeStart'
|
name='timeStart'
|
||||||
|
validate={handleValidate}
|
||||||
updateValues={updateValues}
|
updateValues={updateValues}
|
||||||
time={timeStart}
|
time={timeStart}
|
||||||
delay={delay}
|
delay={delay}
|
||||||
/>
|
/>
|
||||||
<EditableTimer
|
<EditableTimer
|
||||||
name='timeEnd'
|
name='timeEnd'
|
||||||
|
validate={handleValidate}
|
||||||
updateValues={updateValues}
|
updateValues={updateValues}
|
||||||
time={timeEnd}
|
time={timeEnd}
|
||||||
delay={delay}
|
delay={delay}
|
||||||
|
|||||||
@@ -1,45 +1,67 @@
|
|||||||
import { Editable, EditableInput, EditablePreview } from '@chakra-ui/editable';
|
import { Editable, EditableInput, EditablePreview } from '@chakra-ui/editable';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { addAndFormat, timeToDate, dateToTime } from '../dateConfig';
|
import {
|
||||||
|
timeFormat,
|
||||||
|
stringFromMillis,
|
||||||
|
timeStringToMillis,
|
||||||
|
} from '../dateConfig';
|
||||||
|
import { showErrorToast } from '../helpers/toastManager';
|
||||||
import style from './EditableTimer.module.css';
|
import style from './EditableTimer.module.css';
|
||||||
|
|
||||||
export default function EditableTimer(props) {
|
export default function EditableTimer(props) {
|
||||||
const { name, updateValues, time, delay } = props;
|
const { name, updateValues, time, delay, validate } = props;
|
||||||
const [value, setValue] = useState('');
|
const [value, setValue] = useState('');
|
||||||
const [editing, setEditing] = useState(false);
|
|
||||||
|
|
||||||
// prepare time fields
|
// prepare time fields
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (time == null) return;
|
if (time == null) return;
|
||||||
setValue(addAndFormat(time, delay));
|
try {
|
||||||
|
setValue(stringFromMillis(time + delay, false));
|
||||||
|
} catch (error) {
|
||||||
|
showErrorToast('Error parsing date', error.text);
|
||||||
|
}
|
||||||
}, [time, delay]);
|
}, [time, delay]);
|
||||||
|
|
||||||
const handleSubmit = (submitedVal) => {
|
const validateValue = (value) => {
|
||||||
setValue(addAndFormat(time, delay));
|
const success = handleSubmit(value);
|
||||||
setEditing(false);
|
|
||||||
|
|
||||||
const newTime = timeToDate(submitedVal);
|
if (success) setValue(value);
|
||||||
|
else setValue(stringFromMillis(time + delay, false));
|
||||||
// No need to update if it hasnt changed
|
|
||||||
if (newTime === value) return;
|
|
||||||
updateValues(name, newTime);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const showNormal = () => {
|
const handleSubmit = (value) => {
|
||||||
if (!editing) setValue(dateToTime(time));
|
// Check if there is anything there
|
||||||
|
if (value === '') return false;
|
||||||
|
|
||||||
|
// Time now and time submitedVal
|
||||||
|
const original = stringFromMillis(time, false);
|
||||||
|
|
||||||
|
// check if time is different from before
|
||||||
|
if (value === original) return false;
|
||||||
|
|
||||||
|
// conver to millis object
|
||||||
|
const millis = timeStringToMillis(value, timeFormat);
|
||||||
|
|
||||||
|
// validate with parent
|
||||||
|
if (!validate(name, millis)) return false;
|
||||||
|
|
||||||
|
// update entry
|
||||||
|
updateValues(name, millis);
|
||||||
|
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleChange = (submitedVal) => {
|
const showOriginal = () => {
|
||||||
setEditing(true);
|
setValue(stringFromMillis(time, false));
|
||||||
setValue(submitedVal);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={style.time}>
|
<div className={style.time}>
|
||||||
<Editable
|
<Editable
|
||||||
onEdit={() => showNormal}
|
onFocus={() => showOriginal()}
|
||||||
onSubmit={(v) => handleSubmit(v)}
|
onEdit={() => showOriginal}
|
||||||
onChange={(v) => handleChange(v)}
|
onChange={(v) => setValue(v)}
|
||||||
|
onSubmit={(v) => validateValue(v)}
|
||||||
value={value}
|
value={value}
|
||||||
placeholder='--:--'
|
placeholder='--:--'
|
||||||
className={delay > 0 ? style.delayedEditable : style.editable}
|
className={delay > 0 ? style.delayedEditable : style.editable}
|
||||||
|
|||||||
Reference in New Issue
Block a user