-
Notifications
You must be signed in to change notification settings - Fork 9
Forcefully add special subproperties to the selection of RichText, file, and role fields #852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,15 @@ import type { | |
| UseActionFormSubmit, | ||
| } from "../use-action-form/types.js"; | ||
| import { isPlainObject, processDefaultValues, toDefaultValues } from "../use-action-form/utils.js"; | ||
| import { getRelatedModelFields, isHasManyOrHasManyThroughField, isRelationshipField, pathListToSelection } from "../use-table/helpers.js"; | ||
| import { | ||
| fileSelection, | ||
| getRelatedModelFields, | ||
| isHasManyOrHasManyThroughField, | ||
| isRelationshipField, | ||
| pathListToSelection, | ||
| richTextSelection, | ||
| roleAssignmentsSelection, | ||
| } from "../use-table/helpers.js"; | ||
| import type { FieldErrors, FieldValues, UseFormReturn } from "../useActionForm.js"; | ||
| import { useActionForm } from "../useActionForm.js"; | ||
| import { get, getFlattenedObjectKeys, set } from "../utils.js"; | ||
|
|
@@ -221,7 +229,7 @@ const useFormSelection = (props: { | |
| if (!select || !modelApiIdentifier) { | ||
| return; | ||
| } | ||
| return forceIdsIntoSelect({ select, rootFieldsMetadata }); | ||
| return forceRequiredFieldsIntoSelect({ select, rootFieldsMetadata }); | ||
| }, [select, modelApiIdentifier, rootFieldsMetadata]); | ||
|
|
||
| if (!modelApiIdentifier || !fields.length) { | ||
|
|
@@ -238,38 +246,56 @@ const useFormSelection = (props: { | |
| return pathListToSelection(modelApiIdentifier, paths, fieldMetaData); | ||
| }; | ||
|
|
||
| const forceIdsIntoSelect = (props: { select: FieldSelection; rootFieldsMetadata: FieldMetadata[] }) => { | ||
| const forceRequiredFieldsIntoSelect = (props: { select: FieldSelection; rootFieldsMetadata: FieldMetadata[] }) => { | ||
| const { select: originalSelect, rootFieldsMetadata } = props; | ||
| const select = structuredClone(originalSelect); | ||
|
|
||
| select.id = true; // Always select the ID for the root model | ||
|
|
||
| const addIdToSelection = (selectPath: string, fieldMetadata: FieldMetadata) => { | ||
| if (!isRelationshipField(fieldMetadata)) { | ||
| return; // Non relationships do not need additional selection | ||
| } | ||
| const addRequiredFieldsToSelection = (selectPath: string, fieldMetadata: FieldMetadata) => { | ||
| const isRichTextField = fieldMetadata.fieldType === FieldType.RichText; | ||
| const isFileField = fieldMetadata.fieldType === FieldType.File; | ||
| const isRolesField = fieldMetadata.fieldType === FieldType.RoleAssignments; | ||
| const isRelationship = isRelationshipField(fieldMetadata); | ||
|
|
||
| const existingSelection = get(select, selectPath); | ||
| if (!existingSelection || typeof existingSelection !== "object") { | ||
| // Do not go deeper than what is defined in the select object | ||
| return; | ||
| if (!existingSelection) { | ||
| return; // Do not select at all | ||
| } | ||
|
|
||
| const isManyRelation = isHasManyOrHasManyThroughField(fieldMetadata); | ||
| const currentFieldSelectPathPrefix = isManyRelation ? `${selectPath}.edges.node` : `${selectPath}`; | ||
| const idPath = `${currentFieldSelectPathPrefix}.id`; | ||
| if (isRichTextField) { | ||
| return set(select, selectPath, richTextSelection); // Assume that the whole rich text is expected to be selected | ||
| } | ||
|
|
||
| set(select, idPath, true); | ||
| if (isFileField) { | ||
| return set(select, selectPath, fileSelection); // Assume whole file is expected to be selected | ||
| } | ||
| if (isRolesField) { | ||
| return set(select, selectPath, roleAssignmentsSelection); // Assume whole role assignments are expected to be selected | ||
| } | ||
|
|
||
| const relatedModelFields = getRelatedModelFields(fieldMetadata); | ||
| if (isRelationship) { | ||
| if (typeof existingSelection !== "object") { | ||
| // Do not go deeper than what is defined in the select object | ||
| return; | ||
| } | ||
|
|
||
| const isManyRelation = isHasManyOrHasManyThroughField(fieldMetadata); | ||
| const currentFieldSelectPathPrefix = isManyRelation ? `${selectPath}.edges.node` : `${selectPath}`; | ||
| const idPath = `${currentFieldSelectPathPrefix}.id`; | ||
|
|
||
| set(select, idPath, true); | ||
|
|
||
| for (const relatedModelField of relatedModelFields) { | ||
| addIdToSelection(`${currentFieldSelectPathPrefix}.${relatedModelField.apiIdentifier}`, relatedModelField); | ||
| const relatedModelFields = getRelatedModelFields(fieldMetadata); | ||
|
|
||
| for (const relatedModelField of relatedModelFields) { | ||
| addRequiredFieldsToSelection(`${currentFieldSelectPathPrefix}.${relatedModelField.apiIdentifier}`, relatedModelField); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| for (const field of rootFieldsMetadata) { | ||
| addIdToSelection(field.apiIdentifier, field); | ||
| addRequiredFieldsToSelection(field.apiIdentifier, field); | ||
| } | ||
|
|
||
| return select; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this is complicated enough to deserve a test -- if you're outskie today LMK and I can whip one up!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll be around until about 4pm. I'll try to get one up before then
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately I gotta run now. If you could add some quick tests, that would be spectacular 🫡 |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sadly api-client-core is no more -- this change has to go in the generated code inside the monorepo now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sadly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops - fat fingered a merge but reverted immediately here #964