Browse Source

feat(nc-gui): kanban delete stack logic

pull/3818/head
Wing-Kam Wong 2 years ago
parent
commit
0ec57b0146
  1. 12
      packages/nc-gui/components/smartsheet/Kanban.vue
  2. 81
      packages/nc-gui/composables/useKanbanViewData.ts

12
packages/nc-gui/components/smartsheet/Kanban.vue

@ -39,6 +39,8 @@ const deleteStackVModel = ref(false)
const stackToBeDeleted = ref('') const stackToBeDeleted = ref('')
const stackIdxToBeDeleted = ref(0)
const { const {
loadKanbanData, loadKanbanData,
loadMoreKanbanData, loadMoreKanbanData,
@ -146,13 +148,14 @@ const kanbanListRef = (kanbanListElement: HTMLElement) => {
} }
} }
const handleDeleteStackClick = (stackTitle: string) => { const handleDeleteStackClick = (stackTitle: string, stackIdx: number) => {
deleteStackVModel.value = true deleteStackVModel.value = true
stackToBeDeleted.value = stackTitle stackToBeDeleted.value = stackTitle
stackIdxToBeDeleted.value = stackIdx
} }
const handleDeleteStackConfirmClick = async () => { const handleDeleteStackConfirmClick = async () => {
await deleteStack(stackToBeDeleted.value) await deleteStack(stackToBeDeleted.value, stackIdxToBeDeleted.value)
deleteStackVModel.value = false deleteStackVModel.value = false
} }
@ -236,7 +239,10 @@ onMounted(() => {
Collapse Stack Collapse Stack
</div> </div>
</a-menu-item> </a-menu-item>
<a-menu-item v-if="stack.title !== 'uncategorized'" @click="handleDeleteStackClick(stack.title)"> <a-menu-item
v-if="stack.title !== 'uncategorized'"
@click="handleDeleteStackClick(stack.title, stackIdx)"
>
<div class="py-2 flex gap-2 items-center"> <div class="py-2 flex gap-2 items-center">
<mdi-delete class="text-gray-500" /> <mdi-delete class="text-gray-500" />
<!-- TODO: i18n --> <!-- TODO: i18n -->

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

@ -154,16 +154,28 @@ export function useKanbanViewData(
// handle deleted options // handle deleted options
const columnOptionIds = (groupingFieldColumn.value?.colOptions as SelectOptionsType)?.options.map(({ id }) => id) const columnOptionIds = (groupingFieldColumn.value?.colOptions as SelectOptionsType)?.options.map(({ id }) => id)
stackMetaObj.value[grp_column_id] const cols = stackMetaObj.value[grp_column_id].filter(({ id }) => id !== 'uncategorized' && !columnOptionIds.includes(id))
.filter(({ id }) => id !== 'uncategorized' && !columnOptionIds.includes(id)) for (const col of cols) {
.forEach(({ id }) => { const idx = stackMetaObj.value[grp_column_id].map((ele: Record<string, any>) => ele.id).indexOf(col.id)
const idx = stackMetaObj.value[grp_column_id].map((ele: Record<string, any>) => ele.id).indexOf(id) if (idx !== -1) {
if (idx !== -1) { stackMetaObj.value[grp_column_id].splice(idx, 1)
deleteStack(stackMetaObj.value[grp_column_id][idx].title!) // there are two cases
stackMetaObj.value[grp_column_id].splice(idx, 1) // 1. delete option from Add / Edit Stack in kanban view
// 2. delete option from grid view, then switch to kanban view
// for the second case, formattedData.value and countByStack.value would be empty at this moment
// however, the data will be correct after rendering
if (Object.keys(formattedData.value).length && Object.keys(countByStack.value).length) {
// for the first case, no reload is executed.
// hence, we set groupingField to null for all records under the target stack
await bulkUpdateGroupingFieldValue(col.title!, true)
// merge the to-be-deleted stack to uncategorized stack
formattedData.value.uncategorized = [...formattedData.value.uncategorized, ...formattedData.value[col.title!]]
// update the record count
countByStack.value.uncategorized += countByStack.value[col.title!]
} }
}) isChanged = true
}
}
groupingFieldColOptions.value = stackMetaObj.value[grp_column_id] groupingFieldColOptions.value = stackMetaObj.value[grp_column_id]
if (isChanged) { if (isChanged) {
@ -295,33 +307,50 @@ export function useKanbanViewData(
where: `(${groupingField.value},eq,${stackTitle})`, where: `(${groupingField.value},eq,${stackTitle})`,
}, },
) )
// update to groupingField value to target value if (stackTitle in formattedData.value) {
formattedData.value[stackTitle] = formattedData.value[stackTitle].map((o) => ({ // update to groupingField value to target value
...o, formattedData.value[stackTitle] = formattedData.value[stackTitle].map((o) => ({
row: { ...o,
...o.row, row: {
[groupingField.value]: groupingFieldVal, ...o.row,
}, [groupingField.value]: groupingFieldVal,
oldRow: { },
...o.oldRow, oldRow: {
[groupingField.value]: o.row[groupingField.value], ...o.oldRow,
}, [groupingField.value]: o.row[groupingField.value],
})) },
}))
}
} catch (e: any) { } catch (e: any) {
message.error(await extractSdkResponseErrorMsg(e)) message.error(await extractSdkResponseErrorMsg(e))
} }
} }
async function deleteStack(stackTitle: string) { async function deleteStack(stackTitle: string, stackIdx: number) {
if (!viewMeta?.value?.id || !groupingFieldColumn.value) return
try { try {
// set groupingField to null for all records under the target stack // set groupingField to null for all records under the target stack
await bulkUpdateGroupingFieldValue(stackTitle, true) await bulkUpdateGroupingFieldValue(stackTitle, true)
// merge the 'deleted' stack to uncategorized stack // merge the to-be-deleted stack to uncategorized stack
formattedData.value.uncategorized = [...formattedData.value.uncategorized, ...formattedData.value[stackTitle]] formattedData.value.uncategorized = [...formattedData.value.uncategorized, ...formattedData.value[stackTitle]]
countByStack.value.uncategorized += countByStack.value[stackTitle] countByStack.value.uncategorized += countByStack.value[stackTitle]
// clear the 'deleted' stack // clear state for the to-be-deleted stack
formattedData.value[stackTitle] = [] delete formattedData.value[stackTitle]
countByStack.value[stackTitle] = 0 delete countByStack.value[stackTitle]
// delete the stack, i.e. grouping field value
const newOptions = (groupingFieldColumn.value.colOptions as SelectOptionsType).options.filter((o) => o.title !== stackTitle)
;(groupingFieldColumn.value.colOptions as SelectOptionsType).options = newOptions
await api.dbTableColumn.update(groupingFieldColumn.value.id!, {
...groupingFieldColumn.value,
colOptions: {
options: newOptions,
},
} as any)
// update kanban stack meta
stackMetaObj.value[kanbanMetaData.value.grp_column_id!].splice(stackIdx, 1)
groupingFieldColOptions.value.splice(stackIdx, 1)
await updateKanbanStackMeta()
} catch (e: any) { } catch (e: any) {
message.error(await extractSdkResponseErrorMsg(e)) message.error(await extractSdkResponseErrorMsg(e))
} }

Loading…
Cancel
Save