2020-08-20 15:35:46 +05:00
|
|
|
import { dequal } from 'dequal';
|
2019-10-18 08:06:34 +05:00
|
|
|
import React, { useCallback, useEffect, useRef } from 'react';
|
2019-08-31 04:07:25 +05:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { Button, Form } from 'semantic-ui-react';
|
2019-11-15 03:45:59 +05:00
|
|
|
import { useToggle } from '../../lib/hooks';
|
2019-08-31 04:07:25 +05:00
|
|
|
import { Input, Popup } from '../../lib/custom-ui';
|
|
|
|
|
2019-11-15 03:45:59 +05:00
|
|
|
import { useForm } from '../../hooks';
|
2023-02-27 19:09:51 +01:00
|
|
|
import {
|
|
|
|
createStopwatch,
|
|
|
|
getStopwatchParts,
|
|
|
|
startStopwatch,
|
|
|
|
stopStopwatch,
|
|
|
|
updateStopwatch,
|
|
|
|
} from '../../utils/stopwatch';
|
|
|
|
|
|
|
|
import styles from './StopwatchEditStep.module.scss';
|
|
|
|
|
|
|
|
const createData = (stopwatch) => {
|
|
|
|
if (!stopwatch) {
|
2019-08-31 04:07:25 +05:00
|
|
|
return {
|
|
|
|
hours: '0',
|
|
|
|
minutes: '0',
|
|
|
|
seconds: '0',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:09:51 +01:00
|
|
|
const { hours, minutes, seconds } = getStopwatchParts(stopwatch);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
return {
|
|
|
|
hours: `${hours}`,
|
|
|
|
minutes: `${minutes}`,
|
|
|
|
seconds: `${seconds}`,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-02-27 19:09:51 +01:00
|
|
|
const StopwatchEditStep = React.memo(({ defaultValue, onUpdate, onBack, onClose }) => {
|
2019-08-31 04:07:25 +05:00
|
|
|
const [t] = useTranslation();
|
|
|
|
const [data, handleFieldChange, setData] = useForm(() => createData(defaultValue));
|
2022-06-20 18:27:39 +02:00
|
|
|
const [isEditing, toggleEditing] = useToggle();
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
const hoursField = useRef(null);
|
|
|
|
const minutesField = useRef(null);
|
|
|
|
const secondsField = useRef(null);
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
const handleStartClick = useCallback(() => {
|
2023-02-27 19:09:51 +01:00
|
|
|
onUpdate(startStopwatch(defaultValue));
|
2019-08-31 04:07:25 +05:00
|
|
|
onClose();
|
|
|
|
}, [defaultValue, onUpdate, onClose]);
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
const handleStopClick = useCallback(() => {
|
2023-02-27 19:09:51 +01:00
|
|
|
onUpdate(stopStopwatch(defaultValue));
|
2019-08-31 04:07:25 +05:00
|
|
|
}, [defaultValue, onUpdate]);
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
const handleClearClick = useCallback(() => {
|
2019-08-31 04:07:25 +05:00
|
|
|
if (defaultValue) {
|
|
|
|
onUpdate(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
onClose();
|
|
|
|
}, [defaultValue, onUpdate, onClose]);
|
|
|
|
|
2022-06-20 18:27:39 +02:00
|
|
|
const handleToggleEditingClick = useCallback(() => {
|
2019-08-31 04:07:25 +05:00
|
|
|
setData(createData(defaultValue));
|
2022-06-20 18:27:39 +02:00
|
|
|
toggleEditing();
|
|
|
|
}, [defaultValue, setData, toggleEditing]);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
const handleSubmit = useCallback(() => {
|
2019-08-31 04:07:25 +05:00
|
|
|
const parts = {
|
|
|
|
hours: parseInt(data.hours, 10),
|
|
|
|
minutes: parseInt(data.minutes, 10),
|
|
|
|
seconds: parseInt(data.seconds, 10),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Number.isNaN(parts.hours)) {
|
|
|
|
hoursField.current.select();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Number.isNaN(parts.minutes) || parts.minutes > 60) {
|
|
|
|
minutesField.current.select();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Number.isNaN(parts.seconds) || parts.seconds > 60) {
|
|
|
|
secondsField.current.select();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defaultValue) {
|
2023-02-27 19:09:51 +01:00
|
|
|
if (!dequal(parts, getStopwatchParts(defaultValue))) {
|
|
|
|
onUpdate(updateStopwatch(defaultValue, parts));
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
} else {
|
2023-02-27 19:09:51 +01:00
|
|
|
onUpdate(createStopwatch(parts));
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
onClose();
|
|
|
|
}, [defaultValue, onUpdate, onClose, data]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isEditing) {
|
|
|
|
hoursField.current.select();
|
|
|
|
}
|
|
|
|
}, [isEditing]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Popup.Header onBack={onBack}>
|
2023-02-27 19:09:51 +01:00
|
|
|
{t('common.editStopwatch', {
|
2019-08-31 04:07:25 +05:00
|
|
|
context: 'title',
|
|
|
|
})}
|
|
|
|
</Popup.Header>
|
|
|
|
<Popup.Content>
|
|
|
|
<Form onSubmit={handleSubmit}>
|
|
|
|
<div className={styles.fieldWrapper}>
|
|
|
|
<div className={styles.fieldBox}>
|
|
|
|
<div className={styles.text}>{t('common.hours')}</div>
|
2019-11-15 03:45:59 +05:00
|
|
|
<Input.Mask
|
2019-08-31 04:07:25 +05:00
|
|
|
ref={hoursField}
|
|
|
|
name="hours"
|
|
|
|
value={data.hours}
|
|
|
|
mask="9999"
|
|
|
|
maskChar={null}
|
|
|
|
disabled={!isEditing}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={styles.fieldBox}>
|
|
|
|
<div className={styles.text}>{t('common.minutes')}</div>
|
2019-11-15 03:45:59 +05:00
|
|
|
<Input.Mask
|
2019-08-31 04:07:25 +05:00
|
|
|
ref={minutesField}
|
|
|
|
name="minutes"
|
|
|
|
value={data.minutes}
|
|
|
|
mask="99"
|
|
|
|
maskChar={null}
|
|
|
|
disabled={!isEditing}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={styles.fieldBox}>
|
|
|
|
<div className={styles.text}>{t('common.seconds')}</div>
|
2019-11-15 03:45:59 +05:00
|
|
|
<Input.Mask
|
2019-08-31 04:07:25 +05:00
|
|
|
ref={secondsField}
|
|
|
|
name="seconds"
|
|
|
|
value={data.seconds}
|
|
|
|
mask="99"
|
|
|
|
maskChar={null}
|
|
|
|
disabled={!isEditing}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
icon={isEditing ? 'close' : 'edit'}
|
|
|
|
className={styles.iconButton}
|
2022-06-20 18:27:39 +02:00
|
|
|
onClick={handleToggleEditingClick}
|
2019-08-31 04:07:25 +05:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{isEditing && <Button positive content={t('action.save')} />}
|
|
|
|
</Form>
|
2020-02-03 18:42:31 +05:00
|
|
|
{!isEditing &&
|
|
|
|
(defaultValue && defaultValue.startedAt ? (
|
2019-08-31 04:07:25 +05:00
|
|
|
<Button positive content={t('action.stop')} icon="pause" onClick={handleStopClick} />
|
|
|
|
) : (
|
|
|
|
<Button positive content={t('action.start')} icon="play" onClick={handleStartClick} />
|
|
|
|
))}
|
|
|
|
<Button
|
|
|
|
negative
|
|
|
|
content={t('action.remove')}
|
|
|
|
className={styles.deleteButton}
|
|
|
|
onClick={handleClearClick}
|
|
|
|
/>
|
|
|
|
</Popup.Content>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-02-27 19:09:51 +01:00
|
|
|
StopwatchEditStep.propTypes = {
|
2019-08-31 04:07:25 +05:00
|
|
|
defaultValue: PropTypes.object, // eslint-disable-line react/forbid-prop-types
|
|
|
|
onUpdate: PropTypes.func.isRequired,
|
|
|
|
onBack: PropTypes.func,
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2023-02-27 19:09:51 +01:00
|
|
|
StopwatchEditStep.defaultProps = {
|
2019-08-31 04:07:25 +05:00
|
|
|
defaultValue: undefined,
|
|
|
|
onBack: undefined,
|
|
|
|
};
|
|
|
|
|
2023-02-27 19:09:51 +01:00
|
|
|
export default StopwatchEditStep;
|