Browse Source

feat(nc-gui): add updateKanbanMeta & stack_meta logic

pull/3818/head
Wing-Kam Wong 2 years ago
parent
commit
99fda1668d
  1. 45
      packages/nc-gui/composables/useKanbanViewData.ts

45
packages/nc-gui/composables/useKanbanViewData.ts

@ -19,6 +19,7 @@ export function useKanbanViewData(
const { isUIAllowed } = useUIPermission() const { isUIAllowed } = useUIPermission()
const groupingFieldColOptions = useState<Record<string, any>[]>('KanbanGroupingFieldColOptions', () => []) const groupingFieldColOptions = useState<Record<string, any>[]>('KanbanGroupingFieldColOptions', () => [])
const kanbanMetaData = useState<KanbanType>('KanbanMetaData', () => ({})) const kanbanMetaData = useState<KanbanType>('KanbanMetaData', () => ({}))
// formattedData structure
// { // {
// [val1] : [ // [val1] : [
// {row: {...}, oldRow: {...}, rowMeta: {...}}, // {row: {...}, oldRow: {...}, rowMeta: {...}},
@ -47,16 +48,34 @@ export function useKanbanViewData(
// TODO: handle share view case // TODO: handle share view case
if ((!project?.value?.id || !meta?.value?.id || !viewMeta?.value?.id) && !isPublic.value) return if ((!project?.value?.id || !meta?.value?.id || !viewMeta?.value?.id) && !isPublic.value) return
// reset formattedData to avoid storing previous data // reset formattedData to avoid storing previous data after changing grouping field
// after changing grouping field
formattedData.value = {} formattedData.value = {}
// grouping field column options const { grp_column_id, stack_meta } = kanbanMetaData.value
const groupingFieldColumn = meta?.value?.columns?.filter((f) => f.title === groupingField.value)[0] as Record<string, any>
groupingFieldColOptions.value = [ const stackMetaObj = JSON.parse(stack_meta as string) || {}
...(groupingFieldColumn?.colOptions?.options ?? []),
{ id: 'uncategorized', title: 'Uncategorized', order: 0 }, if (stack_meta && grp_column_id && stackMetaObj[grp_column_id]) {
].sort((a: Record<string, any>, b: Record<string, any>) => a.order - b.order) // the target stack meta exists, use it directly
groupingFieldColOptions.value = stackMetaObj[grp_column_id]
} else {
// retrieve the grouping field column
const groupingFieldColumn = meta?.value?.columns?.filter((f) => f.title === groupingField.value)[0] as Record<string, any>
// build stack meta
groupingFieldColOptions.value = [
...(groupingFieldColumn?.colOptions?.options ?? []),
// enrich uncategorized stack
{ id: 'uncategorized', title: 'Uncategorized', order: 0 },
].sort((a: Record<string, any>, b: Record<string, any>) => a.order - b.order)
// if grouping column id is present, add the grouping field column options to stackMetaObj
if (grp_column_id) {
stackMetaObj[grp_column_id] = groupingFieldColOptions.value
await updateKanbanMeta({
stack_meta: stackMetaObj,
})
}
}
await Promise.all( await Promise.all(
groupingFieldColOptions.value.map(async (option) => { groupingFieldColOptions.value.map(async (option) => {
@ -83,6 +102,15 @@ export function useKanbanViewData(
groupingField.value = groupingFieldCol?.title as string groupingField.value = groupingFieldCol?.title as string
} }
async function updateKanbanMeta(updateObj: Partial<KanbanType>) {
if (!viewMeta?.value?.id) return
await $api.dbView.kanbanUpdate(viewMeta.value.id, {
...kanbanMetaData.value,
...updateObj,
})
await loadKanbanMeta()
}
async function insertRow(row: Record<string, any>, rowIndex = formattedData.value.uncatgorized?.length) { async function insertRow(row: Record<string, any>, rowIndex = formattedData.value.uncatgorized?.length) {
try { try {
const insertObj = meta?.value?.columns?.reduce((o: any, col) => { const insertObj = meta?.value?.columns?.reduce((o: any, col) => {
@ -172,6 +200,7 @@ export function useKanbanViewData(
return { return {
loadKanbanData, loadKanbanData,
loadKanbanMeta, loadKanbanMeta,
updateKanbanMeta,
kanbanMetaData, kanbanMetaData,
formattedData, formattedData,
groupingField, groupingField,

Loading…
Cancel
Save