From 3ba7837d056813ca5e1e96af224c8fb42654c3b3 Mon Sep 17 00:00:00 2001 From: Samer Salameh Date: Thu, 12 Nov 2026 11:23:04 +0300 Subject: [PATCH] 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 --- src/features/forms/EntityForm.tsx | 15 ++++ src/features/view/RecordActionButtons.tsx | 90 +++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/features/view/RecordActionButtons.tsx diff --git a/src/features/forms/EntityForm.tsx b/src/features/forms/EntityForm.tsx index b67ad86..0dbc6ee 100644 --- a/src/features/forms/EntityForm.tsx +++ b/src/features/forms/EntityForm.tsx @@ -14,6 +14,7 @@ import { getExtraFields, getView, runAction, type ExtraSpec, type Option } from import { fetchRecaptchaSiteKey } from '../../api/session'; import { useRecaptcha } from '../../auth/recaptcha'; 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 { AttachmentField } from './AttachmentField'; import { DateField } from '../../components/DateField'; @@ -606,6 +607,20 @@ export function EntityForm({ {t(action)} )} + {/* 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 && ( + { + 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' && } diff --git a/src/features/view/RecordActionButtons.tsx b/src/features/view/RecordActionButtons.tsx new file mode 100644 index 0000000..3226d3f --- /dev/null +++ b/src/features/view/RecordActionButtons.tsx @@ -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; + onDone: () => void; +}) { + const t = useT(); + const ui = useUi(); + const notify = useNotify(); + const qc = useQueryClient(); + const [action, setAction] = useState(null); + const [extra, setExtra] = useState<{ action: EntityAction; spec: ExtraSpec } | null>(null); + + const run = useMutation({ + meta: { loading: 'processing' }, + mutationFn: (v: { a: EntityAction; reason?: Record }) => 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) => ( + + ))} + + setAction(null)}> + {t(String(action?.ACTION_CAPTION_CODE ?? action?.ACTION_NAME ?? ''))} + + {t('CONFIRM_MSG')} + {run.error && {t((run.error as Error).message)}} + + + + + + + + {extra && ( + setExtra(null)} + onSubmit={(reason) => run.mutate({ a: extra.action, reason })} + /> + )} + + ); +}