From 3a85fd0b11fbc4f8632f671dc49bd65abdd05d04 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Mon, 12 Sep 2022 19:21:38 +0800 Subject: [PATCH] feat(nc-gui): groupingField logic in useKanbanViewData --- .../nc-gui/composables/useKanbanViewData.ts | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/nc-gui/composables/useKanbanViewData.ts b/packages/nc-gui/composables/useKanbanViewData.ts index 18a2aaad72..e46a095348 100644 --- a/packages/nc-gui/composables/useKanbanViewData.ts +++ b/packages/nc-gui/composables/useKanbanViewData.ts @@ -17,8 +17,8 @@ export function useKanbanViewData( const { $api, $e } = useNuxtApp() const { sorts, nestedFilters } = useSmartsheetStoreOrThrow() const { isUIAllowed } = useUIPermission() - const groupingFieldColOptions = ref[]>([]) - const kanbanMetaData = ref() + const groupingFieldColOptions = useState[]>('KanbanGroupingFieldColOptions', () => []) + const kanbanMetaData = useState('KanbanMetaData', () => ({})) // { // [val1] : [ // {row: {...}, oldRow: {...}, rowMeta: {...}}, @@ -31,9 +31,8 @@ export function useKanbanViewData( // ... // ], // } - const formattedData = useState>('formattedKanbanData', () => ({})) - // TODO: retrieve from meta - const groupingField = 'singleSelect2' + const formattedData = useState>('KanbanFormattedData', () => ({})) + const groupingField = useState('KanbanGroupingField', () => '') const formatData = (list: Record[]) => list.map((row) => ({ @@ -48,8 +47,12 @@ export function useKanbanViewData( // TODO: handle share view case if ((!project?.value?.id || !meta?.value?.id || !viewMeta?.value?.id) && !isPublic.value) return + // reset formattedData to avoid storing previous data + // after changing grouping field + formattedData.value = {} + // grouping field column options - const groupingFieldColumn = meta?.value?.columns?.filter((f) => f.title === groupingField)[0] as Record + const groupingFieldColumn = meta?.value?.columns?.filter((f) => f.title === groupingField.value)[0] as Record groupingFieldColOptions.value = [ ...(groupingFieldColumn?.colOptions?.options ?? []), { id: 'uncategorized', title: 'Uncategorized', order: 0 }, @@ -57,9 +60,9 @@ export function useKanbanViewData( await Promise.all( groupingFieldColOptions.value.map(async (option) => { - let where = `(${groupingField},eq,${option.title})` + let where = `(${groupingField.value},eq,${option.title})` if (option.title === 'Uncategorized') { - where = `(${groupingField},is,null)` + where = `(${groupingField.value},is,null)` } const response = await api.dbViewRow.list('noco', project.value.id!, meta!.value.id!, viewMeta!.value.id, { ...params, @@ -67,7 +70,6 @@ export function useKanbanViewData( ...(isUIAllowed('filterSync') ? {} : { filterArrJson: JSON.stringify(nestedFilters.value) }), where, }) - formattedData.value[option.title] = formatData(response.list) }), ) @@ -76,6 +78,9 @@ export function useKanbanViewData( async function loadKanbanMeta() { if (!viewMeta?.value?.id) return kanbanMetaData.value = await $api.dbView.kanbanRead(viewMeta.value.id) + // set groupingField + const groupingFieldCol = meta?.value?.columns?.filter((f) => f.id === kanbanMetaData?.value?.grp_column_id)[0] + groupingField.value = groupingFieldCol?.title as string } async function insertRow(row: Record, rowIndex = formattedData.value.uncatgorized?.length) { @@ -148,7 +153,7 @@ export function useKanbanViewData( if (row.rowMeta.new) { await insertRow(row.row, formattedData.value[row.row.title].indexOf(row)) } else { - await updateRowProperty(row, groupingField) + await updateRowProperty(row, groupingField.value) } } @@ -162,6 +167,8 @@ export function useKanbanViewData( return formattedData.value.Uncategorized[addAfter] } + watch(groupingField, async (v) => await loadKanbanData()) + return { loadKanbanData, loadKanbanMeta,