import { useEffect, useState } from 'react'; import AddIcon from '@mui/icons-material/Add'; import CancelIcon from '@mui/icons-material/Cancel'; import DoneIcon from '@mui/icons-material/Done'; import RemoveIcon from '@mui/icons-material/RemoveCircleOutline'; import { Box, Button, Checkbox, Dialog, DialogActions, DialogContent, DialogTitle, Grid, TextField, ToggleButton, ToggleButtonGroup, Typography } from '@mui/material'; import { dialogStyle } from 'CustomTheme'; import type Schema from 'async-validator'; import type { ValidateFieldsError } from 'async-validator'; import { BlockFormControlLabel, ValidatedTextField } from 'components'; import { useI18nContext } from 'i18n/i18n-react'; import { updateValue } from 'utils'; import { validate } from 'validators'; import { ScheduleFlag } from './types'; import type { ScheduleItem } from './types'; interface SchedulerDialogProps { open: boolean; creating: boolean; onClose: () => void; onSave: (ei: ScheduleItem) => void; selectedItem: ScheduleItem; validator: Schema; dow: string[]; } const SchedulerDialog = ({ open, creating, onClose, onSave, selectedItem, validator, dow }: SchedulerDialogProps) => { const { LL } = useI18nContext(); const [editItem, setEditItem] = useState(selectedItem); const [fieldErrors, setFieldErrors] = useState(); const updateFormValue = updateValue(setEditItem); useEffect(() => { if (open) { setFieldErrors(undefined); setEditItem(selectedItem); } }, [open, selectedItem]); const close = () => { onClose(); }; const save = async () => { try { setFieldErrors(undefined); await validate(validator, editItem); onSave(editItem); } catch (error) { setFieldErrors(error as ValidateFieldsError); } }; const remove = () => { editItem.deleted = true; onSave(editItem); }; const getFlagNumber = (newFlag: string[]) => { let new_flag = 0; for (const entry of newFlag) { new_flag |= Number(entry); } return new_flag; }; const getFlagString = (f: number) => { const new_flags: string[] = []; if ((f & 129) === 1) { new_flags.push('1'); } if ((f & 130) === 2) { new_flags.push('2'); } if ((f & 4) === 4) { new_flags.push('4'); } if ((f & 8) === 8) { new_flags.push('8'); } if ((f & 16) === 16) { new_flags.push('16'); } if ((f & 32) === 32) { new_flags.push('32'); } if ((f & 64) === 64) { new_flags.push('64'); } if ((f & 131) === 128) { new_flags.push('128'); } if ((f & 131) === 129) { new_flags.push('129'); } if ((f & 131) === 130) { new_flags.push('130'); } return new_flags; }; const isTimer = editItem.flags === ScheduleFlag.SCHEDULE_TIMER; const isCondition = editItem.flags === ScheduleFlag.SCHEDULE_CONDITION; const isOnChange = editItem.flags === ScheduleFlag.SCHEDULE_ONCHANGE; const showFlag = (si: ScheduleItem, flag: number) => ( {flag === ScheduleFlag.SCHEDULE_TIMER ? LL.TIMER(0) : flag === ScheduleFlag.SCHEDULE_ONCHANGE ? 'On Change' : flag === ScheduleFlag.SCHEDULE_CONDITION ? 'Condition' : dow[Math.log(flag) / Math.log(2)]} ); return ( {creating ? LL.ADD(1) + ' ' + LL.NEW(0) : LL.EDIT()}  {LL.SCHEDULE(1)} { setEditItem({ ...editItem, flags: getFlagNumber(flag) & 127 }); }} > {showFlag(editItem, ScheduleFlag.SCHEDULE_MON)} {showFlag(editItem, ScheduleFlag.SCHEDULE_TUE)} {showFlag(editItem, ScheduleFlag.SCHEDULE_WED)} {showFlag(editItem, ScheduleFlag.SCHEDULE_THU)} {showFlag(editItem, ScheduleFlag.SCHEDULE_FRI)} {showFlag(editItem, ScheduleFlag.SCHEDULE_SAT)} {showFlag(editItem, ScheduleFlag.SCHEDULE_SUN)} {isTimer ? ( ) : ( )} {isOnChange ? ( ) : ( )} {isCondition ? ( ) : ( )} } label={LL.ACTIVE()} /> {isCondition || isOnChange ? ( ) : ( <> {isTimer && ( {LL.SCHEDULER_HELP_2()} )} )} {!creating && ( )} ); }; export default SchedulerDialog;