From c7f23674f97aa3bb4839d32e6bcee0fe2c825d57 Mon Sep 17 00:00:00 2001 From: DrMint Date: Sat, 5 Mar 2022 21:05:20 +0100 Subject: [PATCH] Filtering primary and secondary items --- src/graphql/operation.graphql | 1 + src/graphql/operations-types.ts | 1 + src/pages/library/index.tsx | 47 ++++++++++++++++++++++++++++----- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/graphql/operation.graphql b/src/graphql/operation.graphql index 99d0f31..3f5b07f 100644 --- a/src/graphql/operation.graphql +++ b/src/graphql/operation.graphql @@ -148,6 +148,7 @@ query getLibraryItemsPreview($language_code: String) { subtitle slug root_item + primary thumbnail { data { attributes { diff --git a/src/graphql/operations-types.ts b/src/graphql/operations-types.ts index 1789afd..603eaeb 100644 --- a/src/graphql/operations-types.ts +++ b/src/graphql/operations-types.ts @@ -252,6 +252,7 @@ export type GetLibraryItemsPreviewQuery = { subtitle: string; slug: string; root_item: boolean; + primary: boolean; thumbnail: { __typename: "UploadFileEntityResponse"; data: { diff --git a/src/pages/library/index.tsx b/src/pages/library/index.tsx index 6a2e075..1c511aa 100644 --- a/src/pages/library/index.tsx +++ b/src/pages/library/index.tsx @@ -33,12 +33,21 @@ export default function Library(props: LibraryProps): JSX.Element { const langui = props.langui.websiteInterfaces.data[0].attributes; const [showSubitems, setShowSubitems] = useState(false); + const [showPrimaryItems, setShowPrimaryItems] = useState(true); + const [showSecondaryItems, setShowSecondaryItems] = useState(false); const [sortingMethod, setSortingMethod] = useState(0); const [groupingMethod, setGroupingMethod] = useState(-1); const [filteredItems, setFilteredItems] = useState< LibraryProps["libraryItems"]["libraryItems"]["data"] - >(filterItems(showSubitems, props.libraryItems.libraryItems.data)); + >( + filterItems( + showSubitems, + showPrimaryItems, + showSecondaryItems, + props.libraryItems.libraryItems.data + ) + ); const [sortedItems, setSortedItem] = useState< LibraryProps["libraryItems"]["libraryItems"]["data"] @@ -50,9 +59,19 @@ export default function Library(props: LibraryProps): JSX.Element { useEffect(() => { setFilteredItems( - filterItems(showSubitems, props.libraryItems.libraryItems.data) + filterItems( + showSubitems, + showPrimaryItems, + showSecondaryItems, + props.libraryItems.libraryItems.data + ) ); - }, [showSubitems, props.libraryItems.libraryItems.data]); + }, [ + showSubitems, + props.libraryItems.libraryItems.data, + showPrimaryItems, + showSecondaryItems, + ]); useEffect(() => { setSortedItem(sortBy(sortingMethod, filteredItems)); @@ -95,6 +114,16 @@ export default function Library(props: LibraryProps): JSX.Element {

{langui.show_subitems}:

+ +
+

{langui.show_primary_items}:

+ +
+ +
+

{langui.show_secondary_items}:

+ +
); const contentPanel = ( @@ -242,12 +271,14 @@ function getGroups( function filterItems( showSubitems: boolean, + showPrimaryItems: boolean, + showSecondaryItems: boolean, items: LibraryProps["libraryItems"]["libraryItems"]["data"] ): LibraryProps["libraryItems"]["libraryItems"]["data"] { return [...items].filter((item) => { - let result = true; - if (!showSubitems && !item.attributes.root_item) result = false; + if (!showSubitems && !item.attributes.root_item) return false; if ( + showSubitems && item.attributes.metadata.length > 0 && item.attributes.metadata[0].__typename === "ComponentMetadataOther" && (item.attributes.metadata[0].subtype.data.attributes.slug === @@ -255,8 +286,10 @@ function filterItems( item.attributes.metadata[0].subtype.data.attributes.slug === "relation-set") ) - result = false; - return result; + return false; + if (item.attributes.primary && !showPrimaryItems) return false; + if (!item.attributes.primary && !showSecondaryItems) return false; + return true; }); }