mirror of
https://github.com/remvze/moodist.git
synced 2025-12-17 08:54:13 +00:00
Merge branch 'develop'
This commit is contained in:
commit
136a009379
1 changed files with 40 additions and 20 deletions
|
|
@ -12,18 +12,38 @@ interface SettingProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Setting({ onChange, onClose, show, times }: SettingProps) {
|
export function Setting({ onChange, onClose, show, times }: SettingProps) {
|
||||||
const [values, setValues] = useState(times);
|
const [values, setValues] = useState<Record<string, number | string>>(times);
|
||||||
|
|
||||||
useEffect(() => setValues(times), [times]);
|
useEffect(() => {
|
||||||
|
if (show) setValues(times);
|
||||||
|
}, [times, show]);
|
||||||
|
|
||||||
const handleChange = (id: string) => (value: number) => {
|
const handleChange = (id: string) => (value: number | string) => {
|
||||||
setValues(prev => ({ ...prev, [id]: value * 60 }));
|
setValues(prev => ({
|
||||||
|
...prev,
|
||||||
|
[id]: typeof value === 'number' ? value * 60 : '',
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = e => {
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
onChange(values);
|
console.log({ values });
|
||||||
|
|
||||||
|
const newValues: Record<string, number> = {};
|
||||||
|
|
||||||
|
Object.keys(values).forEach(name => {
|
||||||
|
newValues[name] =
|
||||||
|
typeof values[name] === 'number' ? values[name] : times[name];
|
||||||
|
});
|
||||||
|
|
||||||
|
onChange(newValues);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleCancel = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
onClose();
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
@ -34,32 +54,29 @@ export function Setting({ onChange, onClose, show, times }: SettingProps) {
|
||||||
<Field
|
<Field
|
||||||
id="pomodoro"
|
id="pomodoro"
|
||||||
label="Pomodoro"
|
label="Pomodoro"
|
||||||
value={values.pomodoro / 60}
|
value={values.pomodoro}
|
||||||
onChange={handleChange('pomodoro')}
|
onChange={handleChange('pomodoro')}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
id="short"
|
id="short"
|
||||||
label="Short Break"
|
label="Short Break"
|
||||||
value={values.short / 60}
|
value={values.short}
|
||||||
onChange={handleChange('short')}
|
onChange={handleChange('short')}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
id="long"
|
id="long"
|
||||||
label="Long Break"
|
label="Long Break"
|
||||||
value={values.long / 60}
|
value={values.long}
|
||||||
onChange={handleChange('long')}
|
onChange={handleChange('long')}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className={styles.buttons}>
|
<div className={styles.buttons}>
|
||||||
<button
|
<button type="button" onClick={handleCancel}>
|
||||||
onClick={e => {
|
|
||||||
e.preventDefault();
|
|
||||||
onClose();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
Cancel
|
Cancel
|
||||||
</button>
|
</button>
|
||||||
<button className={styles.primary}>Save</button>
|
<button className={styles.primary} type="submit">
|
||||||
|
Save
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
@ -69,8 +86,8 @@ export function Setting({ onChange, onClose, show, times }: SettingProps) {
|
||||||
interface FieldProps {
|
interface FieldProps {
|
||||||
id: string;
|
id: string;
|
||||||
label: string;
|
label: string;
|
||||||
onChange: (value: number) => void;
|
onChange: (value: number | string) => void;
|
||||||
value: number;
|
value: number | string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Field({ id, label, onChange, value }: FieldProps) {
|
function Field({ id, label, onChange, value }: FieldProps) {
|
||||||
|
|
@ -83,9 +100,12 @@ function Field({ id, label, onChange, value }: FieldProps) {
|
||||||
className={styles.input}
|
className={styles.input}
|
||||||
max={120}
|
max={120}
|
||||||
min={1}
|
min={1}
|
||||||
|
required
|
||||||
type="number"
|
type="number"
|
||||||
value={value}
|
value={typeof value === 'number' ? value / 60 : ''}
|
||||||
onChange={e => onChange(Number(e.target.value))}
|
onChange={e => {
|
||||||
|
onChange(e.target.value === '' ? '' : Number(e.target.value));
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue