-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(list): use component files instead of array of enabled routes
- Loading branch information
Showing
7 changed files
with
63 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { FetchError, useDataEngine } from '@dhis2/app-runtime' | ||
import React from 'react' | ||
import { useQuery } from 'react-query' | ||
import { SectionListWrapper } from '../components' | ||
import { useModelListView } from '../components/sectionList/listView' | ||
import { | ||
useSchemaFromHandle, | ||
useParamsForDataQuery, | ||
BaseListModel, | ||
DEFAULT_FIELD_FILTERS, | ||
} from '../lib' | ||
import { getFieldFilter } from '../lib/models/path' | ||
import { WrapQueryResponse } from '../types' | ||
import { PagedResponse } from '../types/models' | ||
|
||
type ModelListResponse = WrapQueryResponse<PagedResponse<BaseListModel, string>> | ||
|
||
export const DefaultSectionList = () => { | ||
const { columns } = useModelListView() | ||
const schema = useSchemaFromHandle() | ||
const engine = useDataEngine() | ||
const modelListName = schema.plural | ||
|
||
const initialParams = useParamsForDataQuery() | ||
|
||
const query = { | ||
result: { | ||
resource: modelListName, | ||
params: { | ||
...initialParams, | ||
fields: columns | ||
.map((column) => getFieldFilter(schema, column.path)) | ||
.concat(DEFAULT_FIELD_FILTERS), | ||
}, | ||
}, | ||
} | ||
const { error, data } = useQuery({ | ||
queryKey: [query], | ||
queryFn: ({ queryKey: [query], signal }) => { | ||
return engine.query(query, { signal }) as Promise<ModelListResponse> | ||
}, | ||
}) | ||
const modelList = data?.result[modelListName] | ||
|
||
return ( | ||
<div> | ||
<SectionListWrapper | ||
error={error as FetchError | undefined} | ||
data={modelList} | ||
pager={data?.result.pager} | ||
// key={schema.name} | ||
/> | ||
</div> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { DefaultSectionList } from '../DefaultSectionList' | ||
|
||
export const Component = DefaultSectionList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { DefaultSectionList } from '../DefaultSectionList' | ||
|
||
export const Component = DefaultSectionList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,3 @@ | ||
import { useDataQuery } from '@dhis2/app-runtime' | ||
import { SharingDialog } from '@dhis2/ui' | ||
import React, { useEffect } from 'react' | ||
import { SectionListWrapper } from '../../components' | ||
import { useModelListView } from '../../components/sectionList/listView' | ||
import { | ||
useSchemaFromHandle, | ||
useParamsForDataQuery, | ||
DEFAULT_FIELD_FILTERS, | ||
DefaultFields, | ||
} from '../../lib/' | ||
import { getFieldFilter } from '../../lib/models/path' | ||
import { Query, WrapQueryResponse } from '../../types' | ||
import { DataElement, ModelCollectionResponse } from '../../types/models' | ||
import { DefaultSectionList } from '../DefaultSectionList' | ||
|
||
type FilteredDataElement = Pick<DataElement, DefaultFields> & | ||
Partial<DataElement> | ||
|
||
type DataElements = ModelCollectionResponse<FilteredDataElement, 'dataElements'> | ||
|
||
type DataElementsResponse = WrapQueryResponse<DataElements> | ||
|
||
const query: Query = { | ||
result: { | ||
resource: 'dataElements', | ||
params: (params) => params, | ||
}, | ||
} | ||
|
||
export const Component = () => { | ||
const { columns, query: listViewQuery } = useModelListView() | ||
const initialParams = useParamsForDataQuery() | ||
const schema = useSchemaFromHandle() | ||
const { refetch, error, data } = useDataQuery<DataElementsResponse>( | ||
query, | ||
// refetched on mount by effect below | ||
{ lazy: true } | ||
) | ||
|
||
useEffect(() => { | ||
// wait to fetch until selected-columns are loaded | ||
// so we dont fetch data multiple times | ||
if (listViewQuery.isLoading) { | ||
return | ||
} | ||
refetch({ | ||
...initialParams, | ||
fields: columns | ||
.map((column) => getFieldFilter(schema, column.path)) | ||
.concat(DEFAULT_FIELD_FILTERS), | ||
}) | ||
}, [refetch, initialParams, columns, listViewQuery.isLoading, schema]) | ||
|
||
return ( | ||
<div> | ||
<SectionListWrapper | ||
error={error} | ||
data={data?.result.dataElements} | ||
pager={data?.result.pager} | ||
/> | ||
</div> | ||
) | ||
} | ||
export const Component = DefaultSectionList |