Fleet edit page: offer the record's other actions (temporary stop) beside Save
The old record page was a form that drew every action the backend listed for the record's status. The new edit form only had Save. A vehicle's edit page now also shows the record's simple actions (temporary stop, delete...), with the same confirmation / reason popup the record page uses; Save stays hidden when the role cannot update the record. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e631b3c12b
commit
3ba7837d05
|
|
@ -14,6 +14,7 @@ import { getExtraFields, getView, runAction, type ExtraSpec, type Option } from
|
||||||
import { fetchRecaptchaSiteKey } from '../../api/session';
|
import { fetchRecaptchaSiteKey } from '../../api/session';
|
||||||
import { useRecaptcha } from '../../auth/recaptcha';
|
import { useRecaptcha } from '../../auth/recaptcha';
|
||||||
import { fromInputDate, resolveFields, toDisplayDate, toInputDate, toPayload, type Mode, type ResolvedField } from './fields';
|
import { fromInputDate, resolveFields, toDisplayDate, toInputDate, toPayload, type Mode, type ResolvedField } from './fields';
|
||||||
|
import { RecordActionButtons } from '../view/RecordActionButtons';
|
||||||
import { CONFIRM_ENTITIES, CREATE_DEFAULTS, CREATE_HIDDEN, CREATE_REQUIRED, EDIT_HIDDEN_FIELDS, aliasSource, EDIT_HIDDEN_SECTIONS, FLEET_ENTITIES, HIDDEN_CREATE_TITLES, NON_OMANI_ONLY, getLayout, type DynamicLookup } from './layouts';
|
import { CONFIRM_ENTITIES, CREATE_DEFAULTS, CREATE_HIDDEN, CREATE_REQUIRED, EDIT_HIDDEN_FIELDS, aliasSource, EDIT_HIDDEN_SECTIONS, FLEET_ENTITIES, HIDDEN_CREATE_TITLES, NON_OMANI_ONLY, getLayout, type DynamicLookup } from './layouts';
|
||||||
import { AttachmentField } from './AttachmentField';
|
import { AttachmentField } from './AttachmentField';
|
||||||
import { DateField } from '../../components/DateField';
|
import { DateField } from '../../components/DateField';
|
||||||
|
|
@ -606,6 +607,20 @@ export function EntityForm({
|
||||||
{t(action)}
|
{t(action)}
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
|
{/* a vehicle's edit page also offers the record's other actions (temporary stop…), as the old form did */}
|
||||||
|
{mode === 'EDIT' && !publicMode && FLEET_ENTITIES.includes(entity) && view.data && id && (
|
||||||
|
<RecordActionButtons
|
||||||
|
entity={entity}
|
||||||
|
id={id}
|
||||||
|
actions={view.data.actions}
|
||||||
|
payload={() => {
|
||||||
|
const d = view.data!.data;
|
||||||
|
const aliased = Object.fromEntries(Object.entries(layout?.alias ?? {}).map(([f, src]) => [f, d[aliasSource(src, lang)]]));
|
||||||
|
return toPayload(fields, { ...d, ...aliased });
|
||||||
|
}}
|
||||||
|
onDone={() => nav(`/e/${entity}/grid`)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
{!publicMode && search.get('own') !== '1' && <Button onClick={() => nav(profile ? '/' : `/e/${entity}/grid`)}>{t('BACK')}</Button>}
|
{!publicMode && search.get('own') !== '1' && <Button onClick={() => nav(profile ? '/' : `/e/${entity}/grid`)}>{t('BACK')}</Button>}
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,90 @@
|
||||||
|
import { useState } from 'react';
|
||||||
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||||
|
import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, Typography } from '@mui/material';
|
||||||
|
import { useNotify } from '../../notify/NotifyProvider';
|
||||||
|
import { useT, useUi } from '../../i18n';
|
||||||
|
import { getExtraFields, runAction, type EntityAction, type ExtraSpec } from '../../api/entity';
|
||||||
|
import { ExtraFieldsDialog } from '../forms/ExtraFieldsDialog';
|
||||||
|
import { isDangerous, isSimpleAction } from './actions';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The record's own actions (temporary stop, delete…) as buttons, for pages that are a form. The old record page was a form
|
||||||
|
* and drew every action the backend listed for the record's status beside Save; the new record page has its own buttons,
|
||||||
|
* and this gives the edit form the same ones. An action that needs a reason opens that popup first, the others ask to confirm.
|
||||||
|
*/
|
||||||
|
export function RecordActionButtons({
|
||||||
|
entity, id, actions, payload, onDone,
|
||||||
|
}: {
|
||||||
|
entity: string;
|
||||||
|
id: string;
|
||||||
|
actions: EntityAction[];
|
||||||
|
/** the record's values, as the record page sends them with an action */
|
||||||
|
payload: () => Record<string, unknown>;
|
||||||
|
onDone: () => void;
|
||||||
|
}) {
|
||||||
|
const t = useT();
|
||||||
|
const ui = useUi();
|
||||||
|
const notify = useNotify();
|
||||||
|
const qc = useQueryClient();
|
||||||
|
const [action, setAction] = useState<EntityAction | null>(null);
|
||||||
|
const [extra, setExtra] = useState<{ action: EntityAction; spec: ExtraSpec } | null>(null);
|
||||||
|
|
||||||
|
const run = useMutation({
|
||||||
|
meta: { loading: 'processing' },
|
||||||
|
mutationFn: (v: { a: EntityAction; reason?: Record<string, string> }) => runAction(entity, v.a.ACTION_NAME, payload(), id, [], v.reason),
|
||||||
|
onSuccess: (res) => {
|
||||||
|
notify.success(String(res.MESSAGE ?? 'SUCCESSFUL.OPERATION'), ui('saved'));
|
||||||
|
setAction(null);
|
||||||
|
setExtra(null);
|
||||||
|
void qc.invalidateQueries({ queryKey: ['grid', entity] });
|
||||||
|
onDone();
|
||||||
|
},
|
||||||
|
onError: (e) => notify.error(e),
|
||||||
|
});
|
||||||
|
|
||||||
|
const ask = async (a: EntityAction) => {
|
||||||
|
run.reset();
|
||||||
|
try {
|
||||||
|
const spec = await getExtraFields(entity, a.ACTION_NAME);
|
||||||
|
if (spec) setExtra({ action: a, spec });
|
||||||
|
else setAction(a);
|
||||||
|
} catch (e) {
|
||||||
|
notify.error(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// saving is the form's own button; creating is not an action of an existing record
|
||||||
|
const list = actions.filter((a) => isSimpleAction(a) && a.ACTION_NAME !== 'UPDATE' && a.ACTION_NAME !== 'CREATE');
|
||||||
|
if (!list.length) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{list.map((a) => (
|
||||||
|
<Button key={a.ACTION_NAME} variant="outlined" color={isDangerous(a.ACTION_NAME) ? 'error' : 'primary'} onClick={() => void ask(a)}>
|
||||||
|
{t(String(a.ACTION_CAPTION_CODE ?? a.ACTION_NAME))}
|
||||||
|
</Button>
|
||||||
|
))}
|
||||||
|
|
||||||
|
<Dialog open={!!action} onClose={() => setAction(null)}>
|
||||||
|
<DialogTitle>{t(String(action?.ACTION_CAPTION_CODE ?? action?.ACTION_NAME ?? ''))}</DialogTitle>
|
||||||
|
<DialogContent>
|
||||||
|
<Typography>{t('CONFIRM_MSG')}</Typography>
|
||||||
|
{run.error && <Alert severity="error" sx={{ mt: 2 }}>{t((run.error as Error).message)}</Alert>}
|
||||||
|
</DialogContent>
|
||||||
|
<DialogActions>
|
||||||
|
<Button onClick={() => setAction(null)}>{t('NO')}</Button>
|
||||||
|
<Button variant="contained" disabled={run.isPending} onClick={() => action && run.mutate({ a: action })}>{t('YES')}</Button>
|
||||||
|
</DialogActions>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
|
{extra && (
|
||||||
|
<ExtraFieldsDialog
|
||||||
|
spec={extra.spec}
|
||||||
|
title={t(String(extra.action.ACTION_CAPTION_CODE ?? extra.action.ACTION_NAME))}
|
||||||
|
onCancel={() => setExtra(null)}
|
||||||
|
onSubmit={(reason) => run.mutate({ a: extra.action, reason })}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue