diff --git a/src/components/sectionList/filters/filtersKeys.ts b/src/components/sectionList/filters/filtersKeys.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/src/components/sectionList/filters/index.ts b/src/components/sectionList/filters/index.ts index 01def766..40486a57 100644 --- a/src/components/sectionList/filters/index.ts +++ b/src/components/sectionList/filters/index.ts @@ -1,4 +1,3 @@ -export * from './useSectionListFilter' export * from './ConstantSelectionFilter' export * from './IdentifiableFilter' export * from './ConstantFilters' diff --git a/src/components/sectionList/filters/useSectionListFilter.ts b/src/components/sectionList/filters/useSectionListFilter.ts deleted file mode 100644 index b251aa84..00000000 --- a/src/components/sectionList/filters/useSectionListFilter.ts +++ /dev/null @@ -1,139 +0,0 @@ -import { useCallback, useMemo } from 'react' -import { useQueryParam, ObjectParam, UrlUpdateType } from 'use-query-params' -import { - Schema, - useSchemaFromHandle, - CustomObjectParam, - SectionListFilterObjectParamType, - getViewConfigForSection, - IDENTIFIABLE_KEY, -} from '../../../lib' -import { usePaginationQueryParams } from '../SectionListPagination' - -type ObjectParamType = typeof ObjectParam.default - -type Filters = Record - -const getVerifiedFiltersForSchema = ( - filters: ObjectParamType, - schema: Schema -): Filters => { - if (!filters) { - return {} - } - /* TODO: verify values for filters */ - const relevantFilters = Object.entries(filters).filter(([key]) => { - return key === IDENTIFIABLE_KEY || schema.properties[key] - }) - return Object.fromEntries(relevantFilters) -} - -const getRelevantFiltersForSchema = ( - filters: SectionListFilterObjectParamType, - schema: Schema -): Filters => { - if (!filters) { - return {} - } - const viewConfig = getViewConfigForSection(schema.singular) - const relevantFilterKeys = - viewConfig.filters.available.concat('identifiable') - /* TODO: verify values for filters */ - const relevantFilters = Object.entries(filters).filter(([key]) => { - return key === IDENTIFIABLE_KEY || schema.properties[key] - }) - return Object.fromEntries(relevantFilters) -} - -const useFilterQueryParam = () => { - return useQueryParam('filter', CustomObjectParam) -} - -export const useSectionListFilters = () => { - const [filter, setFilterParam] = useFilterQueryParam() - const [, setPagingParam] = usePaginationQueryParams() - - const schema = useSchemaFromHandle() - - // override setFilter to be able to reset Page when filter changes - const setFilter = useCallback( - ( - filter: Parameters[0], - updateType?: UrlUpdateType - ) => { - setFilterParam(filter, updateType) - // set page to 1 when filter changes - // do this here instead of useEffect to prevent unnecessary refetches - setPagingParam((pagingParams) => ({ ...pagingParams, page: 1 })) - }, - [setFilterParam, setPagingParam] - ) - - return useMemo( - () => [getVerifiedFiltersForSchema(filter, schema), setFilter] as const, - [filter, schema, setFilter] - ) -} - -/** Helper-hook to select a single filter. - * eg. [domainType, setDomainType] = useSectionListFilter('domainType') - */ -export const useSectionListFilter = ( - filterKey: string -): [string | undefined, (value: string | undefined) => void] => { - const [filters, setFilters] = useSectionListFilters() - - const boundSetFilter = useCallback( - (value: string | undefined) => { - if (!value) { - setFilters((filters) => { - if (!filters) { - return undefined - } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { [filterKey]: _, ...rest } = filters - return Object.keys(rest).length === 0 ? undefined : rest - }) - } else { - setFilters((filters) => ({ ...filters, [filterKey]: value })) - } - }, - [filterKey, setFilters] - ) - - return [filters?.[filterKey] ?? undefined, boundSetFilter] -} - -const parseToQueryFilter = (filters: Filters): string[] => { - const { [IDENTIFIABLE_KEY]: identifiableValue, ...restFilters } = filters - const queryFilters: string[] = [] - - const identifiableFilter = `identifiable:token:${identifiableValue}` - if (identifiableValue) { - queryFilters.push(identifiableFilter) - } - Object.entries(restFilters).forEach(([key, value]) => { - queryFilters.push(`${key}:eq:${value}`) - }) - return queryFilters -} - -export const useSectionListQueryFilter = () => { - const [filters] = useSectionListFilters() - - return useMemo(() => { - return parseToQueryFilter(filters) - }, [filters]) -} - -export const useQueryParamsForModelGist = () => { - const [paginationParams] = usePaginationQueryParams() - const filterParams = useSectionListQueryFilter() - - return useMemo(() => { - return { - ...paginationParams, - filter: filterParams, - } - }, [paginationParams, filterParams]) -} diff --git a/src/lib/constants/sectionListView/sectionListViewFilterKeys.ts b/src/lib/constants/sectionListView/sectionListViewFilterKeys.ts deleted file mode 100644 index e0c8b779..00000000 --- a/src/lib/constants/sectionListView/sectionListViewFilterKeys.ts +++ /dev/null @@ -1,25 +0,0 @@ -// special key for handling search for identifiable objects -// eg. searches for name, code, id and shortname -// this would translate to "token" in the old API, but does not exist in GIST-API -export const IDENTIFIABLE_KEY = 'identifiable' - -/* Allowed "keys" to filter by - Used to specify the allowed filters in the query-Params as well - as mapping to the correct filter component */ -export const validFilterKeys = [ - IDENTIFIABLE_KEY, - 'domainType', - 'valueType', - 'dataSet', - 'categoryCombo', -] as const - -const filterKeysSet = new Set(validFilterKeys) - -export type FilterKeys = typeof validFilterKeys -//export const filterKeys = Object.keys(filterKeys) - -export type FilterKey = FilterKeys[number] - -// Identifiable is not configurable, and is always shown in the list -export type ConfigurableFilterKey = Exclude diff --git a/src/lib/routeUtils/CustomQueryParam.ts b/src/lib/routeUtils/CustomQueryParam.ts deleted file mode 100644 index 8cc9bf91..00000000 --- a/src/lib/routeUtils/CustomQueryParam.ts +++ /dev/null @@ -1,34 +0,0 @@ -import pick from 'lodash/pick' -import { - encodeObject, - decodeObject, - ObjectParam, - QueryParamConfig, -} from 'use-query-params' -import { - FilterKey, - validFilterKeys, -} from '../constants/sectionListView/sectionListViewFilterKeys' -const entrySeparator = '~' // default is "_" which breaks constants (delimited by _) - -export const CustomObjectParam: typeof ObjectParam = { - encode: (obj) => encodeObject(obj, undefined, entrySeparator), - - decode: (str) => decodeObject(str, undefined, entrySeparator), -} - -type FilterObject = { [key in FilterKey]?: string } | undefined | null -export type SectionListFilterObjectParamType = QueryParamConfig - -export const SectionListFilterObjectParam: SectionListFilterObjectParamType = { - encode: (obj) => { - const filteredObj = pick(obj, validFilterKeys) - - return encodeObject(filteredObj, undefined, entrySeparator) - }, - - decode: (str) => { - const decoded = decodeObject(str, undefined, entrySeparator) - return pick(decoded, validFilterKeys) - }, -} diff --git a/src/lib/routeUtils/index.ts b/src/lib/routeUtils/index.ts index 4fd6ade0..f22e38c6 100644 --- a/src/lib/routeUtils/index.ts +++ b/src/lib/routeUtils/index.ts @@ -1,4 +1,3 @@ -export * from './CustomQueryParam' export { routePaths, getOverviewPath,