Skip to content

Commit 07568d6

Browse files
committed
fix: improve initial state logic
1 parent c872985 commit 07568d6

File tree

2 files changed

+77
-45
lines changed

2 files changed

+77
-45
lines changed

packages/ui/src/components/MagnetarTable.vue

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import {
1717
import {
1818
calcCollection,
1919
carbonCopyMap,
20-
columnsToInitialOrderByState,
20+
filtersAndColumnsToInitialState,
2121
filterStateToClauses,
22-
filtersToInitialState,
2322
getRequiredOrderByBasedOnFilters,
2423
mapUnshift,
2524
orderByStateToClauses,
@@ -55,35 +54,48 @@ function muiLabel(label: MUILabel): string {
5554
// const emit = defineEmits<{}>()
5655
const fetchState = ref<'ok' | 'end' | 'fetching' | { error: string }>('ok')
5756
58-
const initialFilterState = ((): FiltersState => {
59-
const map = filtersToInitialState(props.filters)
57+
const initialState = ((): {
58+
filtersState: FiltersState
59+
orderByState: OrderByState
60+
} => {
61+
const { filtersState, orderByState } = filtersAndColumnsToInitialState({
62+
columns: props.columns,
63+
filters: props.filters,
64+
})
6065
if (props.filtersState) {
6166
for (const [index, state] of props.filtersState) {
62-
map.set(index, state)
67+
filtersState.set(index, state)
6368
}
6469
}
65-
return map
70+
return {
71+
filtersState,
72+
orderByState: props.orderByState || orderByState,
73+
}
6674
})()
67-
const initialOrderByState: OrderByState =
68-
props.orderByState || columnsToInitialOrderByState(props.columns, initialFilterState)
6975
70-
const filtersState = ref<FiltersState>(carbonCopyMap(initialFilterState))
71-
const orderByState = ref<OrderByState>(carbonCopyMap(initialOrderByState))
76+
const filtersState = ref<FiltersState>(carbonCopyMap(initialState.filtersState))
77+
const orderByState = ref<OrderByState>(carbonCopyMap(initialState.orderByState))
7278
7379
const currentFilters = computed(() => filterStateToClauses(filtersState.value, props.filters))
7480
const currentOrderBy = computed(() => orderByStateToClauses(orderByState.value))
7581
76-
const initialStateActive = computed<boolean>(
77-
() =>
78-
filtersState.value.size === initialFilterState.size &&
79-
orderByState.value.size === initialOrderByState.size &&
80-
[...filtersState.value.entries()].every(
81-
([key, value]) => initialFilterState.get(key) === value
82-
) &&
83-
[...orderByState.value.entries()].every(
84-
([key, value]) => initialOrderByState.get(key) === value
85-
)
86-
)
82+
/**
83+
* This will always be true in case there was no initialState
84+
*/
85+
const initialStateActive = computed<boolean>(() => {
86+
const initialFilters = initialState.filtersState
87+
const initialOrderBy = initialState.orderByState
88+
if (!initialFilters.size && !initialOrderBy.size) {
89+
return true
90+
}
91+
return (
92+
filtersState.value.size === initialFilters.size &&
93+
orderByState.value.size === initialOrderBy.size &&
94+
[...filtersState.value.entries()].every(([key, value]) => initialFilters.get(key) === value) &&
95+
[...orderByState.value.entries()].every(([key, value]) => initialOrderBy.get(key) === value)
96+
)
97+
})
98+
8799
const hasSomeFilterOrOrderby = computed<boolean>(
88100
() => !!filtersState.value.size || !!orderByState.value.size
89101
)
@@ -99,8 +111,8 @@ function clearAllRecords(): void {
99111
}
100112
101113
function resetState(): void {
102-
filtersState.value = carbonCopyMap(initialFilterState)
103-
orderByState.value = carbonCopyMap(initialOrderByState)
114+
filtersState.value = carbonCopyMap(initialState.filtersState)
115+
orderByState.value = carbonCopyMap(initialState.orderByState)
104116
clearAllRecords()
105117
fetchMore()
106118
}

packages/ui/src/utils.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ export function whereClausesEqual(where1?: WhereClause, where2?: WhereClause): b
3333
)
3434
}
3535

36-
export function filtersToInitialState(filters: MUIFilter<Record<string, any>>[]): FiltersState {
36+
export function filtersAndColumnsToInitialState(params: {
37+
columns: MUIColumn<any>[]
38+
filters: MUIFilter<Record<string, any>>[]
39+
}): {
40+
filtersState: FiltersState
41+
orderByState: OrderByState
42+
} {
43+
const { columns, filters } = params
3744
// remember, see `FiltersState` for instructions how to save the state.
38-
return filters.reduce<FiltersState>((map, f, i) => {
45+
const _filtersState = filters.reduce<FiltersState>((map, f, i) => {
3946
if (f.type === 'radio' || f.type === 'select') {
4047
const firstChecked = f.options?.find((o) => o.checked)
4148
if (firstChecked) map.set(i, firstChecked.where)
@@ -55,6 +62,39 @@ export function filtersToInitialState(filters: MUIFilter<Record<string, any>>[])
5562
}
5663
return map
5764
}, new Map())
65+
66+
// maybe we needed to clear other filters, let's do that for the first filter we find that needs it
67+
const entryThatClearsOtherFilters = [..._filtersState.entries()].find(([filterIndex]) => {
68+
const filter = filters[filterIndex]
69+
return filter?.clearOtherFilters
70+
})
71+
const filtersState = entryThatClearsOtherFilters
72+
? new Map([entryThatClearsOtherFilters])
73+
: _filtersState
74+
75+
// maybe we needed to clear other orderBy, let's do that for the first filter we find that needs it
76+
const entryThatClearsOtherOrderBy = [...filtersState.entries()].find(([filterIndex]) => {
77+
const filter = filters[filterIndex]
78+
return filter?.clearOrderBy
79+
})
80+
81+
const _orderByState = entryThatClearsOtherOrderBy
82+
? new Map()
83+
: sort(columns)
84+
// sort columns by sortable.position
85+
.asc((c) => (isPlainObject(c.sortable) ? c.sortable.position : -1))
86+
// then grab each column's sortable.orderBy and save as "direction" in a map
87+
.reduce<OrderByState>((map, column) => {
88+
if (isPlainObject(column.sortable) && column.sortable.orderBy && column.fieldPath) {
89+
map.set(column.fieldPath, column.sortable.orderBy)
90+
}
91+
return map
92+
}, new Map())
93+
94+
const newEntries = getRequiredOrderByBasedOnFilters(filtersState)
95+
const orderByState = newEntries.length ? mapUnshift(_orderByState, ...newEntries) : _orderByState
96+
97+
return { filtersState, orderByState }
5898
}
5999

60100
/**
@@ -98,26 +138,6 @@ export function getRequiredOrderByBasedOnFilters(
98138
}, [])
99139
}
100140

101-
export function columnsToInitialOrderByState(
102-
columns: MUIColumn<any>[],
103-
initialFilterState: FiltersState
104-
): OrderByState {
105-
const orderByState = sort(columns)
106-
// sort columns by sortable.position
107-
.asc((c) => (isPlainObject(c.sortable) ? c.sortable.position : -1))
108-
// then grab each column's sortable.orderBy and save as "direction" in a map
109-
.reduce<OrderByState>((map, column) => {
110-
if (isPlainObject(column.sortable) && column.sortable.orderBy && column.fieldPath) {
111-
map.set(column.fieldPath, column.sortable.orderBy)
112-
}
113-
return map
114-
}, new Map())
115-
116-
const newEntries = getRequiredOrderByBasedOnFilters(initialFilterState)
117-
if (newEntries.length) return mapUnshift(orderByState, ...newEntries)
118-
return orderByState
119-
}
120-
121141
/** Clears JavaScript reference pointers */
122142
export function carbonCopyMap<T extends Map<any, any>>(map: T): T {
123143
let newMap = new Map() as T

0 commit comments

Comments
 (0)