From af5b5d61ca3a2aae8f4f1fe26f14c81fd4ea58e7 Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Mon, 27 Nov 2023 16:28:37 +0100 Subject: [PATCH] refactor: remove useModelGist --- .../sectionList/SectionListPagination.tsx | 21 +- .../filters/useSectionListFilter.ts | 22 +-- src/lib/models/index.ts | 7 - src/lib/models/useModelGist.ts | 187 ------------------ 4 files changed, 10 insertions(+), 227 deletions(-) delete mode 100644 src/lib/models/useModelGist.ts diff --git a/src/components/sectionList/SectionListPagination.tsx b/src/components/sectionList/SectionListPagination.tsx index 73814723..fe179405 100644 --- a/src/components/sectionList/SectionListPagination.tsx +++ b/src/components/sectionList/SectionListPagination.tsx @@ -5,14 +5,13 @@ import { NumericObjectParam, withDefault, } from 'use-query-params' -import { GistPaginator } from '../../lib/' import { Pager } from '../../types/generated' type SectionListPaginationProps = { pager: Pager | undefined } -type PaginationQueryParams = { +export type PaginationQueryParams = { page: number pageSize: number } @@ -67,16 +66,15 @@ const validatePagerParams = ( } } -function useUpdatePaginationParams(pager?: Pager): GistPaginator { - const [, setParams] = usePaginationQueryParams() +type Paginator = { + changePageSize: (pageSize: number) => boolean + getPrevPage: () => boolean + goToPage: (page: number) => boolean + pager?: Pager +} - const getNextPage = useCallback(() => { - if (!pager?.nextPage) { - return false - } - setParams((prevPager) => ({ ...prevPager, page: pager.page + 1 })) - return true - }, [pager, setParams]) +function useUpdatePaginationParams(pager?: Pager): Paginator { + const [, setParams] = usePaginationQueryParams() const getPrevPage = useCallback(() => { if (!pager?.prevPage) { @@ -106,7 +104,6 @@ function useUpdatePaginationParams(pager?: Pager): GistPaginator { ) return { - getNextPage, getPrevPage, goToPage, changePageSize, diff --git a/src/components/sectionList/filters/useSectionListFilter.ts b/src/components/sectionList/filters/useSectionListFilter.ts index b284d5d0..088d4c1c 100644 --- a/src/components/sectionList/filters/useSectionListFilter.ts +++ b/src/components/sectionList/filters/useSectionListFilter.ts @@ -1,11 +1,6 @@ import { useCallback, useMemo } from 'react' import { useQueryParam, ObjectParam, UrlUpdateType } from 'use-query-params' -import { - Schema, - useSchemaFromHandle, - CustomObjectParam, - GistParams, -} from '../../../lib' +import { Schema, useSchemaFromHandle, CustomObjectParam } from '../../../lib' import { usePaginationQueryParams } from '../SectionListPagination' type ObjectParamType = typeof ObjectParam.default @@ -17,21 +12,6 @@ type Filters = Record // this would translate to "token" in the old API, but does not exist in GIST-API export const IDENTIFIABLE_KEY = 'identifiable' -const IDENTIFIABLE_FIELDS = { - name: { - operator: 'ilike', - }, - code: { - operator: 'ilike', - }, - shortName: { - operator: 'ilike', - }, - id: { - operator: 'eq', - }, -} - const getVerifiedFiltersForSchema = ( filters: ObjectParamType, schema: Schema diff --git a/src/lib/models/index.ts b/src/lib/models/index.ts index bcdb99fd..725e338c 100644 --- a/src/lib/models/index.ts +++ b/src/lib/models/index.ts @@ -1,10 +1,3 @@ -export { useModelGist } from './useModelGist' -export type { - GistPaginator, - GistParams, - UseModelGistResultPaginated, - UseModelGistResult, -} from './useModelGist' export { isValidUid } from './uid' export { parsePublicAccessString } from './parsePublicAccess' export { getIn, stringToPathArray, getFieldFilterFromPath } from './path' diff --git a/src/lib/models/useModelGist.ts b/src/lib/models/useModelGist.ts deleted file mode 100644 index cd6b3453..00000000 --- a/src/lib/models/useModelGist.ts +++ /dev/null @@ -1,187 +0,0 @@ -import { useDataQuery } from '@dhis2/app-runtime' -import React, { useState, useCallback } from 'react' -import { - GistParams as BaseGistParams, - IdentifiableObject, - GistResponse, - GistPager, - GistCollectionResponse, - GistPagedResponse, - GistObjectResponse, -} from '../../types/models' -import { QueryResponse, Query } from '../../types/query' - -// these normally are just strings, but useQuery supports arrays -export type GistParams = Omit & { - fields?: string | string[] - filter?: string | string[] - pageSize?: number -} - -type GistResourceString = `${string}/gist` -type ResourceQuery = Query[number] -type GistResourceQuery = Omit & { - resource: GistResourceString -} - -// note that we do not support parallel queries -// this makes it significantly easier to implement -// and parallel use of gist queries shouldn't be a common use case -type GistQuery = { - result: GistResourceQuery -} - -type GistQueryResult = { - result: Response -} - -function createGistQuery( - resource: GistResourceString, - params?: GistParams -): GistQuery { - return { - result: { - resource: `${resource}`, - params: ({ ...dynamicParams }) => ({ - pageListName: 'result', - total: true, - order: 'name:ASC', - ...params, - ...dynamicParams, - }), - }, - } -} - -function usePagination( - refetch: QueryResponse['refetch'], - data?: unknown -): GistPaginator | null { - let pager: GistPager | undefined - - if (isDataCollection(data)) { - pager = data.pager - } - const getNextPage = useCallback(() => { - if (!pager?.nextPage) { - return false - } - refetch({ page: pager.page + 1 }) - return true - }, [refetch, pager]) - - const getPrevPage = useCallback(() => { - if (!pager?.prevPage) { - return false - } - refetch({ page: pager.page - 1 }) - return true - }, [refetch, pager]) - - const goToPage = useCallback( - (page: number) => { - if (!pager?.pageCount || page > pager.pageCount) { - return false - } - refetch({ page: page }) - return true - }, - [refetch, pager] - ) - - const changePageSize = useCallback( - (pageSize: number) => { - refetch({ pageSize: pageSize }) - return true - }, - [refetch] - ) - - return { - getNextPage, - getPrevPage, - goToPage, - changePageSize, - pager, - } -} - -export type GistPaginator = { - changePageSize: (pageSize: number) => boolean - getNextPage: () => boolean - getPrevPage: () => boolean - goToPage: (page: number) => boolean - pager?: GistPager -} - -type BaseUseModelGistResult = Pick< - QueryResponse, - 'loading' | 'error' | 'called' | 'refetch' -> & { - data?: Response -} - -export type UseModelGistResultPaginated = - BaseUseModelGistResult & { - pagination: GistPaginator - } -export type UseModelGistResult = - | BaseUseModelGistResult - | UseModelGistResultPaginated - -export const isDataCollection = ( - data: unknown -): data is GistCollectionResponse => { - // gist endpoints are always paged if they're collections - return (data as GistCollectionResponse)?.pager !== undefined -} - -type UseDataQueryOptions = Parameters[1] -export function useModelGist( - gistResource: GistResourceString, - params?: GistParams, - dataQueryOptions?: UseDataQueryOptions -): UseModelGistResultPaginated - -export function useModelGist( - gistResource: GistResourceString, - params?: GistParams, - dataQueryOptions?: UseDataQueryOptions -): UseModelGistResult - -export function useModelGist( - gistResource: GistResourceString, - params?: GistParams, - dataQueryOptions?: Parameters[1] -): UseModelGistResult { - const [gistQuery] = useState( - createGistQuery(gistResource, params) - ) - const queryResponse = useDataQuery>( - gistQuery, - dataQueryOptions - ) - - const pagination = usePagination( - queryResponse.refetch, - queryResponse.data?.result - ) - - return React.useMemo(() => { - const baseResult: UseModelGistResult = { - loading: queryResponse.loading, - called: queryResponse.called, - error: queryResponse.error, - data: queryResponse.data?.result, - refetch: queryResponse.refetch, - } - if (pagination) { - const result: UseModelGistResultPaginated = { - ...baseResult, - pagination, - } - return result - } - return baseResult - }, [queryResponse, pagination]) -}