Browse Source

feat(nc-gui): support multiple tn in srcDestMapping

pull/4135/head
Wing-Kam Wong 2 years ago
parent
commit
e596bf7ac1
  1. 65
      packages/nc-gui/components/template/Editor.vue

65
packages/nc-gui/components/template/Editor.vue

@ -93,7 +93,7 @@ const uiTypeOptions = ref<Option[]>(
})), })),
) )
const srcDestMapping = ref<Record<string, any>[]>([]) const srcDestMapping = ref<Record<string, Record<string, any>[]>>({})
const data = reactive<{ const data = reactive<{
title: string | null title: string | null
@ -124,12 +124,18 @@ const validators = computed(() =>
const { validate, validateInfos } = useForm(data, validators) const { validate, validateInfos } = useForm(data, validators)
// TODO(import): show error message once
const isValid = computed(() => { const isValid = computed(() => {
if (importDataOnly) { if (importDataOnly) {
for (const record of srcDestMapping.value) { for (const tn of Object.keys(srcDestMapping.value)) {
if (!fieldsValidation(record)) { if (!atLeastOneEnabledValidation(tn)) {
return false return false
} }
for (const record of srcDestMapping.value[tn]) {
if (!fieldsValidation(record, tn)) {
return false
}
}
} }
} else { } else {
for (const [_, o] of Object.entries(validateInfos)) { for (const [_, o] of Object.entries(validateInfos)) {
@ -258,10 +264,11 @@ function remapColNames(batchData: any[], columns: ColumnType[]) {
) )
} }
function missingRequiredColumnsValidation() { function missingRequiredColumnsValidation(tn: string) {
const missingRequiredColumns = columns.value.filter( const missingRequiredColumns = columns.value.filter(
(c: Record<string, any>) => (c: Record<string, any>) =>
(c.pk ? !c.ai && !c.cdf : !c.cdf && c.rqd) && !srcDestMapping.value.some((r) => r.destCn === c.title), (c.pk ? !c.ai && !c.cdf : !c.cdf && c.rqd) &&
!srcDestMapping.value[tn].some((r: Record<string, any>) => r.destCn === c.title),
) )
if (missingRequiredColumns.length) { if (missingRequiredColumns.length) {
@ -272,16 +279,15 @@ function missingRequiredColumnsValidation() {
return true return true
} }
function atLeastOneEnabledValidation() { function atLeastOneEnabledValidation(tn: string) {
if (srcDestMapping.value.filter((v) => v.enabled === true).length === 0) { if (srcDestMapping.value[tn].filter((v: Record<string, any>) => v.enabled === true).length === 0) {
message.error(t('msg.error.selectAtleastOneColumn')) message.error(t('msg.error.selectAtleastOneColumn'))
return false return false
} }
return true return true
} }
function fieldsValidation(record: Record<string, any>) { function fieldsValidation(record: Record<string, any>, tn: string) {
// if it is not selected, then pass validation // if it is not selected, then pass validation
if (!record.enabled) { if (!record.enabled) {
return true return true
@ -292,7 +298,7 @@ function fieldsValidation(record: Record<string, any>) {
return false return false
} }
if (srcDestMapping.value.filter((v) => v.destCn === record.destCn).length > 1) { if (srcDestMapping.value[tn].filter((v: Record<string, any>) => v.destCn === record.destCn).length > 1) {
message.error(t('msg.error.duplicateMappingFound')) message.error(t('msg.error.duplicateMappingFound'))
return false return false
} }
@ -366,11 +372,13 @@ function updateImportTips(projectName: string, tableName: string, progress: numb
async function importTemplate() { async function importTemplate() {
if (importDataOnly) { if (importDataOnly) {
// validate required columns for (const table of data.tables) {
if (!missingRequiredColumnsValidation()) return // validate required columns
if (!missingRequiredColumnsValidation(table.table_name)) return
// validate at least one column needs to be selected // validate at least one column needs to be selected
if (!atLeastOneEnabledValidation()) return if (!atLeastOneEnabledValidation(table.table_name)) return
}
try { try {
isImporting.value = true isImporting.value = true
@ -386,7 +394,7 @@ async function importTemplate() {
for (let i = 0, progress = 0; i < total; i += maxRowsToParse) { for (let i = 0, progress = 0; i < total; i += maxRowsToParse) {
const batchData = data.slice(i, i + maxRowsToParse).map((row: Record<string, any>) => const batchData = data.slice(i, i + maxRowsToParse).map((row: Record<string, any>) =>
srcDestMapping.value.reduce((res: Record<string, any>, col: Record<string, any>) => { srcDestMapping.value[k].reduce((res: Record<string, any>, col: Record<string, any>) => {
if (col.enabled && col.destCn) { if (col.enabled && col.destCn) {
const v = columns.value.find((c: Record<string, any>) => c.title === col.destCn) as Record<string, any> const v = columns.value.find((c: Record<string, any>) => c.title === col.destCn) as Record<string, any>
let input = row[col.srcCn] let input = row[col.srcCn]
@ -537,18 +545,23 @@ async function importTemplate() {
} }
function mapDefaultColumns() { function mapDefaultColumns() {
srcDestMapping.value = [] srcDestMapping.value = {}
for (const col of importColumns[0]) { for (let i = 0; i < data.tables.length; i++) {
const o = { srcCn: col.column_name, destCn: '', enabled: true } for (const col of importColumns[i]) {
if (columns.value) { const o = { srcCn: col.column_name, destCn: '', enabled: true }
const tableColumn = columns.value.find((c) => c.column_name === col.column_name) if (columns.value) {
if (tableColumn) { const tableColumn = columns.value.find((c) => c.column_name === col.column_name)
o.destCn = tableColumn.title as string if (tableColumn) {
} else { o.destCn = tableColumn.title as string
o.enabled = false } else {
o.enabled = false
}
}
if (!(data.tables[i].table_name in srcDestMapping.value)) {
srcDestMapping.value[data.tables[i].table_name] = []
} }
srcDestMapping.value[data.tables[i].table_name].push(o)
} }
srcDestMapping.value.push(o)
} }
} }
@ -608,7 +621,7 @@ function isSelectDisabled(uidt: string, disableSelect = false) {
v-if="srcDestMapping" v-if="srcDestMapping"
class="template-form" class="template-form"
row-class-name="template-form-row" row-class-name="template-form-row"
:data-source="srcDestMapping" :data-source="srcDestMapping[table.table_name]"
:columns="srcDestMappingColumns" :columns="srcDestMappingColumns"
:pagination="false" :pagination="false"
> >

Loading…
Cancel
Save