Skip to content

Commit

Permalink
fix(modelListView): allow empty filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Dec 2, 2024
1 parent f44cb14 commit c02c9f6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
18 changes: 6 additions & 12 deletions src/components/sectionList/listView/ManageListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ export const ManageListView = ({
const isDefault = (arr: string[], def: string[]) =>
arr.join() === def.join()

// save empty view if default, this makes the app able to update the default view
// save default as null, this makes the app able to update the default view
const view = {
name: 'default',
columns: isDefault(values.columns, defaultColumns)
? []
? null
: values.columns,
filters: isDefault(values.filters, defaultFilters)
? []
? null
: values.filters,
}

Expand All @@ -108,16 +108,10 @@ 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.map(toPath),
filters: savedFilters.map(toFilterKey),
}
}, [savedFilters, savedColumns, defaultColumns, defaultFilters])
}, [savedFilters, savedColumns])

const handleChangeTab = (tab: 'columns' | 'filters', e: SyntheticEvent) => {
e.preventDefault()
Expand Down
48 changes: 23 additions & 25 deletions src/components/sectionList/listView/useModelListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ const valuesQueryKey = [
const dataStoreModelListViewSchema = z.object({
name: z.string(),
sectionModel: z.string(),
columns: z.array(z.string()),
filters: z.array(z.string()),
// undefined means explicit default
columns: z.array(z.string()).nullable(),
filters: z.array(z.string()).nullable(),
})

type DataStoreModelListView = z.infer<typeof dataStoreModelListViewSchema>
Expand Down Expand Up @@ -59,33 +60,37 @@ const parseViewToModelListView = (
name: string
): ModelListView => {
const listView = dataStoreModelListViewSchema.safeParse(data)

const defaultView = getDefaultViewForSection(name)
if (!listView.success) {
return getDefaultViewForSection(name)
return defaultView
}
const viewConfig = getViewConfigForSection(name)

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 columns =
parsedView.columns?.flatMap((path) => {
const columnConfig = viewConfig.columns.available.find(
(col) => col.path === path
)
return columnConfig ? [columnConfig] : []
}) || defaultView.columns

const filters = parsedView.filters.flatMap((filterKey) => {
const filterConfig = viewConfig.filters.available.find(
(filter) => filter.filterKey === filterKey
)
const filters =
parsedView.filters?.flatMap((filterKey) => {
const filterConfig = viewConfig.filters.available.find(
(filter) => filter.filterKey === filterKey
)

return filterConfig ? [filterConfig] : []
})
return filterConfig ? [filterConfig] : []
}) || defaultView.filters

return {
...parsedView,
columns,
columns: columns.length < 1 ? defaultView.columns : columns,
filters,
}
}
Expand Down Expand Up @@ -142,15 +147,8 @@ export const useModelListView = () => {
const defaultView = getDefaultViewForSection(section.name)
const selectedView = query.data || defaultView

const columns =
selectedView.columns.length < 1
? defaultView.columns
: selectedView.columns
const filters =
selectedView.filters.length < 1
? defaultView.filters
: selectedView.filters

const columns = selectedView.columns
const filters = selectedView.filters
return { view: selectedView, columns, filters, query }
}

Expand Down

0 comments on commit c02c9f6

Please sign in to comment.