From 5b2a80a90902f547d20f4cf337f75b3d1fb4860c Mon Sep 17 00:00:00 2001 From: winzlieb Date: Wed, 9 Mar 2022 21:58:51 +0100 Subject: [PATCH] fixing some type errors --- .../search/components/QueryClientProvider.tsx | 2 +- .../components/ngo/HostOfferLookupTable.tsx | 31 ++++++++++++------- .../components/ngo/HostOfferLookupWrapper.tsx | 9 +++--- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/frontend/search/components/QueryClientProvider.tsx b/frontend/search/components/QueryClientProvider.tsx index 513693c..18e5cb1 100644 --- a/frontend/search/components/QueryClientProvider.tsx +++ b/frontend/search/components/QueryClientProvider.tsx @@ -4,7 +4,7 @@ import {ReactQueryDevtools} from 'react-query/devtools' const queryClient = new QueryClient() -export default function MyQueryClientProvider({children}: {children: React.Component}) { +export default function MyQueryClientProvider({children}: {children: React.ReactChild}) { return ( {children} diff --git a/frontend/search/components/ngo/HostOfferLookupTable.tsx b/frontend/search/components/ngo/HostOfferLookupTable.tsx index cbe6656..a976a28 100644 --- a/frontend/search/components/ngo/HostOfferLookupTable.tsx +++ b/frontend/search/components/ngo/HostOfferLookupTable.tsx @@ -1,4 +1,4 @@ -import React from 'react' +import React, {ReactNode} from 'react' import { CheckBoxOutlineBlank, CheckBox } from '@mui/icons-material'; @@ -21,6 +21,16 @@ type HostOfferLookupTableProps = { type ColumnRaw = { name: string; header: string; type: string } +/** + * you can generate an inital raw column json by running the following + * function + */ +const makeColumnDefinition = (data: any ) => Object.keys(data) + .map(k => ({ + name: k, + header: k.replace(/_/g, ' '), + type: typeof data[k]})) + const columnsRaw: ColumnRaw[] = [ { "name": "beds", @@ -117,14 +127,17 @@ const operatorsForType = { boolean: 'eq' } -const customRendererForType = [ +type CustomRendererMatcher = { + match: { [key: string]: any } + render: (...args: any[]) => ReactNode +} + +const customRendererForType: CustomRendererMatcher[] = [ { - id: 'bool2checkbox', match: {type: 'boolean'}, render: ({ value }) => !!value ? : }, { - id: 'email', match: {type: 'string', name: 'contact_email'}, render: ({ value }) => ({value}) } @@ -135,14 +148,13 @@ const findMatchingRenderer = (c: ColumnRaw) => { // @ts-ignore return Object.keys(d.match).reduce((prev, cur) => prev && c[cur] === d.match[cur] , true ) }) - console.log(customRenderer) return customRenderer?.render } const columns: TypeColumn[] = columnsRaw .map(c => ({ ...c, - render: findMatchingRenderer(c) || null, + render: findMatchingRenderer(c) || undefined, filterEditor: filterMappings[c.type as 'string' | 'number' | 'boolean' | 'date'] })) @@ -157,12 +169,6 @@ const defaultFilterValue: TypeFilterValue = columns } as unknown as TypeSingleFilterValue } ) -const makeColumnDefinition = (data: any ) => Object.keys(data) - .map(k => ({ - name: k, - header: k.replace(/_/g, ' '), - type: typeof data[k]})) - const HostOfferLookupTable = ({ data }: HostOfferLookupTableProps) => { const dataSource = data.get_offers || [] return <> @@ -174,6 +180,7 @@ const HostOfferLookupTable = ({ data }: HostOfferLookupTableProps) => { defaultFilterValue={defaultFilterValue} rowIndexColumn enableSelection + enableColumnAutosize={false} columns={columns} dataSource={dataSource} style={{minHeight: '1000px'}} diff --git a/frontend/search/components/ngo/HostOfferLookupWrapper.tsx b/frontend/search/components/ngo/HostOfferLookupWrapper.tsx index dafbb7f..0d419a1 100644 --- a/frontend/search/components/ngo/HostOfferLookupWrapper.tsx +++ b/frontend/search/components/ngo/HostOfferLookupWrapper.tsx @@ -7,11 +7,10 @@ type HostLookupWrapperProps = Record const HostOfferLookupWrapper = ({}: HostLookupWrapperProps) => { const {data, isFetching} = useGetOffersQuery({auth: testAuth as Auth}, {staleTime: 60 * 1000}) - if (isFetching) { - return "loading…" - } else if (data?.get_offers) { - return () - } + + return <>{ + (!isFetching && data?.get_offers) ? : "loading…" + } } export default HostOfferLookupWrapper