Skip to content
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

refactor(FormEditor): use .getRelationship() in viewSpec #4763

Open
wants to merge 3 commits into
base: production
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 10 additions & 11 deletions specifyweb/frontend/js_src/lib/components/FormEditor/viewSpec.ts
Expand Up @@ -406,12 +406,14 @@ const subViewSpec = (
syncer(
(raw: string) => {
const cellName = cell.rest.node.attributes.name;
const cellRelationship = table?.fields
.filter((field) => field.isRelationship)
.find((table) => table.name === cellName) as
| Relationship
| undefined;
const cellRelatedTableName = cellRelationship?.relatedTable.name;
const cellRelationship =
typeof cellName === 'string'
? syncers.field(table?.name).serializer(cellName)?.at(-1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by using syncers.field, an error will be thrown on invalid field name

: undefined;
const cellRelatedTableName =
cellRelationship?.isRelationship === true
? cellRelationship.relatedTable.name
: undefined;
const parsed = toLargeSortConfig(raw);
const fieldNames = syncers
.field(cellRelatedTableName)
Expand Down Expand Up @@ -774,11 +776,8 @@ const textSpec = f.store(() =>
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const textAreaSpec = (
_field: SpecToJson<ReturnType<typeof rawFieldSpec>>,
{
rawType,
}: {
readonly rawType: string;
}
_: unknown,
rawType: string
) =>
createXmlSpec({
rows: pipe(
Expand Down
7 changes: 4 additions & 3 deletions specifyweb/frontend/js_src/lib/components/Syncer/syncers.ts
Expand Up @@ -599,7 +599,8 @@ export const syncers = {
MAPPER extends {
readonly [KEY in TYPE_MAPPER[keyof TYPE_MAPPER] | 'Unknown']: (
input: IN,
extra: EXTRA & { readonly rawType: keyof TYPE_MAPPER }
extra: EXTRA,
rawType: keyof TYPE_MAPPER
) => BaseSpec<SimpleXmlNode>;
},
MAPPED extends {
Expand Down Expand Up @@ -658,7 +659,7 @@ export const syncers = {
]) as TYPE_MAPPER[keyof TYPE_MAPPER]) ?? ('Unknown' as const);
const spec = mapper[type] ?? mapper.Unknown;
const { serializer } = syncers.object(
spec(cell, { ...extraPayload, rawType })
spec(cell, extraPayload, rawType)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was causing extraPayload object to loose it's prototype chain, thus table was loosing the getRelationship and all other methods

);

return {
Expand Down Expand Up @@ -692,7 +693,7 @@ export const syncers = {
)?.[0] ?? rawType;
const spec = mapper[definition.type] ?? mapper.Unknown;
const { deserializer } = syncers.object(
spec(cell as unknown as IN, { ...extraPayload, rawType })
spec(cell as unknown as IN, extraPayload, rawType)
);
const node = deserializer(definition);
const rawNode: NodeWithContext<SimpleXmlNode> = {
Expand Down