From a1f7337b146ee01a38a8c62ea3b6ccc8834e02ed Mon Sep 17 00:00:00 2001 From: Mohammer5 Date: Thu, 11 Apr 2024 13:07:15 +0800 Subject: [PATCH] refactor(section list view config): make config static --- i18n/en.pot | 29 ++-- .../sectionList/ColumnHeaderSortable.tsx | 12 +- src/components/sectionList/SectionList.tsx | 17 ++- src/components/sectionList/SectionListRow.tsx | 9 +- .../sectionList/SectionListWrapper.tsx | 6 +- .../sectionList/filters/DynamicFilters.tsx | 5 +- .../sectionList/filters/useFilterKeys.tsx | 5 +- .../sectionList/listView/ManageListView.tsx | 32 ++-- src/components/sectionList/listView/types.ts | 5 +- .../sectionList/listView/useModelListView.tsx | 27 ++-- src/components/sectionList/types.ts | 5 +- .../constants/translatedModelProperties.ts | 2 + src/lib/sectionList/index.ts | 2 +- src/lib/sectionList/listViews/index.ts | 2 - .../listViews/sectionListViewsConfig.ts | 100 ------------ .../listViews/viewConfigResolver.ts | 144 ------------------ src/lib/sectionList/sectionListViewsConfig.ts | 86 +++++++++++ src/pages/dataElements/List.tsx | 3 +- 18 files changed, 150 insertions(+), 341 deletions(-) delete mode 100644 src/lib/sectionList/listViews/index.ts delete mode 100644 src/lib/sectionList/listViews/sectionListViewsConfig.ts delete mode 100644 src/lib/sectionList/listViews/viewConfigResolver.ts create mode 100644 src/lib/sectionList/sectionListViewsConfig.ts diff --git a/i18n/en.pot b/i18n/en.pot index 7b74c9db..2661dad0 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -5,8 +5,8 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" -"POT-Creation-Date: 2024-03-20T01:08:59.818Z\n" -"PO-Revision-Date: 2024-03-20T01:08:59.818Z\n" +"POT-Creation-Date: 2024-04-10T10:01:54.632Z\n" +"PO-Revision-Date: 2024-04-10T10:01:54.632Z\n" msgid "schemas" msgstr "schemas" @@ -75,6 +75,12 @@ msgstr "Save and close" msgid "" msgstr "" +msgid "Custom attributes" +msgstr "Custom attributes" + +msgid "Custom fields for your DHIS2 instance" +msgstr "Custom fields for your DHIS2 instance" + msgid "Code" msgstr "Code" @@ -759,9 +765,6 @@ msgstr "This field requires a unique value, please choose another one" msgid "Required" msgstr "Required" -msgid "Custom attributes" -msgstr "Custom attributes" - msgid "Exit without saving" msgstr "Exit without saving" @@ -792,21 +795,9 @@ msgstr "Set up the information for this data element group" msgid "Explain the purpose of this data element group." msgstr "Explain the purpose of this data element group." -msgid "@TODO" -msgstr "@TODO" - -msgid "Custom fields for your DHIS2 instance" -msgstr "Custom fields for your DHIS2 instance" - msgid "Selected data elements" msgstr "Selected data elements" -msgid "Explain the purpose of this data element and how it's measured." -msgstr "Explain the purpose of this data element and how it's measured." - -msgid "A data element name should be concise and easy to recognize." -msgstr "A data element name should be concise and easy to recognize." - msgid "Create data element" msgstr "Create data element" @@ -896,8 +887,8 @@ msgstr "Disaggregation and Option sets" msgid "Set up disaggregation and predefined options." msgstr "Set up disaggregation and predefined options." -msgid "LegendSet" -msgstr "LegendSet" +msgid "Legend set" +msgstr "Legend set" msgid "" "Visualize values for this data element in Analytics app. Multiple legendSet " diff --git a/src/components/sectionList/ColumnHeaderSortable.tsx b/src/components/sectionList/ColumnHeaderSortable.tsx index f55f68e3..c6301c32 100644 --- a/src/components/sectionList/ColumnHeaderSortable.tsx +++ b/src/components/sectionList/ColumnHeaderSortable.tsx @@ -5,6 +5,7 @@ import { isValidSortPathForSchema, useSchema, useSectionListSortOrder, + getTranslatedProperty, } from '../../lib' import { SelectedColumn, SelectedColumns } from './types' @@ -38,8 +39,7 @@ export const HeaderColumnsSortable = ({ ) const getDataTableSortDirection = (column: SelectedColumn) => { - const allowSort = - column && isValidSortPathForSchema(schema, column.path) + const allowSort = column && isValidSortPathForSchema(schema, column) if (!allowSort) { return undefined @@ -48,7 +48,7 @@ export const HeaderColumnsSortable = ({ return 'default' } const [sortedColumn, sortedDirection] = sortOrder - const columnIsSorted = sortedColumn === column.path + const columnIsSorted = sortedColumn === column return columnIsSorted ? sortedDirection : 'default' } @@ -58,10 +58,10 @@ export const HeaderColumnsSortable = ({ - {headerColumn.label} + {getTranslatedProperty(headerColumn)} ))} diff --git a/src/components/sectionList/SectionList.tsx b/src/components/sectionList/SectionList.tsx index 1abb07e7..2ae220e1 100644 --- a/src/components/sectionList/SectionList.tsx +++ b/src/components/sectionList/SectionList.tsx @@ -8,24 +8,29 @@ import { Checkbox, } from '@dhis2/ui' import React, { PropsWithChildren } from 'react' -import { isSchemaSection, useSectionHandle } from '../../lib' +import { + isSchemaSection, + useSectionHandle, + getTranslatedProperty, +} from '../../lib' import { CheckBoxOnChangeObject } from '../../types' import { HeaderColumnsSortable } from './ColumnHeaderSortable' +import { useModelListView } from './listView' import css from './SectionList.module.css' import { SelectedColumns } from './types' type SectionListProps = { - headerColumns: SelectedColumns onSelectAll: (checked: boolean) => void allSelected?: boolean } export const SectionList = ({ allSelected, - headerColumns, children, onSelectAll, }: PropsWithChildren) => { + const { columns: headerColumns } = useModelListView() + return ( @@ -66,9 +71,9 @@ const HeaderColumns = ({ headerColumns={headerColumns} /> ) : ( - headerColumns.map((headerColumn) => ( - - {headerColumn.label} + headerColumns.map((column) => ( + + {getTranslatedProperty(column)} )) )} diff --git a/src/components/sectionList/SectionListRow.tsx b/src/components/sectionList/SectionListRow.tsx index 4867dbd5..b94807af 100644 --- a/src/components/sectionList/SectionListRow.tsx +++ b/src/components/sectionList/SectionListRow.tsx @@ -5,12 +5,12 @@ import { BaseListModel, TOOLTIPS } from '../../lib' import { canEditModel } from '../../lib/models/access' import { CheckBoxOnChangeObject } from '../../types' import { TooltipWrapper } from '../tooltip' +import { useModelListView } from './listView' import css from './SectionList.module.css' -import { SelectedColumns, SelectedColumn } from './types' +import { SelectedColumn } from './types' export type SectionListRowProps = { modelData: Model - selectedColumns: SelectedColumns onSelect: (modelId: string, checked: boolean) => void selected: boolean renderActions: (model: Model) => React.ReactNode @@ -26,7 +26,6 @@ export const SectionListRow = React.memo(function SectionListRow< Model extends BaseListModel >({ active, - selectedColumns, modelData, onSelect, onClick, @@ -35,6 +34,8 @@ export const SectionListRow = React.memo(function SectionListRow< renderColumnValue, }: SectionListRowProps) { const editAccess = canEditModel(modelData) + const { columns: selectedColumns } = useModelListView() + return ( {selectedColumns.map((selectedColumn) => ( onClick?.(modelData)} > {renderColumnValue(selectedColumn, modelData)} diff --git a/src/components/sectionList/SectionListWrapper.tsx b/src/components/sectionList/SectionListWrapper.tsx index 80cd5069..2c266885 100644 --- a/src/components/sectionList/SectionListWrapper.tsx +++ b/src/components/sectionList/SectionListWrapper.tsx @@ -7,7 +7,6 @@ import { SectionListHeaderBulk } from './bulk' import { DetailsPanel, DefaultDetailsPanelContent } from './detailsPanel' import { FilterWrapper } from './filters/FilterWrapper' import { DefaultListActions } from './listActions' -import { useModelListView } from './listView' import { ModelValue } from './modelValue/ModelValue' import { SectionList } from './SectionList' import css from './SectionList.module.css' @@ -33,7 +32,6 @@ export const SectionListWrapper = ({ pager, refetch, }: SectionListWrapperProps) => { - const { columns: headerColumns } = useModelListView() const schema = useSchemaFromHandle() const { selectedModels, checkAllSelected, add, remove, toggle, clearAll } = useSelectedModels() @@ -85,7 +83,7 @@ export const SectionListWrapper = ({ /* Note that SectionListRow is memoed, to prevent re-rendering every item when interacting with a row */ const renderColumnValue = useCallback( - ({ path }: SelectedColumn, model: BaseListModel) => { + (path: SelectedColumn, model: BaseListModel) => { return ( ) @@ -121,7 +119,6 @@ export const SectionListWrapper = ({ )} @@ -130,7 +127,6 @@ export const SectionListWrapper = ({ > - -const filterKeyToComponentMap: FilterKeyToComponentMap = { +const filterKeyToComponentMap: Record = { categoryCombo: CategoryComboFilter, dataSet: DataSetFilter, domainType: DomainTypeSelectionFilter, diff --git a/src/components/sectionList/filters/useFilterKeys.tsx b/src/components/sectionList/filters/useFilterKeys.tsx index ab7425ed..001ea265 100644 --- a/src/components/sectionList/filters/useFilterKeys.tsx +++ b/src/components/sectionList/filters/useFilterKeys.tsx @@ -15,17 +15,16 @@ export const useFilterKeys = () => { // combine filters and views, since filters in URL might not be selected for view // but we should show them when they have a value const filterKeys = useMemo(() => { - const viewFilterKeys = viewFilters.map(({ filterKey }) => filterKey) const selectedFiltersNotInView = Object.entries(filters) .filter( ([filterKey, value]) => value !== undefined && filterKey !== IDENTIFIABLE_FILTER_KEY && - !viewFilterKeys.includes(filterKey as ConfigurableFilterKey) + !viewFilters.includes(filterKey as ConfigurableFilterKey) ) .map(([filterKey]) => filterKey) as ConfigurableFilterKey[] - return viewFilterKeys.concat(selectedFiltersNotInView) + return viewFilters.concat(selectedFiltersNotInView) }, [filters, viewFilters]) return filterKeys } diff --git a/src/components/sectionList/listView/ManageListView.tsx b/src/components/sectionList/listView/ManageListView.tsx index 7267f2fc..ad11b4bc 100644 --- a/src/components/sectionList/listView/ManageListView.tsx +++ b/src/components/sectionList/listView/ManageListView.tsx @@ -16,6 +16,7 @@ import { getColumnsForSection, getFiltersForSection, useModelSectionHandleOrThrow, + getTranslatedProperty, } from '../../../lib' import css from './ManageListView.module.css' import { useModelListView, useMutateModelListViews } from './useModelListView' @@ -29,10 +30,6 @@ type ManageColumnsDialogProps = { children: (props: RenderProps) => React.ReactNode } -const toPath = (propertyDescriptor: { path: string }) => propertyDescriptor.path -const toFilterKey = (filterDescriptor: { filterKey: string }) => - filterDescriptor.filterKey - type FormValues = { columns: string[] filters: string[] @@ -49,6 +46,7 @@ const validate = (values: FormValues) => { } return errors } + export const ManageListView = ({ onSaved, children, @@ -67,8 +65,8 @@ export const ManageListView = ({ const columnsConfig = getColumnsForSection(section.name) const filtersConfig = getFiltersForSection(section.name) - const defaultColumns = columnsConfig.default.map(toPath) - const defaultFilters = filtersConfig.default.map(toFilterKey) + const defaultColumns = columnsConfig.default + const defaultFilters = filtersConfig.default const handleSave = async (values: FormValues) => { const isDefault = (arr: string[], def: string[]) => @@ -102,14 +100,8 @@ export const ManageListView = ({ const initialValues = useMemo(() => { return { - columns: - savedColumns.length > 0 - ? savedColumns.map(toPath) - : defaultColumns, - filters: - savedFilters.length > 0 - ? savedFilters.map(toFilterKey) - : defaultFilters, + columns: savedColumns.length > 0 ? savedColumns : defaultColumns, + filters: savedFilters.length > 0 ? savedFilters : defaultFilters, } }, [savedFilters, savedColumns, defaultColumns, defaultFilters]) @@ -149,9 +141,9 @@ export const ManageListView = ({ loading={query.isLoading} defaultOptions={defaultColumns} availableOptions={columnsConfig.available.map( - (c) => ({ - label: c.label, - value: c.path, + (path) => ({ + label: getTranslatedProperty(path), + value: path, }) )} /> @@ -164,9 +156,9 @@ export const ManageListView = ({ loading={query.isLoading} defaultOptions={defaultFilters} availableOptions={filtersConfig.available.map( - (f) => ({ - label: f.label, - value: f.filterKey, + (filterKey) => ({ + label: getTranslatedProperty(filterKey), + value: filterKey, }) )} /> diff --git a/src/components/sectionList/listView/types.ts b/src/components/sectionList/listView/types.ts index 10e0b5b2..f465e036 100644 --- a/src/components/sectionList/listView/types.ts +++ b/src/components/sectionList/listView/types.ts @@ -1,11 +1,10 @@ import { SectionName } from '../../../lib' -import type { FilterDescriptor, ModelPropertyDescriptor } from '../../../lib' export interface ModelListView { name: string sectionModel: string - columns: ReadonlyArray - filters: ReadonlyArray + columns: string[] + filters: string[] } export type ModelListViews = { diff --git a/src/components/sectionList/listView/useModelListView.tsx b/src/components/sectionList/listView/useModelListView.tsx index 6f89f414..aaebb47a 100644 --- a/src/components/sectionList/listView/useModelListView.tsx +++ b/src/components/sectionList/listView/useModelListView.tsx @@ -66,22 +66,13 @@ const parseViewToModelListView = ( const parsedView = listView.data - // map to config to make sure we don't use invalid columns - // Preserve order by mapping from parsedView to config-object - const columns = parsedView.columns.flatMap((path) => { - const columnConfig = viewConfig.columns.available.find( - (col) => col.path === path - ) - return columnConfig ? [columnConfig] : [] - }) - - const filters = parsedView.filters.flatMap((filterKey) => { - const filterConfig = viewConfig.filters.available.find( - (filter) => filter.filterKey === filterKey - ) + const columns = parsedView.columns.filter((path) => + viewConfig.columns.available.includes(path) + ) - return filterConfig ? [filterConfig] : [] - }) + const filters = parsedView.filters.filter((filterKey) => + viewConfig.filters.available.includes(filterKey) + ) return { ...parsedView, @@ -95,8 +86,8 @@ const formatViewToDataStore = ( ): z.infer => { const savedView = { ...view, - columns: view.columns.map((c) => c.path), - filters: view.filters.map((f) => f.filterKey), + columns: view.columns, + filters: view.filters, } return savedView @@ -114,7 +105,7 @@ const createValidViewSelect = (sectionName: string) => { return getDefaultViewForSection(sectionName) } - const viewForSection = modelListViews.data[sectionName][0] + const viewForSection = modelListViews.data[sectionName]?.[0] if (!viewForSection) { return getDefaultViewForSection(sectionName) } diff --git a/src/components/sectionList/types.ts b/src/components/sectionList/types.ts index d3b33099..6571e071 100644 --- a/src/components/sectionList/types.ts +++ b/src/components/sectionList/types.ts @@ -1,7 +1,4 @@ -export type SelectedColumn = { - label: string - path: string -} +export type SelectedColumn = string export type SelectedColumns = ReadonlyArray diff --git a/src/lib/constants/translatedModelProperties.ts b/src/lib/constants/translatedModelProperties.ts index e9ee6d00..d77f3238 100644 --- a/src/lib/constants/translatedModelProperties.ts +++ b/src/lib/constants/translatedModelProperties.ts @@ -18,6 +18,8 @@ const TRANSLATED_PROPERTY: Record = { valueType: i18n.t('Value type'), user: i18n.t('Owner'), // user refers to the owner of the object zeroIsSignificant: i18n.t('Zero is significant'), + 'sharing.public': i18n.t('Public access'), + publicAccess: i18n.t('Public access'), } const camelCaseToSentenceCase = (camelCase: string) => diff --git a/src/lib/sectionList/index.ts b/src/lib/sectionList/index.ts index 98350236..190f2892 100644 --- a/src/lib/sectionList/index.ts +++ b/src/lib/sectionList/index.ts @@ -5,6 +5,6 @@ export { useUpdatePaginationParams, } from './usePaginationParams' export * from './useParamsForDataQuery' -export * from './listViews' +export * from './sectionListViewsConfig' export * from './useSectionListSortOrder' export * from './fieldFilters' diff --git a/src/lib/sectionList/listViews/index.ts b/src/lib/sectionList/listViews/index.ts deleted file mode 100644 index e59552fb..00000000 --- a/src/lib/sectionList/listViews/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './viewConfigResolver' -export * from './sectionListViewsConfig' diff --git a/src/lib/sectionList/listViews/sectionListViewsConfig.ts b/src/lib/sectionList/listViews/sectionListViewsConfig.ts deleted file mode 100644 index 0a72a568..00000000 --- a/src/lib/sectionList/listViews/sectionListViewsConfig.ts +++ /dev/null @@ -1,100 +0,0 @@ -import i18n from '@dhis2/d2-i18n' -import type { ConfigurableFilterKey } from '../filters' - -export interface ModelPropertyDescriptor { - label: string - path: string -} - -export interface FilterDescriptor { - label: string - filterKey: ConfigurableFilterKey -} - -type Descriptor = - | (ModelPropertyDescriptor & Partial) - | (FilterDescriptor & Partial) - -/* Configs can either define the label and filterKey, or a string -If config is a string, getTranslatedProperty will be used to get the label. */ - -export type FilterConfig = ConfigurableFilterKey | FilterDescriptor - -export type ModelPropertyConfig = string | ModelPropertyDescriptor - -export interface ViewConfigPart { - available?: ReadonlyArray - overrideDefaultAvailable?: boolean - default?: ReadonlyArray -} - -export interface ViewConfig { - columns: ViewConfigPart - filters: ViewConfigPart -} - -// generic here is just used for "satisfies" below, for code-completion of future customizations -// cant use "[key in SectionName]" here, because section.name might be a "string" -export type SectionListViewConfig = { - [key in Key]?: ViewConfig -} - -const DESCRIPTORS = { - publicAccess: { - path: 'sharing.public', - label: i18n.t('Public access'), - filterKey: 'publicAccess', - }, -} satisfies Record - -// This is the default views, and can be overriden per section in modelListViewsConfig below -export const defaultModelViewConfig = { - columns: { - available: [ - 'name', - 'shortName', - 'code', - 'created', - 'createdBy', - 'href', - 'id', - 'lastUpdatedBy', - DESCRIPTORS.publicAccess, - ], - default: ['name', DESCRIPTORS.publicAccess, 'lastUpdated'], - }, - filters: { - available: [], - default: [ - // NOTE: Identifiable is special, and is always included in the default filters - // It should not be handled the same way as "configurable" filters - ], - }, -} satisfies ViewConfig - -/* this is the default views (eg. which columns and filters) to show in the List-page for each section - Note: by default, the available columns are merged with columnsDefault.available above. - If it's needed to override this for a section, set overrideDefaultAvailable to true - and list all available columns in the available array below. - Default-list will NOT be merged with columnsDefault.default - list all explicitly. - elements in the default array implies they are also available, no need to list them in both. */ - -export const modelListViewsConfig = { - dataElement: { - columns: { - available: ['zeroIsSignificant'], - default: [ - 'name', - { label: i18n.t('Domain type'), path: 'domainType' }, - { label: i18n.t('Value type'), path: 'valueType' }, - 'categoryCombo', - 'lastUpdated', - DESCRIPTORS.publicAccess, - ], - }, - filters: { - default: ['domainType', 'valueType', 'dataSet', 'categoryCombo'], - available: [DESCRIPTORS.publicAccess], - }, - }, -} satisfies SectionListViewConfig diff --git a/src/lib/sectionList/listViews/viewConfigResolver.ts b/src/lib/sectionList/listViews/viewConfigResolver.ts deleted file mode 100644 index 740d93ae..00000000 --- a/src/lib/sectionList/listViews/viewConfigResolver.ts +++ /dev/null @@ -1,144 +0,0 @@ -import { getTranslatedProperty } from '../../constants/translatedModelProperties' -import { uniqueBy } from '../../utils' -import { - defaultModelViewConfig, - modelListViewsConfig, - ViewConfigPart, - ModelPropertyConfig, - ModelPropertyDescriptor, - FilterDescriptor, - FilterConfig, -} from './sectionListViewsConfig' - -interface ResolvedViewConfigPart { - available: ReadonlyArray - default: ReadonlyArray -} -interface ResolvedViewConfig { - columns: ResolvedViewConfigPart - filters: ResolvedViewConfigPart -} - -interface ResolvedSectionListView { - [key: string]: ResolvedViewConfig -} - -export const toModelPropertyDescriptor = ( - propertyConfig: ModelPropertyConfig -): ModelPropertyDescriptor => { - if (typeof propertyConfig === 'string') { - return { - label: getTranslatedProperty(propertyConfig), - path: propertyConfig, - } - } - return propertyConfig -} - -export const toFilterDescriptor = ( - propertyConfig: FilterConfig -): FilterDescriptor => { - if (typeof propertyConfig === 'string') { - return { - label: getTranslatedProperty(propertyConfig), - filterKey: propertyConfig, - } - } - return propertyConfig -} - -const resolveFilterConfig = ( - part: ViewConfigPart -): ResolvedViewConfigPart => { - const { default: defaultFilters, available: defaultAvailableFilers } = - defaultModelViewConfig.filters - - const mergedAvailableDescriptors = uniqueBy( - [ - part.available || [], - part.overrideDefaultAvailable ? [] : defaultAvailableFilers || [], - part.default || [], - ] - .flat() - .map((propConfig) => toFilterDescriptor(propConfig)), - (prop) => prop.filterKey - ) - const defaultPropConfig = part.default || defaultFilters || [] - const defaultDescriptors = defaultPropConfig.map((propConfig) => - toFilterDescriptor(propConfig) - ) - return { - available: mergedAvailableDescriptors, - default: defaultDescriptors, - } -} - -const resolveColumnConfig = ( - part: ViewConfigPart -): ResolvedViewConfigPart => { - const { default: defaultFilters, available: defaultAvailableFilers } = - defaultModelViewConfig.columns - - const mergedAvailableDescriptors = uniqueBy( - [ - part.available || [], - part.overrideDefaultAvailable ? [] : defaultAvailableFilers || [], - part.default || [], - ] - .flat() - .map((propConfig) => toModelPropertyDescriptor(propConfig)), - (prop) => prop.path - ) - const defaultPropConfig = part.default || defaultFilters || [] - const defaultDescriptors = defaultPropConfig.map((propConfig) => - toModelPropertyDescriptor(propConfig) - ) - return { - available: mergedAvailableDescriptors, - default: defaultDescriptors, - } -} - -// merge the default modelViewConfig with the modelViewsConfig for each section -const resolveListViewsConfig = () => { - const merged: ResolvedSectionListView = {} - - Object.entries(modelListViewsConfig).forEach((viewConfig) => { - const [sectionName, sectionViewConfig] = viewConfig - merged[sectionName] = { - columns: resolveColumnConfig(sectionViewConfig.columns), - filters: resolveFilterConfig(sectionViewConfig.filters), - } - }) - return merged -} - -const resolvedModelViewsConfig = resolveListViewsConfig() -const resolvedDefaultConfig = { - columns: resolveColumnConfig(defaultModelViewConfig.columns), - filters: resolveFilterConfig(defaultModelViewConfig.filters), -} - -export const getViewConfigForSection = ( - sectionName: string -): ResolvedViewConfig => { - const resolvedConfig = resolvedModelViewsConfig[sectionName] - if (resolvedConfig) { - return resolvedConfig - } - return resolvedDefaultConfig -} - -export const getColumnsForSection = ( - sectionName: string -): ResolvedViewConfig['columns'] => { - const view = getViewConfigForSection(sectionName) - return view.columns -} - -export const getFiltersForSection = ( - sectionName: string -): ResolvedViewConfig['filters'] => { - const view = getViewConfigForSection(sectionName) - return view.filters -} diff --git a/src/lib/sectionList/sectionListViewsConfig.ts b/src/lib/sectionList/sectionListViewsConfig.ts new file mode 100644 index 00000000..f362523e --- /dev/null +++ b/src/lib/sectionList/sectionListViewsConfig.ts @@ -0,0 +1,86 @@ +interface ViewConfigPart { + available: string[] + default: string[] +} + +interface ViewConfig { + columns: ViewConfigPart + filters: ViewConfigPart +} + +interface SectionListView { + [key: string]: ViewConfig +} + +// NOTE: Identifiable is special, and is always displayed +// It should not be handled the same way as "configurable" filters +export const modelListViewsConfig: SectionListView = { + default: { + columns: { + available: [ + 'code', + 'created', + 'createdBy', + 'href', + 'id', + 'lastUpdated', + 'name', + 'sharing.public', + 'shortName', + ], + default: ['name', 'sharing.public', 'lastUpdated'], + }, + filters: { + available: [], + default: [], + }, + }, + dataElement: { + columns: { + available: [ + 'categoryCombo', + 'code', + 'created', + 'createdBy', + 'domainType', + 'href', + 'id', + 'lastUpdated', + 'name', + 'sharing.public', + 'shortName', + 'valueType', + 'zeroIsSignificant', + ], + default: [ + 'name', + 'domainType', + 'valueType', + 'categoryCombo', + 'lastUpdated', + 'sharing.public', + ], + }, + filters: { + available: [ + 'categoryCombo', + 'dataSet', + 'domainType', + 'valueType', + 'publicAccess', + ], + default: ['publicAccess'], + }, + }, +} + +export const getViewConfigForSection = (sectionName: string): ViewConfig => + modelListViewsConfig[sectionName] || modelListViewsConfig.default + +export const getColumnsForSection = ( + sectionName: string +): ViewConfig['columns'] => getViewConfigForSection(sectionName).columns + +export const getFiltersForSection = ( + sectionName: string +): ViewConfig['filters'] => getViewConfigForSection(sectionName).filters diff --git a/src/pages/dataElements/List.tsx b/src/pages/dataElements/List.tsx index 51c39765..ac9abf97 100644 --- a/src/pages/dataElements/List.tsx +++ b/src/pages/dataElements/List.tsx @@ -1,5 +1,4 @@ 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' @@ -46,7 +45,7 @@ export const Component = () => { refetch({ ...initialParams, fields: columns - .map((column) => getFieldFilter(schema, column.path)) + .map((column) => getFieldFilter(schema, column)) .concat(DEFAULT_FIELD_FILTERS), }) }, [refetch, initialParams, columns, listViewQuery.isLoading, schema])