|
| 1 | +/** |
| 2 | + * Copyright (c) 2025-present, NDLA. |
| 3 | + * |
| 4 | + * This source code is licensed under the GPLv3 license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +import { useCallback, useEffect, useMemo } from "react"; |
| 10 | +import { useTranslation } from "react-i18next"; |
| 11 | +import { gql, useQuery } from "@apollo/client"; |
| 12 | +import { CloseLine } from "@ndla/icons"; |
| 13 | +import { Button, Heading, Spinner } from "@ndla/primitives"; |
| 14 | +import { styled } from "@ndla/styled-system/jsx"; |
| 15 | +import { FilterContainer } from "./FilterContainer"; |
| 16 | +import { RESOURCE_NODE_TYPE } from "./searchUtils"; |
| 17 | +import { useStableSearchPageParams } from "./useStableSearchPageParams"; |
| 18 | +import { GQLGrepFilterQuery, GQLGrepFilterQueryVariables } from "../../graphqlTypes"; |
| 19 | + |
| 20 | +const FiltersWrapper = styled("div", { |
| 21 | + base: { |
| 22 | + display: "flex", |
| 23 | + gap: "small", |
| 24 | + flexWrap: "wrap", |
| 25 | + }, |
| 26 | +}); |
| 27 | + |
| 28 | +// const CompetenceWrapper = styled("div", { |
| 29 | +// base: { |
| 30 | +// display: "flex", |
| 31 | +// flexDirection: "column", |
| 32 | +// gap: "small", |
| 33 | +// }, |
| 34 | +// }); |
| 35 | +// const CompetenceItemWrapper = styled("div", { |
| 36 | +// base: { |
| 37 | +// display: "flex", |
| 38 | +// flexDirection: "column", |
| 39 | +// gap: "xxsmall", |
| 40 | +// }, |
| 41 | +// }); |
| 42 | + |
| 43 | +const grepFilterQuery = gql` |
| 44 | + query grepFilter($codes: [String!], $language: String!) { |
| 45 | + competenceGoals(codes: $codes, language: $language) { |
| 46 | + id |
| 47 | + title |
| 48 | + type |
| 49 | + curriculum { |
| 50 | + id |
| 51 | + title |
| 52 | + } |
| 53 | + competenceGoalSet { |
| 54 | + id |
| 55 | + title |
| 56 | + } |
| 57 | + } |
| 58 | + coreElements(codes: $codes, language: $language) { |
| 59 | + id |
| 60 | + title |
| 61 | + description |
| 62 | + } |
| 63 | + } |
| 64 | +`; |
| 65 | + |
| 66 | +export const GrepFilter = () => { |
| 67 | + const [searchParams, setSearchParams] = useStableSearchPageParams(); |
| 68 | + const { t, i18n } = useTranslation(); |
| 69 | + const nodeType = searchParams.get("type"); |
| 70 | + |
| 71 | + const codes = useMemo(() => searchParams.get("grepCodes")?.split(",") ?? [], [searchParams]); |
| 72 | + |
| 73 | + const grepQuery = useQuery<GQLGrepFilterQuery, GQLGrepFilterQueryVariables>(grepFilterQuery, { |
| 74 | + variables: { language: i18n.language, codes }, |
| 75 | + skip: !codes.length || (!!nodeType && nodeType !== RESOURCE_NODE_TYPE), |
| 76 | + }); |
| 77 | + |
| 78 | + useEffect(() => { |
| 79 | + if (nodeType && nodeType !== RESOURCE_NODE_TYPE && codes.length) { |
| 80 | + setSearchParams({ grepCodes: null }); |
| 81 | + } |
| 82 | + }, [codes.length, nodeType, setSearchParams]); |
| 83 | + |
| 84 | + // const groupedCompetenceGoals = useMemo(() => { |
| 85 | + // return groupCompetenceGoals(grepQuery.data?.competenceGoals ?? [], true, "LK20"); |
| 86 | + // }, [grepQuery.data?.competenceGoals]); |
| 87 | + // |
| 88 | + // const mappedCoreElements = useMemo(() => { |
| 89 | + // return ( |
| 90 | + // grepQuery.data?.coreElements?.map((element) => ({ |
| 91 | + // title: element.title, |
| 92 | + // text: element.description ?? "", |
| 93 | + // id: element.id, |
| 94 | + // url: "", |
| 95 | + // })) ?? [] |
| 96 | + // ); |
| 97 | + // }, [grepQuery.data?.coreElements]); |
| 98 | + |
| 99 | + const data = grepQuery.data ?? grepQuery.previousData; |
| 100 | + |
| 101 | + const grepElements = useMemo( |
| 102 | + () => [data?.competenceGoals, data?.coreElements].filter((arr) => !!arr).flat(), |
| 103 | + [data?.competenceGoals, data?.coreElements], |
| 104 | + ); |
| 105 | + |
| 106 | + const onRemoveCode = useCallback( |
| 107 | + (value: string) => { |
| 108 | + const newCodes = codes.filter((code) => code !== value); |
| 109 | + setSearchParams({ grepCodes: newCodes.join(",") }); |
| 110 | + }, |
| 111 | + [codes, setSearchParams], |
| 112 | + ); |
| 113 | + |
| 114 | + if (!grepQuery.loading && !grepQuery.data?.competenceGoals?.length && !grepQuery.data?.coreElements?.length) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + return ( |
| 119 | + <FilterContainer> |
| 120 | + <Heading asChild consumeCss textStyle="label.medium" fontWeight="bold"> |
| 121 | + <h3>{t("searchPage.grepFilter.heading")}</h3> |
| 122 | + </Heading> |
| 123 | + {/* <CompetenceWrapper> */} |
| 124 | + {/* {!!groupedCompetenceGoals?.length && ( */} |
| 125 | + {/* <CompetenceItemWrapper> */} |
| 126 | + {/* <Heading textStyle="title.large" asChild consumeCss> */} |
| 127 | + {/* <h4>{t("competenceGoals.competenceGoalItem.title")}</h4> */} |
| 128 | + {/* </Heading> */} |
| 129 | + {/* {groupedCompetenceGoals.map((goal, index) => ( */} |
| 130 | + {/* <CompetenceItem item={goal} key={index} /> */} |
| 131 | + {/* ))} */} |
| 132 | + {/* </CompetenceItemWrapper> */} |
| 133 | + {/* )} */} |
| 134 | + {/* {!!grepQuery.data?.coreElements?.length && ( */} |
| 135 | + {/* <CompetenceItemWrapper> */} |
| 136 | + {/* <Heading textStyle="title.large" asChild consumeCss> */} |
| 137 | + {/* <h2>{t("competenceGoals.competenceTabCorelabel")}</h2> */} |
| 138 | + {/* </Heading> */} |
| 139 | + {/* <CompetenceItem item={{ elements: mappedCoreElements }} /> */} |
| 140 | + {/* </CompetenceItemWrapper> */} |
| 141 | + {/* )} */} |
| 142 | + {/* </CompetenceWrapper> */} |
| 143 | + {!!grepQuery.loading && !grepQuery.previousData && <Spinner />} |
| 144 | + <FiltersWrapper> |
| 145 | + {codes.map((grep) => { |
| 146 | + const item = grepElements.find((g) => g.id === grep); |
| 147 | + if (!item) return null; |
| 148 | + return ( |
| 149 | + <Button |
| 150 | + key={item.id} |
| 151 | + size="small" |
| 152 | + variant="primary" |
| 153 | + onClick={() => onRemoveCode(item.id)} |
| 154 | + aria-label={t("searchPage.grepFilter.removeFilter", { code: item.id, title: item.title })} |
| 155 | + title={t("searchPage.grepFilter.removeFilter", { code: item.id, title: item.title })} |
| 156 | + > |
| 157 | + {item.id} |
| 158 | + {" - "} |
| 159 | + {item.title} |
| 160 | + <CloseLine /> |
| 161 | + </Button> |
| 162 | + ); |
| 163 | + })} |
| 164 | + </FiltersWrapper> |
| 165 | + </FilterContainer> |
| 166 | + ); |
| 167 | +}; |
0 commit comments