Skip to content

Commit

Permalink
feat(web): implement referenced items resolver (#1059)
Browse files Browse the repository at this point in the history
* feat: implement referenced items resolver
  • Loading branch information
nourbalaha authored and yk-eukarya committed Oct 1, 2024
1 parent daec107 commit 188d925
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 119 deletions.
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 @@ export type Item = {
createdAt: Date;
updatedAt: Date;
status: ItemStatus;
referencedItems?: FormItem[];
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 @@ import {
useUpdateItemMutation,
useUpdateRequestMutation,
useSearchItemQuery,
useGetItemsByIdsQuery,
useGetGroupsQuery,
FieldType as GQLFieldType,
StringOperator,
Expand Down Expand Up @@ -188,27 +187,9 @@ export default () => {
[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],
);

const { data: groupData } = useGetGroupsQuery({
Expand Down
34 changes: 7 additions & 27 deletions web/src/components/organisms/Project/Content/ContentList/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
Comment as GQLComment,
useSearchItemQuery,
Asset as GQLAsset,
useGetItemsByIdsQuery,
useUpdateItemMutation,
useCreateItemMutation,
SchemaFieldType,
Expand Down Expand Up @@ -113,34 +112,15 @@ export default () => {
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);
}
});
}
});

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 @@ export const convertItem = (GQLItem: GQLItem | undefined): Item | undefined => {
}) 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,
})) ?? [],
createdBy: GQLItem.createdBy?.name,
updatedBy: GQLItem.updatedBy?.name,
createdAt: GQLItem.createdAt,
Expand Down
Loading

0 comments on commit 188d925

Please sign in to comment.