mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-03 14:37:58 +00:00
2fa397548a
* feat/navigate upgrade react router * feat/navigate migrate to react router 6 * feat/navigate style modal * feat/navigate create endpoints for app settings * feat/navigate protect editor with pin * feat/navigate apply dynamic routing draft * feat/navigate upgrade relevant packages * feat/navigate restructure directory and add tests * feat/navigate test aliases validation * feat/navigate validate aliases before sending * feat/navigate create data endpoint * feat/navigate config: prettier * feat/navigate invalidate empty strings * feat/navigate create endpoints * feat/navigate parse on import * feat/navigate navigate to alias * feat/navigate user help and sample data * feat/navigate refact aliases modal * feat/navigate refact settings style * feat/navigate update sample db * feat/navigate link is relative to hostname * feat/navigate navigate to first match * feat/navigate update readme and version bump * feat/navigate config: create shared module * feat/navigate config: cheat module install * feat/navigate fix tests * feat/navigate run tests in pull request * Update ontime_cy.yml
70 lines
2.0 KiB
React
70 lines
2.0 KiB
React
import { Editable, EditableInput, EditablePreview } from '@chakra-ui/editable';
|
|
import { useContext, useEffect, useState } from 'react';
|
|
import {
|
|
isTimeString,
|
|
timeStringToMillis,
|
|
} from '../utils/dateConfig';
|
|
import { stringFromMillis } from 'ontime-utils/time';
|
|
import style from './EditableTimer.module.css';
|
|
import { LoggingContext } from '../../app/context/LoggingContext';
|
|
|
|
export default function EditableTimer(props) {
|
|
const { name, actionHandler, time, delay, validate } = props;
|
|
const { emitError } = useContext(LoggingContext);
|
|
const [value, setValue] = useState('');
|
|
|
|
// prepare time fields
|
|
useEffect(() => {
|
|
if (time == null) return;
|
|
try {
|
|
setValue(stringFromMillis(time + delay));
|
|
} catch (error) {
|
|
emitError(`Unable to parse date: ${error.text}`);
|
|
}
|
|
}, [time, delay, emitError]);
|
|
|
|
const validateValue = (value) => {
|
|
const success = handleSubmit(value);
|
|
if (success) setValue(value);
|
|
else setValue(stringFromMillis(time + delay, true));
|
|
};
|
|
|
|
const handleSubmit = (value) => {
|
|
// Check if there is anything there
|
|
if (value === '') return false;
|
|
|
|
// check if its valid time string
|
|
if (!isTimeString(value)) return false;
|
|
|
|
// convert entered value to milliseconds
|
|
const newValMillis = timeStringToMillis(value);
|
|
|
|
// Time now and time submitedVal
|
|
const originalMillis = time + delay;
|
|
|
|
// check if time is different from before
|
|
if (newValMillis === originalMillis) return false;
|
|
|
|
// validate with parent
|
|
if (!validate(name, newValMillis)) return false;
|
|
|
|
// update entry
|
|
actionHandler('update', { field: name, value: newValMillis });
|
|
|
|
return true;
|
|
};
|
|
|
|
return (
|
|
<Editable
|
|
onChange={(v) => setValue(v)}
|
|
onSubmit={(v) => validateValue(v)}
|
|
onCancel={() => setValue(stringFromMillis(time + delay, true))}
|
|
value={value}
|
|
className={delay > 0 ? style.delayedEditable : style.editable}
|
|
>
|
|
<EditablePreview />
|
|
<EditableInput type='text' placeholder='--:--:--' />
|
|
</Editable>
|
|
);
|
|
}
|