Skip to content

Commit 13ee708

Browse files
Forcefully add special subproperties to the selection of RichText, file, and role fields
1 parent a2a1b9f commit 13ee708

2 files changed

Lines changed: 47 additions & 21 deletions

File tree

packages/react/src/auto/AutoForm.ts

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ import type {
2020
UseActionFormSubmit,
2121
} from "../use-action-form/types.js";
2222
import { isPlainObject, processDefaultValues, toDefaultValues } from "../use-action-form/utils.js";
23-
import { getRelatedModelFields, isHasManyOrHasManyThroughField, isRelationshipField, pathListToSelection } from "../use-table/helpers.js";
23+
import {
24+
fileSelection,
25+
getRelatedModelFields,
26+
isHasManyOrHasManyThroughField,
27+
isRelationshipField,
28+
pathListToSelection,
29+
richTextSelection,
30+
roleAssignmentsSelection,
31+
} from "../use-table/helpers.js";
2432
import type { FieldErrors, FieldValues, UseFormReturn } from "../useActionForm.js";
2533
import { useActionForm } from "../useActionForm.js";
2634
import { get, getFlattenedObjectKeys, set, type ErrorWrapper, type OptionsType } from "../utils.js";
@@ -219,7 +227,7 @@ const useFormSelection = (props: {
219227
if (!select || !modelApiIdentifier) {
220228
return;
221229
}
222-
return forceIdsIntoSelect({ select, rootFieldsMetadata });
230+
return forceRequiredFieldsIntoSelect({ select, rootFieldsMetadata });
223231
}, [select, modelApiIdentifier, rootFieldsMetadata]);
224232

225233
if (!modelApiIdentifier || !fields.length) {
@@ -236,38 +244,56 @@ const useFormSelection = (props: {
236244
return pathListToSelection(modelApiIdentifier, paths, fieldMetaData);
237245
};
238246

239-
const forceIdsIntoSelect = (props: { select: FieldSelection; rootFieldsMetadata: FieldMetadata[] }) => {
247+
const forceRequiredFieldsIntoSelect = (props: { select: FieldSelection; rootFieldsMetadata: FieldMetadata[] }) => {
240248
const { select: originalSelect, rootFieldsMetadata } = props;
241249
const select = structuredClone(originalSelect);
242250

243251
select.id = true; // Always select the ID for the root model
244252

245-
const addIdToSelection = (selectPath: string, fieldMetadata: FieldMetadata) => {
246-
if (!isRelationshipField(fieldMetadata)) {
247-
return; // Non relationships do not need additional selection
248-
}
253+
const addRequiredFieldsToSelection = (selectPath: string, fieldMetadata: FieldMetadata) => {
254+
const isRichTextField = fieldMetadata.fieldType === FieldType.RichText;
255+
const isFileField = fieldMetadata.fieldType === FieldType.File;
256+
const isRolesField = fieldMetadata.fieldType === FieldType.RoleAssignments;
257+
const isRelationship = isRelationshipField(fieldMetadata);
249258

250259
const existingSelection = get(select, selectPath);
251-
if (!existingSelection || typeof existingSelection !== "object") {
252-
// Do not go deeper than what is defined in the select object
253-
return;
260+
if (!existingSelection) {
261+
return; // Do not select at all
254262
}
255263

256-
const isManyRelation = isHasManyOrHasManyThroughField(fieldMetadata);
257-
const currentFieldSelectPathPrefix = isManyRelation ? `${selectPath}.edges.node` : `${selectPath}`;
258-
const idPath = `${currentFieldSelectPathPrefix}.id`;
264+
if (isRichTextField) {
265+
return set(select, selectPath, richTextSelection); // Assume that the whole rich text is expected to be selected
266+
}
259267

260-
set(select, idPath, true);
268+
if (isFileField) {
269+
return set(select, selectPath, fileSelection); // Assume whole file is expected to be selected
270+
}
271+
if (isRolesField) {
272+
return set(select, selectPath, roleAssignmentsSelection); // Assume whole role assignments are expected to be selected
273+
}
261274

262-
const relatedModelFields = getRelatedModelFields(fieldMetadata);
275+
if (isRelationship) {
276+
if (typeof existingSelection !== "object") {
277+
// Do not go deeper than what is defined in the select object
278+
return;
279+
}
280+
281+
const isManyRelation = isHasManyOrHasManyThroughField(fieldMetadata);
282+
const currentFieldSelectPathPrefix = isManyRelation ? `${selectPath}.edges.node` : `${selectPath}`;
283+
const idPath = `${currentFieldSelectPathPrefix}.id`;
284+
285+
set(select, idPath, true);
263286

264-
for (const relatedModelField of relatedModelFields) {
265-
addIdToSelection(`${currentFieldSelectPathPrefix}.${relatedModelField.apiIdentifier}`, relatedModelField);
287+
const relatedModelFields = getRelatedModelFields(fieldMetadata);
288+
289+
for (const relatedModelField of relatedModelFields) {
290+
addRequiredFieldsToSelection(`${currentFieldSelectPathPrefix}.${relatedModelField.apiIdentifier}`, relatedModelField);
291+
}
266292
}
267293
};
268294

269295
for (const field of rootFieldsMetadata) {
270-
addIdToSelection(field.apiIdentifier, field);
296+
addRequiredFieldsToSelection(field.apiIdentifier, field);
271297
}
272298

273299
return select;

packages/react/src/use-table/helpers.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,18 @@ export const isRelationshipField = (field: { fieldType: GadgetFieldType }) => {
331331
return isHasOneOrBelongsToField(field) || isHasManyOrHasManyThroughField(field);
332332
};
333333

334-
const richTextSelection = {
334+
export const richTextSelection = {
335335
markdown: true,
336336
truncatedHTML: true,
337337
};
338338

339-
const fileSelection = {
339+
export const fileSelection = {
340340
url: true,
341341
mimeType: true,
342342
fileName: true,
343343
};
344344

345-
const roleAssignmentsSelection = {
345+
export const roleAssignmentsSelection = {
346346
key: true,
347347
name: true,
348348
};

0 commit comments

Comments
 (0)