diff --git a/packages/nc-gui-v2/components/template/Editor.vue b/packages/nc-gui-v2/components/template/Editor.vue index 453a683b7e..6038bf26c5 100644 --- a/packages/nc-gui-v2/components/template/Editor.vue +++ b/packages/nc-gui-v2/components/template/Editor.vue @@ -13,12 +13,12 @@ import MdiPlusIcon from '~icons/mdi/plus' import MdiKeyStarIcon from '~icons/mdi/key-star' import MdiDeleteOutlineIcon from '~icons/mdi/delete-outline' import { extractSdkResponseErrorMsg, fieldRequiredValidator, getUIDTIcon } from '~/utils' -import { MetaInj } from '~/context' +import { MetaInj, ReloadViewDataHookInj } from '~/context' interface Props { quickImportType: 'csv' | 'excel' | 'json' projectTemplate: Record - importData: any[] + importData: Record[] importColumns: any[] importOnly: boolean } @@ -36,6 +36,8 @@ const meta = inject(MetaInj) const columns = computed(() => meta?.value?.columns || []) +const reloadHook = inject(ReloadViewDataHookInj)! + const useForm = Form.useForm const { $api } = useNuxtApp() @@ -297,7 +299,61 @@ async function importTemplate() { // validate at least one column needs to be selected if (!atLeastOneEnabledValidation()) return - // TODO: ok + try { + isImporting.value = true + console.log(importData) + const data = importData[meta?.value?.title as string] + console.log(srcDestMapping.value) + for (let i = 0, progress = 0; i < data.length; i += 500) { + const batchData = data.slice(i, i + 500).map((row: Record) => + srcDestMapping.value.reduce((res: Record, col: Record) => { + console.log(col) + if (col.enabled && col.destCn) { + const v = columns.value.find((c: Record) => c.title === col.destCn) as Record + let input = row[col.srcCn] + // parse potential boolean values + if (v.uidt == UITypes.Checkbox) { + input = input.replace(/["']/g, '').toLowerCase().trim() + if (input == 'false' || input == 'no' || input == 'n') { + input = '0' + } else if (input == 'true' || input == 'yes' || input == 'y') { + input = '1' + } + } else if (v.uidt === UITypes.Number) { + if (input == '') { + input = null + } + } else if (v.uidt === UITypes.SingleSelect || v.uidt === UITypes.MultiSelect) { + if (input == '') { + input = null + } + } + res[col.destCn] = input + } + return res + }, {}), + ) + await $api.dbTableRow.bulkCreate('noco', project.value.title as string, meta?.value.title as string, batchData) + progress += batchData.length + // this.$store.commit('loader/MutMessage', `Importing data : ${progress}/${data.length}`); + // this.$store.commit('loader/MutProgress', Math.round((100 * progress) / data.length)); + } + + // reload table + reloadHook.trigger() + + notification.success({ + message: 'Successfully imported table data', + duration: 3, + }) + } catch (e: any) { + notification.error({ + message: e.message, + duration: 3, + }) + } finally { + isImporting.value = false + } } else { // check if form is valid try {