edit name from dialog

This commit is contained in:
Proddy
2023-03-02 17:13:29 +01:00
parent 54eee7ca9f
commit 0dfd32412e
2 changed files with 57 additions and 65 deletions

View File

@@ -51,7 +51,6 @@ import { ScheduleItem, ScheduleFlag } from './types';
import { useI18nContext } from 'i18n/i18n-react'; import { useI18nContext } from 'i18n/i18n-react';
import * as EMSESP from './api'; import * as EMSESP from './api';
export const APIURL = window.location.origin + '/api/';
const SettingsScheduler: FC = () => { const SettingsScheduler: FC = () => {
const { LL, locale } = useI18nContext(); const { LL, locale } = useI18nContext();
@@ -69,7 +68,8 @@ const SettingsScheduler: FC = () => {
time: '12:00', time: '12:00',
cmd: '', cmd: '',
value: '', value: '',
description: '' description: '',
o_id: ''
}; };
const [schedule, setSchedule] = useState<ScheduleItem[]>([emptySchedule]); const [schedule, setSchedule] = useState<ScheduleItem[]>([emptySchedule]);
const [scheduleItem, setScheduleItem] = useState<ScheduleItem>(); const [scheduleItem, setScheduleItem] = useState<ScheduleItem>();
@@ -97,7 +97,7 @@ const SettingsScheduler: FC = () => {
const schedule_theme = useTheme({ const schedule_theme = useTheme({
Table: ` Table: `
--data-table-library_grid-template-columns: 140px 48px 324px 72px 240px repeat(1, minmax(100px, 1fr)); --data-table-library_grid-template-columns: 152px 36px 324px 72px 240px repeat(1, minmax(100px, 1fr));
`, `,
BaseRow: ` BaseRow: `
font-size: 14px; font-size: 14px;
@@ -106,22 +106,18 @@ const SettingsScheduler: FC = () => {
} }
`, `,
BaseCell: ` BaseCell: `
&:nth-of-type(1) {
padding: 8px;
}
&:nth-of-type(2) { &:nth-of-type(2) {
text-align: center; text-align: center;
}, }
&:nth-of-type(3) {
text-align: center;
},
&:nth-of-type(4) {
text-align: center;
},
`, `,
HeaderRow: ` HeaderRow: `
text-transform: uppercase; text-transform: uppercase;
background-color: black; background-color: black;
color: #90CAF9; color: #90CAF9;
.th { .th {
padding: 8px;
border-bottom: 1px solid #565656; border-bottom: 1px solid #565656;
font-weight: 500; font-weight: 500;
height: 36px; height: 36px;
@@ -132,7 +128,6 @@ const SettingsScheduler: FC = () => {
position: relative; position: relative;
cursor: pointer; cursor: pointer;
.td { .td {
padding: 8px;
border-top: 1px solid #565656; border-top: 1px solid #565656;
border-bottom: 1px solid #565656; border-bottom: 1px solid #565656;
} }
@@ -325,13 +320,8 @@ const SettingsScheduler: FC = () => {
}; };
const updateScheduleItem = () => { const updateScheduleItem = () => {
if (scheduleItem) { setSchedule([...schedule.filter((si) => creating || si.o_id !== scheduleItem.o_id), scheduleItem]);
const new_schedule = [...schedule.filter((si) => si.id !== scheduleItem.id), scheduleItem].sort((a, b) =>
a.time.localeCompare(b.time)
);
setSchedule(new_schedule);
setScheduleItem(undefined); setScheduleItem(undefined);
}
}; };
const renderSchedule = () => { const renderSchedule = () => {
@@ -340,7 +330,11 @@ const SettingsScheduler: FC = () => {
} }
return ( return (
<Table data={{ nodes: schedule.filter((si) => !si.deleted) }} theme={schedule_theme} layout={{ custom: true }}> <Table
data={{ nodes: schedule.filter((si) => !si.deleted).sort((a, b) => a.time.localeCompare(b.time)) }}
theme={schedule_theme}
layout={{ custom: true }}
>
{(tableList: any) => ( {(tableList: any) => (
<> <>
<Header> <Header>
@@ -414,7 +408,7 @@ const SettingsScheduler: FC = () => {
if (scheduleItem) { if (scheduleItem) {
try { try {
setFieldErrors(undefined); setFieldErrors(undefined);
await validate(schedulerItemValidation(schedule, creating), scheduleItem); await validate(schedulerItemValidation(schedule, scheduleItem.o_id), scheduleItem);
updateScheduleItem(); updateScheduleItem();
} catch (errors: any) { } catch (errors: any) {
setFieldErrors(errors); setFieldErrors(errors);
@@ -422,16 +416,19 @@ const SettingsScheduler: FC = () => {
} }
}; };
const closeDialog = () => {
setScheduleItem(undefined);
setFieldErrors();
};
const renderEditSchedule = () => { const renderEditSchedule = () => {
if (scheduleItem) { if (scheduleItem) {
return ( return (
<Dialog open={!!scheduleItem} onClose={() => setScheduleItem(undefined)}> <Dialog open={!!scheduleItem} onClose={() => closeDialog()}>
<DialogTitle> <DialogTitle>
{creating ? LL.ADD(0) + ' ' + LL.NEW() + ' ' + LL.SCHEDULE() : LL.EDIT() + " '" + scheduleItem.id + "'"} {creating ? LL.ADD(0) + ' ' + LL.NEW() + ' ' + LL.SCHEDULE() : LL.EDIT() + " '" + scheduleItem.id + "'"}
</DialogTitle> </DialogTitle>
<DialogContent dividers> <DialogContent dividers>
{creating ? (
<>
<ValidatedTextField <ValidatedTextField
fieldErrors={fieldErrors} fieldErrors={fieldErrors}
name="id" name="id"
@@ -442,6 +439,7 @@ const SettingsScheduler: FC = () => {
sx={{ width: '60ch' }} sx={{ width: '60ch' }}
onChange={updateValue(setScheduleItem)} onChange={updateValue(setScheduleItem)}
/> />
{creating ? (
<RadioGroup <RadioGroup
row row
name="schedule-type" name="schedule-type"
@@ -459,7 +457,6 @@ const SettingsScheduler: FC = () => {
<FormControlLabel value="w" control={<Radio />} label={LL.WEEKLY()} /> <FormControlLabel value="w" control={<Radio />} label={LL.WEEKLY()} />
<FormControlLabel value="t" control={<Radio />} label={LL.TIMER()} /> <FormControlLabel value="t" control={<Radio />} label={LL.TIMER()} />
</RadioGroup> </RadioGroup>
</>
) : ( ) : (
<Typography variant="h6" color="primary" sx={{ pb: 1 }}> <Typography variant="h6" color="primary" sx={{ pb: 1 }}>
{LL.TYPE()}:&nbsp;{scheduleItem.flags & ScheduleFlag.SCHEDULE_TIMER ? LL.TIMER() : LL.WEEKLY()} {LL.TYPE()}:&nbsp;{scheduleItem.flags & ScheduleFlag.SCHEDULE_TIMER ? LL.TIMER() : LL.WEEKLY()}
@@ -536,16 +533,11 @@ const SettingsScheduler: FC = () => {
</Button> </Button>
</Box> </Box>
)} )}
<Button <Button startIcon={<CancelIcon />} variant="outlined" onClick={() => closeDialog()} color="secondary">
startIcon={<CancelIcon />}
variant="outlined"
onClick={() => setScheduleItem(undefined)}
color="secondary"
>
{LL.CANCEL()} {LL.CANCEL()}
</Button> </Button>
<Button <Button
startIcon={<DoneIcon />} startIcon={creating ? <AddIcon /> : <DoneIcon />}
variant="outlined" variant="outlined"
type="submit" type="submit"
onClick={() => validateScheduleItem()} onClick={() => validateScheduleItem()}

View File

@@ -85,26 +85,26 @@ export const createSettingsValidator = (settings: Settings) =>
}) })
}); });
export const schedulerItemValidation = (schedule: ScheduleItem[], creating: boolean) => export const schedulerItemValidation = (schedule: ScheduleItem[], o_id: string) =>
new Schema({ new Schema({
id: [ id: [
{ required: true, message: 'Name is required' }, { required: true, message: 'Name is required' },
{ {
type: 'string', type: 'string',
pattern: /^[a-zA-Z0-9_\\.]{1,24}$/, pattern: /^[a-zA-Z0-9_\\.]{1,15}$/,
message: "Must be 1-24 characters: alpha numeric, '_' or '.'" message: "Must be 1-15 characters: alpha numeric, '_' or '.'"
}, },
...(creating ? [uniqueIDValidator(schedule)] : []) ...[uniqueIDValidator(schedule, o_id)]
], ],
cmd: [ cmd: [
{ required: true, message: 'Command is required' }, { required: true, message: 'Command is required' },
{ type: 'string', min: 1, max: 32, message: 'Command must be 1-32 characters' } { type: 'string', min: 1, max: 64, message: 'Command must be 1-64 characters' }
] ]
}); });
export const uniqueIDValidator = (schedule: ScheduleItem[]) => ({ export const uniqueIDValidator = (schedule: ScheduleItem[], o_id: string) => ({
validator(rule: InternalRuleItem, id: string, callback: (error?: string) => void) { validator(rule: InternalRuleItem, id: string, callback: (error?: string) => void) {
if (id && schedule.find((si) => si.id === id)) { if (id && o_id !== id && schedule.find((si) => si.id === id)) {
callback('Name already in use'); callback('Name already in use');
} else { } else {
callback(); callback();