Repository navigation
Expand file tree
/
Copy pathRecordBodyTableEditor.tsx
More file actions
457 lines (430 loc) · 21.2 KB
/
Copy pathRecordBodyTableEditor.tsx
File metadata and controls
457 lines (430 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* RecordBodyTableEditor — a record page's master-detail body table in the
* page's EDIT STATE: the child rows as editable lines (add / edit / remove),
* saved together with the record's own fields in ONE batch by the page host.
*
* The record page draws its body tables as `record:related_list` nodes. In its
* edit state the host draws this editor in each one's place and owns the
* save: the editor holds its rows, and hands the host a snapshot (rows, the rows as
* loaded, the child schema) through {@link RecordEditSessionContext}. The host
* folds the record's field draft and every table's snapshot into one
* `POST /api/v1/batch` with {@link buildRecordEditBatch} — the same
* `buildMasterDetailEditBatch` construction `MasterDetailForm` and
* `LineItemsPanel` persist through, so a line is diffed into
* create / update / delete exactly as the edit form diffs it.
*
* The pieces are `MasterDetailForm`'s: the `LineItemsField` grid, the child
* columns `deriveDetail` derives, the field-permission pass
* (`applyColumnPermissions`) and the add / remove rows of the
* affordance-to-grant map (`relatedNew` / `relatedRowDelete`), so the lines
* edit here as they edit in the form.
*
* ⚖️ The server is the validation authority on the record page (the inline
* surface's ruling, `InlineEditSaveBar`): nothing here evaluates a rule. A
* refused batch is PRESENTED — {@link attributeRecordEditRefusal} places each
* reason the server named on the record's field or on the line whose write
* carried that field.
*/
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import type { BatchTransactionOperation, GridFieldMetadata } from '@object-ui/types';
import { resolveAffordance, type SchemaLike } from '@object-ui/core';
import { LineItemsField, preloadGridField, lookupAutofillByField, columnHelpByField, useBooleanValueLabel, type GridColumn } from '@object-ui/fields';
import { cn } from '@object-ui/components';
import { useLocalization } from '@object-ui/i18n';
import { usePermissions } from '@object-ui/permissions';
import {
extractFieldErrors,
extractWriteErrorMessage,
useInlineEdit,
usePredicateScope,
useRecordContext,
} from '@object-ui/react';
import { buildMasterDetailEditBatch, closesRecord, idOf, isBlankRow, type ChildSchema, type MasterDetailEditOptions } from './masterDetailTx';
import { applyColumnPermissions } from './fieldWriteGate';
import { deriveDetail, lockColumnsByParentHandLock, parentHandLockHolds } from './deriveMasterDetail';
import { schemaDefaultValues } from './schemaDefaults';
import { currencyFieldDefsOf, followedColumnCurrencies, parentCurrencyFieldsOf } from './parentCurrencyColumns';
import { useFormChromeTranslation } from './formChrome';
/** One reason the server gave about one line of a body table. */
export interface BodyTableRowError {
/** The line's position in the table as drawn, from 0. */
row: number;
/** The child field the reason names, when it names one. */
field?: string;
message: string;
}
/** What a body-table editor hands the host at save time. */
export interface BodyTableSnapshot {
childObject: string;
relationshipField: string;
rows: Record<string, any>[];
original: Record<string, any>[];
childSchema: ChildSchema;
}
/** One body-table editor, as the host's session sees it. */
export interface BodyTableEditHandle {
/** The relationship id, `child.fk`. */
id: string;
/** `null` while the table's rows or schema are still loading — the host does not save then. */
snapshot(): BodyTableSnapshot | null;
/** Show (or clear, with `null`) the server's reasons about this table's lines. */
setErrors(rows: BodyTableRowError[] | null, table?: string[] | null): void;
}
/** The host's edit session, read by every body-table editor under it. */
export interface RecordEditSession {
/** Register an editor; the returned function unregisters it. */
register(handle: BodyTableEditHandle): () => void;
/** An editor's lines moved away from (or back to) what it loaded. */
reportDirty(id: string, dirty: boolean): void;
/** A save is in flight: every editor takes no input. */
saving: boolean;
}
export const RecordEditSessionContext = React.createContext<RecordEditSession | null>(null);
/** Who wrote each operation of a record edit batch, by operation index. */
export type RecordEditOpOwner = { kind: 'parent' } | { kind: 'row'; table: string; row: number };
/**
* The ONE batch of a record-page save: the record's field draft (an update of
* only the edited keys) and every body table's lines diffed into
* create / update / delete — `buildMasterDetailEditBatch`, table by table, so
* each op can be traced back to the line it came from ({@link RecordEditOpOwner}).
*
* The record's update is op 0, unless the draft puts the record into one of
* its object's closed states (`options.closedStates`, judged by `closesRecord`
* as the master-detail form judges it): then it is the LAST op, after every
* line write. [ADR-0162 D4] Inside the batch's one transaction each write meets
* the record as stored when it arrives, so a line write after the closing
* update would meet a closed record and the whole save would be refused. Read
* the record's op by its owner (`{ kind: 'parent' }`), never by its index.
*/
export function buildRecordEditBatch(
parentObject: string,
parentId: string,
parentDraft: Record<string, any>,
tables: ReadonlyArray<{ id: string } & BodyTableSnapshot>,
options: MasterDetailEditOptions = {},
): { ops: BatchTransactionOperation[]; owners: RecordEditOpOwner[] } {
const parentOp: BatchTransactionOperation = buildMasterDetailEditBatch(parentObject, parentId, parentDraft, [])[0];
const closing = closesRecord(parentDraft, options.closedStates);
const ops: BatchTransactionOperation[] = closing ? [] : [parentOp];
const owners: RecordEditOpOwner[] = closing ? [] : [{ kind: 'parent' }];
for (const t of tables) {
const tableOps = buildMasterDetailEditBatch(parentObject, parentId, {}, [t]).slice(1);
// Rows are traced back by id (update / delete) or by position among the
// id-less rows (create): the builder emits creates first, in row order,
// skipping blank rows; then updates; then deletes.
const createRows = t.rows
.map((r, i) => ({ r, i }))
.filter(({ r }) => !idOf(r) && !isBlankRow({ ...r, [t.relationshipField]: parentId }, t.relationshipField));
let createCursor = 0;
for (const op of tableOps) {
let row = -1;
if (op.action === 'create') {
row = createRows[createCursor++]?.i ?? -1;
} else if (op.id != null) {
row = t.rows.findIndex((r) => idOf(r) === String(op.id));
}
ops.push(op);
owners.push({ kind: 'row', table: t.id, row });
}
}
if (closing) {
ops.push(parentOp);
owners.push({ kind: 'parent' });
}
return { ops, owners };
}
/** How a refused record-edit batch reads, placed on the field or line it is about. */
export interface RecordEditRefusal {
/** Reasons about the record's own fields, by field name. */
fieldErrors: Record<string, string> | null;
/** Reasons about body-table lines, by table id. */
rowErrors: Record<string, BodyTableRowError[]>;
/** Reasons about a table that no single line carries, by table id. */
tableErrors: Record<string, string[]>;
/** The summary line for the whole save. */
message: string;
}
/**
* Place a refused batch's reasons (objectui#6868: presented, never
* re-derived). The batch answers ONE error for the transaction and does not
* say which operation raised it, so each reason the server attributes to a
* field is placed by the operations that WROTE that field: on the record when
* only the record's update carries it, on a line when exactly one line's write
* carries it, and on its table when several lines of one table do. A reason
* that names no field (a lock, a permission refusal) stays the summary.
*/
export function attributeRecordEditRefusal(
err: unknown,
ops: ReadonlyArray<BatchTransactionOperation>,
owners: ReadonlyArray<RecordEditOpOwner>,
): RecordEditRefusal {
const message = extractWriteErrorMessage(err) ?? (err instanceof Error ? err.message : String(err));
const out: RecordEditRefusal = { fieldErrors: null, rowErrors: {}, tableErrors: {}, message };
const fields = extractFieldErrors(err);
if (!fields) return out;
const parentFields: Record<string, string> = {};
for (const { field, message: reason } of fields) {
const carriers = ops
.map((op, i) => ({ op, owner: owners[i] }))
.filter(({ op }) => op.action !== 'delete' && op.data && Object.prototype.hasOwnProperty.call(op.data, field));
const rows = carriers.filter((c) => c.owner?.kind === 'row') as Array<{ op: BatchTransactionOperation; owner: { kind: 'row'; table: string; row: number } }>;
const parent = carriers.some((c) => c.owner?.kind === 'parent');
if (rows.length === 0) {
// Only the record writes it — or nothing does, and the record is still
// the best place: its fields are the page's inputs.
parentFields[field] = reason;
continue;
}
if (parent) parentFields[field] = reason;
const tables = new Set(rows.map((r) => r.owner.table));
if (rows.length === 1 && rows[0].owner.row >= 0) {
const { table, row } = rows[0].owner;
(out.rowErrors[table] ??= []).push({ row, field, message: reason });
} else {
for (const table of tables) (out.tableErrors[table] ??= []).push(reason);
}
}
out.fieldErrors = Object.keys(parentFields).length > 0 ? parentFields : null;
return out;
}
export interface RecordBodyTableEditorProps {
/** The child object of the body table. */
objectName: string;
/** The child's master-detail FK back to the record. */
relationshipField: string;
/** The table's heading, as the read-mode table drew it. */
title?: string;
}
/** Lines read for one table — the window `LineItemsPanel` and `MasterDetailForm` read. */
const BODY_TABLE_EDIT_LIMIT = 500;
/**
* The editor itself. Reads the record (parent) and the data source from
* `RecordContextProvider` (as the page's related lists do), and the session from
* {@link RecordEditSessionContext}; with no session it renders nothing — it is
* a part of the record page's edit state, never a standalone block.
*/
export const RecordBodyTableEditor: React.FC<RecordBodyTableEditorProps> = ({ objectName, relationshipField, title }) => {
// The line grid's body loads on first use (objectstack#36102). Start it now,
// while the child schema is fetched, so the grid is there before its columns.
preloadGridField();
const session = useContext(RecordEditSessionContext);
const record = useRecordContext();
// The record page's adapter, as its related lists read it.
const dataSource = record?.dataSource as any;
const parentObject = record?.objectName;
const parentId = record?.recordId;
const perms = usePermissions();
const { currency: homeCurrency, timezone: homeTimeZone } = useLocalization();
const { t } = useFormChromeTranslation();
const booleanLabel = useBooleanValueLabel();
const booleanTrue = booleanLabel(true);
const booleanFalse = booleanLabel(false);
const id = `${objectName}.${relationshipField}`;
const [childSchema, setChildSchema] = useState<{ fields?: Record<string, any> } | null>(null);
const [rows, setRows] = useState<Record<string, any>[] | null>(null);
const [original, setOriginal] = useState<Record<string, any>[]>([]);
const [rowErrors, setRowErrors] = useState<BodyTableRowError[] | null>(null);
const [tableErrors, setTableErrors] = useState<string[] | null>(null);
const [loadFailed, setLoadFailed] = useState(false);
useEffect(() => {
let cancelled = false;
if (!dataSource || typeof dataSource.getObjectSchema !== 'function') return;
// The child schema is what draws the line grid, so it lands together with
// the grid's body (objectstack#36102); `preloadGridField` never rejects.
Promise.all([dataSource.getObjectSchema(objectName), preloadGridField()])
.then(([s]) => { if (!cancelled) setChildSchema(s ?? { fields: {} }); })
.catch(() => { if (!cancelled) setLoadFailed(true); });
return () => { cancelled = true; };
}, [dataSource, objectName]);
const derived = useMemo(() => {
if (!childSchema || !parentObject) return null;
try {
return deriveDetail(objectName, childSchema as any, parentObject, { booleanLabel, relationshipField });
} catch {
return null;
}
// Keyed on the two WORDS, not on `booleanLabel` (a new function each render).
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [childSchema, parentObject, objectName, relationshipField, booleanTrue, booleanFalse]);
// The lines as stored, read once per edit session (the editor mounts with it).
useEffect(() => {
let cancelled = false;
if (!dataSource || parentId == null || !derived) return;
Promise.resolve(dataSource.find(objectName, {
$filter: { [relationshipField]: parentId },
...(derived.sortField
? { $orderby: [{ field: derived.sortField, order: 'asc' as const }, { field: 'id', order: 'asc' as const }] }
: {}),
$top: BODY_TABLE_EDIT_LIMIT,
}))
.then((res: any) => {
if (cancelled) return;
const loaded = (res?.data ?? []) as Record<string, any>[];
setOriginal(loaded.map((r) => ({ ...r })));
setRows(loaded.map((r) => ({ ...r })));
})
.catch(() => { if (!cancelled) setLoadFailed(true); });
return () => { cancelled = true; };
}, [dataSource, parentId, derived, objectName, relationshipField]);
// objectstack#35816 — a new line starts on the child object's static
// defaults, the values a create form seeds (`schemaDefaultValues`), so a
// required column with a default is filled instead of flagged empty. A
// default the server resolves per insert (`NOW()`, a CEL expression) is not
// seeded; the server fills it as it fills an omitted field. The session
// facts are the ones `ObjectForm` threads (objectstack#35940): the acting
// user, the organization's currency and time zone. Until the child schema
// answers there are no defaults, and a new line starts blank.
const newRowValues = useMemo(
() =>
childSchema
? schemaDefaultValues(childSchema, { currentUserId: perms.userId, homeCurrency, homeTimeZone })
: undefined,
[childSchema, perms.userId, homeCurrency, homeTimeZone],
);
// objectstack#36249 — the currency of each money column, as the record form
// and the other line hosts hand it to the grid: a line amount that follows
// this record through `relationshipField` is in this record's code
// (`columnCurrencies`), and any other money column is in its own field's
// currency (`columnFields`). A pick copies an amount only into a column in
// the same currency.
//
// The record is read as the user has it in the open edit, not as stored:
// the edit state saves a code change on the record and its new lines in one
// batch, so a code staged in the inline draft is the code the lines are
// saved under. A live value is `{ ...data, ...draft }` (InlineEditContext).
const inline = useInlineEdit();
const savedRow = record?.data as Record<string, unknown> | undefined;
const draftRow = inline?.draft;
const headerRow = useMemo<Record<string, unknown> | undefined>(
() => (draftRow && Object.keys(draftRow).length > 0 ? { ...(savedRow ?? {}), ...draftRow } : savedRow),
[savedRow, draftRow],
);
const lineColumnCurrencies = useMemo(
() => followedColumnCurrencies(
parentCurrencyFieldsOf(childSchema?.fields as Record<string, unknown> | undefined, relationshipField),
headerRow,
homeCurrency,
),
[childSchema, relationshipField, headerRow, homeCurrency],
);
const lineCurrencyFields = useMemo(
() => currencyFieldDefsOf(childSchema?.fields as Record<string, unknown> | undefined),
[childSchema],
);
// objectstack#38521 — the record's whole-record hand lock (its object's
// `uiReadonlyWhen`, from the record context's served definition) covers
// these lines as it covers the edit form's (objectstack#38249). The record
// page keeps Edit live on a locked document when some field could still
// open, so the edit state can open over locked lines. Judged as the edit
// form judges it: over the record as the user has it in the open edit, the
// stored row as `previous` (`parentHandLockHolds`). While it holds, no line
// is added, duplicated, removed or reordered, and every cell whose child
// field owns no hand lock is locked (`lockColumnsByParentHandLock`); a child
// field's own `uiReadonlyWhen` / `uiReadonly: true` judges its cell instead.
// Declared writes are untouched: nothing on the server reads either key.
const predicateScope = usePredicateScope();
const parentSchema = record?.objectSchema as { uiReadonlyWhen?: unknown; fields?: unknown } | undefined;
const parentHandLocked = useMemo(
() => parentHandLockHolds(parentSchema, headerRow, { previous: savedRow, scope: predicateScope, parentObjectName: parentObject }),
[parentSchema, headerRow, savedRow, predicateScope, parentObject],
);
const snapshotRef = useRef<BodyTableSnapshot | null>(null);
snapshotRef.current = rows && derived
? { childObject: objectName, relationshipField, rows, original, childSchema }
: null;
const register = session?.register;
useEffect(() => {
if (!register) return;
return register({
id,
snapshot: () => snapshotRef.current,
setErrors: (r, tbl) => {
setRowErrors(r && r.length > 0 ? r : null);
setTableErrors(tbl && tbl.length > 0 ? tbl : null);
},
});
}, [register, id]);
const reportDirty = session?.reportDirty;
const onChange = useCallback((next: Record<string, any>[]) => {
setRows(next);
reportDirty?.(id, true);
}, [reportDirty, id]);
if (!session) return null;
const lineSource = { objectSchema: childSchema as SchemaLike | null, objectName, perms };
const heading = title || (childSchema as any)?.pluralLabel || (childSchema as any)?.label || objectName;
const labelOf = (field?: string) =>
(field && (derived?.columns.find((c) => c.name === field)?.label || (childSchema?.fields as any)?.[field]?.label)) || field;
return (
<section
className="space-y-2"
data-body-table-editor={id}
aria-busy={rows === null && !loadFailed ? true : undefined}
>
<h3 className="text-sm font-medium text-foreground">{heading}</h3>
{loadFailed ? (
<p className="py-4 text-sm text-destructive" role="alert">
{t('form.lineItems.loadFailed')}
</p>
) : rows === null || !derived ? (
<p className="py-4 text-sm text-muted-foreground">{t('common.loading')}</p>
) : (
<LineItemsField
value={rows}
onChange={onChange}
disabled={session.saving}
contextRecord={record?.data as Record<string, unknown> | undefined}
lookupAutofill={lookupAutofillByField(childSchema?.fields as Record<string, unknown> | undefined)}
columnCurrencies={lineColumnCurrencies}
columnFields={lineCurrencyFields}
// Each child field's help, on its column header (objectstack#35804).
columnHelp={columnHelpByField(childSchema?.fields as Record<string, unknown> | undefined)}
newRowValues={newRowValues}
// objectstack#35816 — a new line's empty required cell reads red
// once a save was refused, not the moment the line appears; the
// server stays the judge of what is missing.
newRowRequiredShown={rowErrors !== null || tableErrors !== null}
field={({
columns: applyColumnPermissions(
parentHandLocked ? lockColumnsByParentHandLock(derived.columns as GridColumn[]) : (derived.columns as GridColumn[]),
{ perms, objectName, mode: 'edit' },
),
totalField: derived.amountField,
sortField: derived.sortField,
// The record's hand lock withholds add (and Duplicate), remove and
// reorder (objectstack#38521).
allowAdd: !parentHandLocked && resolveAffordance('relatedNew', lineSource).allowed,
allowDelete: !parentHandLocked && resolveAffordance('relatedRowDelete', lineSource).allowed,
...(parentHandLocked ? { allowReorder: false } : {}),
} satisfies Partial<GridFieldMetadata>) as GridFieldMetadata}
/>
)}
{(rowErrors || tableErrors) && (
<ul
role="alert"
className={cn('space-y-0.5 rounded-md border border-destructive/20 bg-destructive/10 px-2 py-1 text-xs text-destructive')}
>
{(rowErrors ?? []).map((e, i) => (
<li key={`row-${i}`} data-body-table-row-error={e.row}>
<span className="font-medium">
{t('detail.bodyTableRow', { row: e.row + 1 })}
{e.field ? ` · ${labelOf(e.field)}` : ''}
</span>
{': '}
{e.message}
</li>
))}
{(tableErrors ?? []).map((message, i) => (
<li key={`table-${i}`} data-body-table-error="">{message}</li>
))}
</ul>
)}
</section>
);
};