|
|
@ -43,7 +43,7 @@ export function useViewFilters( |
|
|
|
|
|
|
|
|
|
|
|
const reloadHook = inject(ReloadViewDataHookInj) |
|
|
|
const reloadHook = inject(ReloadViewDataHookInj) |
|
|
|
|
|
|
|
|
|
|
|
const { nestedFilters } = useSmartsheetStoreOrThrow() |
|
|
|
const { nestedFilters, allFilters } = useSmartsheetStoreOrThrow() |
|
|
|
|
|
|
|
|
|
|
|
const { baseMeta } = storeToRefs(useBase()) |
|
|
|
const { baseMeta } = storeToRefs(useBase()) |
|
|
|
|
|
|
|
|
|
|
@ -208,7 +208,35 @@ export function useViewFilters( |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const loadFilters = async (hookId?: string, isWebhook = false) => { |
|
|
|
const loadAllChildFilters = async (filters: Filter[]) => { |
|
|
|
|
|
|
|
// Array to store promises of child filter loading
|
|
|
|
|
|
|
|
const promises = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Array to store all child filters
|
|
|
|
|
|
|
|
const allChildFilters: Filter[] = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Iterate over all filters
|
|
|
|
|
|
|
|
for (const filter of filters) { |
|
|
|
|
|
|
|
// Check if the filter is a group
|
|
|
|
|
|
|
|
if (filter.id && filter.is_group) { |
|
|
|
|
|
|
|
// Load children filters from the backend
|
|
|
|
|
|
|
|
const childFilterPromise = $api.dbTableFilter.childrenRead(filter.id).then((response) => { |
|
|
|
|
|
|
|
const childFilters = response.list as Filter[] |
|
|
|
|
|
|
|
allChildFilters.push(...childFilters) |
|
|
|
|
|
|
|
return loadAllChildFilters(childFilters) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
promises.push(childFilterPromise) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait for all promises to resolve
|
|
|
|
|
|
|
|
await Promise.all(promises) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Push all child filters into the allFilters array
|
|
|
|
|
|
|
|
allFilters.value.push(...allChildFilters) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const loadFilters = async (hookId?: string, isWebhook = false, loadAllFilters = false) => { |
|
|
|
if (!view.value?.id) return |
|
|
|
if (!view.value?.id) return |
|
|
|
|
|
|
|
|
|
|
|
if (nestedMode.value) { |
|
|
|
if (nestedMode.value) { |
|
|
@ -228,6 +256,10 @@ export function useViewFilters( |
|
|
|
filters.value = (await $api.dbTableFilter.childrenRead(parentId)).list as Filter[] |
|
|
|
filters.value = (await $api.dbTableFilter.childrenRead(parentId)).list as Filter[] |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
filters.value = (await $api.dbTableFilter.read(view.value!.id!)).list as Filter[] |
|
|
|
filters.value = (await $api.dbTableFilter.read(view.value!.id!)).list as Filter[] |
|
|
|
|
|
|
|
if (loadAllFilters) { |
|
|
|
|
|
|
|
allFilters.value = [...filters.value] |
|
|
|
|
|
|
|
await loadAllChildFilters(allFilters.value) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e: any) { |
|
|
|
} catch (e: any) { |
|
|
@ -241,6 +273,11 @@ export function useViewFilters( |
|
|
|
for (const [i, filter] of Object.entries(filters.value)) { |
|
|
|
for (const [i, filter] of Object.entries(filters.value)) { |
|
|
|
if (filter.status === 'delete') { |
|
|
|
if (filter.status === 'delete') { |
|
|
|
await $api.dbTableFilter.delete(filter.id as string) |
|
|
|
await $api.dbTableFilter.delete(filter.id as string) |
|
|
|
|
|
|
|
if (filter.is_group) { |
|
|
|
|
|
|
|
deleteFilterGroupFromAllFilters(filter) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
allFilters.value = allFilters.value.filter((f) => f.id !== filter.id) |
|
|
|
|
|
|
|
} |
|
|
|
} else if (filter.status === 'update') { |
|
|
|
} else if (filter.status === 'update') { |
|
|
|
await $api.dbTableFilter.update(filter.id as string, { |
|
|
|
await $api.dbTableFilter.update(filter.id as string, { |
|
|
|
...filter, |
|
|
|
...filter, |
|
|
@ -258,6 +295,8 @@ export function useViewFilters( |
|
|
|
fk_parent_id: parentId, |
|
|
|
fk_parent_id: parentId, |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
allFilters.value.push(filters.value[+i]) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -323,6 +362,8 @@ export function useViewFilters( |
|
|
|
...filter, |
|
|
|
...filter, |
|
|
|
fk_parent_id: parentId, |
|
|
|
fk_parent_id: parentId, |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
allFilters.value.push(filters.value[+i]) |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e: any) { |
|
|
|
} catch (e: any) { |
|
|
|
console.log(e) |
|
|
|
console.log(e) |
|
|
@ -334,6 +375,21 @@ export function useViewFilters( |
|
|
|
if (!isWebhook) reloadData?.() |
|
|
|
if (!isWebhook) reloadData?.() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function deleteFilterGroupFromAllFilters(filter: Filter) { |
|
|
|
|
|
|
|
// Find all child filters of the specified parentId
|
|
|
|
|
|
|
|
const childFilters = allFilters.value.filter((f) => f.fk_parent_id === filter.id) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Recursively delete child filter of child filter
|
|
|
|
|
|
|
|
childFilters.forEach((childFilter) => { |
|
|
|
|
|
|
|
if (childFilter.is_group) { |
|
|
|
|
|
|
|
deleteFilterGroupFromAllFilters(childFilter) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Remove the parent object and its children from the array
|
|
|
|
|
|
|
|
allFilters.value = allFilters.value.filter((f) => f.id !== filter.id && f.fk_parent_id !== filter.id) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const deleteFilter = async (filter: Filter, i: number, undo = false) => { |
|
|
|
const deleteFilter = async (filter: Filter, i: number, undo = false) => { |
|
|
|
if (!undo && !filter.is_group) { |
|
|
|
if (!undo && !filter.is_group) { |
|
|
|
addUndo({ |
|
|
|
addUndo({ |
|
|
@ -369,6 +425,7 @@ export function useViewFilters( |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
try { |
|
|
|
try { |
|
|
|
await $api.dbTableFilter.delete(filter.id) |
|
|
|
await $api.dbTableFilter.delete(filter.id) |
|
|
|
|
|
|
|
|
|
|
|
if (!isWebhook) reloadData?.() |
|
|
|
if (!isWebhook) reloadData?.() |
|
|
|
filters.value.splice(i, 1) |
|
|
|
filters.value.splice(i, 1) |
|
|
|
} catch (e: any) { |
|
|
|
} catch (e: any) { |
|
|
@ -382,12 +439,18 @@ export function useViewFilters( |
|
|
|
} |
|
|
|
} |
|
|
|
$e('a:filter:delete', { length: nonDeletedFilters.value.length }) |
|
|
|
$e('a:filter:delete', { length: nonDeletedFilters.value.length }) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.is_group) { |
|
|
|
|
|
|
|
deleteFilterGroupFromAllFilters(filter) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
allFilters.value = allFilters.value.filter((f) => f.id !== filter.id) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const saveOrUpdateDebounced = useDebounceFn(saveOrUpdate, 500) |
|
|
|
const saveOrUpdateDebounced = useDebounceFn(saveOrUpdate, 500) |
|
|
|
|
|
|
|
|
|
|
|
const addFilter = async (undo = false) => { |
|
|
|
const addFilter = async (undo = false, draftFilter: Partial<FilterType> = {}) => { |
|
|
|
filters.value.push(placeholderFilter()) |
|
|
|
filters.value.push(draftFilter?.fk_column_id ? { ...placeholderFilter(), ...draftFilter } : placeholderFilter()) |
|
|
|
if (!undo) { |
|
|
|
if (!undo) { |
|
|
|
addUndo({ |
|
|
|
addUndo({ |
|
|
|
undo: { |
|
|
|
undo: { |
|
|
|