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

feat(web): implement referenced items resolver #1059

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web/src/components/molecules/Content/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
createdAt: Date;
updatedAt: Date;
status: ItemStatus;
referencedItems?: FormItem[];

Check warning on line 23 in web/src/components/molecules/Content/types.ts

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/types.ts#L23

Added line #L23 was not covered by tests
fields: ItemField[] | undefined | null;
metadata: {
id?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
useUpdateItemMutation,
useUpdateRequestMutation,
useSearchItemQuery,
useGetItemsByIdsQuery,
useGetGroupsQuery,
FieldType as GQLFieldType,
StringOperator,
Expand Down Expand Up @@ -188,27 +187,9 @@
[data?.node],
);

const formReferenceItemsIds = useMemo(
() =>
(currentItem?.fields
?.filter(
field => field.type === "Reference" && field.value && typeof field.value === "string",
)
.map(field => field.value) as string[]) ?? [],
[currentItem?.fields],
);

const { data: gqlFormItemsData } = useGetItemsByIdsQuery({
fetchPolicy: "no-cache",
variables: {
id: formReferenceItemsIds,
},
skip: formReferenceItemsIds.length === 0,
});

const formItemsData: FormItem[] | undefined = useMemo(
() => gqlFormItemsData?.nodes as FormItem[],
[gqlFormItemsData?.nodes],
() => currentItem?.referencedItems as FormItem[],
[currentItem?.referencedItems],

Check warning on line 192 in web/src/components/organisms/Project/Content/ContentDetails/hooks.ts

View check run for this annotation

Codecov / codecov/patch

web/src/components/organisms/Project/Content/ContentDetails/hooks.ts#L191-L192

Added lines #L191 - L192 were not covered by tests
);

const { data: groupData } = useGetGroupsQuery({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
Comment as GQLComment,
useSearchItemQuery,
Asset as GQLAsset,
useGetItemsByIdsQuery,
useUpdateItemMutation,
useCreateItemMutation,
SchemaFieldType,
Expand Down Expand Up @@ -113,34 +112,15 @@
selectedRowKeys: [],
});

const referencedItemsIds = useMemo(
() =>
data?.searchItem?.nodes
? data.searchItem.nodes
.filter(item => item?.fields && item?.fields.length > 0)
.flatMap(
item =>
item?.fields
.filter(field => field.type === "Reference" && field.value)
.map(field => field.value),
)
: [],
[data],
);

const { data: referencedItems } = useGetItemsByIdsQuery({
fetchPolicy: "no-cache",
variables: {
id: referencedItemsIds,
},
skip: !referencedItemsIds.length,
});

// eslint-disable-next-line react-hooks/exhaustive-deps
const referencedItemsMap = new Map<string, any>();
(referencedItems?.nodes ?? []).forEach(item => {
if (item && item.__typename === "Item") {
referencedItemsMap.set(item.id, item);
data?.searchItem.nodes?.forEach(item => {
if (item?.referencedItems && item.referencedItems.length > 0) {
item.referencedItems.forEach(reference => {
if (!referencedItemsMap.has(reference.id)) {
referencedItemsMap.set(reference.id, reference);
}
});

Check warning on line 123 in web/src/components/organisms/Project/Content/ContentList/hooks.ts

View check run for this annotation

Codecov / codecov/patch

web/src/components/organisms/Project/Content/ContentList/hooks.ts#L117-L123

Added lines #L117 - L123 were not covered by tests
}
});

Expand Down
10 changes: 10 additions & 0 deletions web/src/components/organisms/Project/Content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
}) as ItemField,
),
status: GQLItem.status,
referencedItems:
GQLItem.referencedItems?.map(item => ({
id: item.id,
title: item.title ?? "",
schemaId: item.schemaId,
createdBy: item.createdBy?.name ?? "",
status: item.status,
createdAt: item.createdAt,
updatedAt: item.updatedAt,
})) ?? [],

Check warning on line 28 in web/src/components/organisms/Project/Content/utils.ts

View check run for this annotation

Codecov / codecov/patch

web/src/components/organisms/Project/Content/utils.ts#L19-L28

Added lines #L19 - L28 were not covered by tests
createdBy: GQLItem.createdBy?.name,
updatedBy: GQLItem.updatedBy?.name,
createdAt: GQLItem.createdAt,
Expand Down
Loading
Loading