From 6d385adf485c26320e78d8b1ebae0646a608f471 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Wed, 10 Aug 2022 15:40:28 +0800 Subject: [PATCH] refactor(gui-v2): fieldsValidation --- .../nc-gui-v2/components/template/Editor.vue | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/packages/nc-gui-v2/components/template/Editor.vue b/packages/nc-gui-v2/components/template/Editor.vue index d72670d943..ea965308dd 100644 --- a/packages/nc-gui-v2/components/template/Editor.vue +++ b/packages/nc-gui-v2/components/template/Editor.vue @@ -211,12 +211,22 @@ function atLeastOneEnabledValidation() { return true } -function fieldsValidation(table: Record, record: Record) { +function fieldsValidation(record: Record) { // if it is not selected, then pass validation if (!record.enabled) { return true } + const tableName = meta?.value.title || '' + + if (!record.destCn) { + notification.error({ + message: `Cannot find the destination column for ${record.srcCn}`, + duration: 3, + }) + return false + } + if (srcDestMapping.value.filter((v) => v.destCn === record.destCn).length > 1) { notification.error({ message: 'Duplicate mapping found, please remove one of the mapping', @@ -230,7 +240,7 @@ function fieldsValidation(table: Record, record: Record) => r[record.srcCn] === null || r[record.srcCn] === undefined || r[record.srcCn] === '') ) { @@ -244,7 +254,7 @@ function fieldsValidation(table: Record, record: Record) => r[record.sourceCn] !== null && r[record.srcCn] !== undefined && isNaN(+r[record.srcCn]), @@ -259,7 +269,7 @@ function fieldsValidation(table: Record, record: Record) => { + importData[tableName].slice(0, maxRowsToParse).some((r: Record) => { if ((r: Record) => r[record.srcCn] !== null && r[record.srcCn] !== undefined) { let input = r[record.srcCn] if (typeof input === 'string') { @@ -300,9 +310,9 @@ async function importTemplate() { try { isImporting.value = true - const data = importData[meta?.value?.title as string] - const projectName = project.value.title as string const tableName = meta?.value.title as string + const data = importData[tableName] + const projectName = project.value.title as string const total = data.length for (let i = 0, progress = 0; i < total; i += maxRowsToParse) { const batchData = data.slice(i, i + maxRowsToParse).map((row: Record) => @@ -447,12 +457,20 @@ async function importTemplate() { } const isValid = computed(() => { - for (const [_, o] of Object.entries(validateInfos)) { - if (o?.validateStatus) { - if (o.validateStatus === 'error') { + if (importOnly) { + for (const record of srcDestMapping.value) { + if (!fieldsValidation(record)) { return false } } + } else { + for (const [_, o] of Object.entries(validateInfos)) { + if (o?.validateStatus) { + if (o.validateStatus === 'error') { + return false + } + } + } } return true }) @@ -471,9 +489,6 @@ function mapDefaultColumns() { } srcDestMapping.value.push(o) } - nextTick(() => { - // TODO: validate - }) } defineExpose({ @@ -528,7 +543,6 @@ onMounted(() => { class="w-52" show-search :filter-option="filterOption" - @change="fieldsValidation(table, record)" >
@@ -540,7 +554,7 @@ onMounted(() => {