Browse Source

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

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

47
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,13 +124,19 @@ 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
}
for (const record of srcDestMapping.value[tn]) {
if (!fieldsValidation(record, tn)) {
return false return false
} }
} }
}
} else { } else {
for (const [_, o] of Object.entries(validateInfos)) { for (const [_, o] of Object.entries(validateInfos)) {
if (o?.validateStatus) { if (o?.validateStatus) {
@ -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) {
for (const table of data.tables) {
// validate required columns // validate required columns
if (!missingRequiredColumnsValidation()) return 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,8 +545,9 @@ 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++) {
for (const col of importColumns[i]) {
const o = { srcCn: col.column_name, destCn: '', enabled: true } const o = { srcCn: col.column_name, destCn: '', enabled: true }
if (columns.value) { if (columns.value) {
const tableColumn = columns.value.find((c) => c.column_name === col.column_name) const tableColumn = columns.value.find((c) => c.column_name === col.column_name)
@ -548,7 +557,11 @@ function mapDefaultColumns() {
o.enabled = false o.enabled = false
} }
} }
srcDestMapping.value.push(o) 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)
}
} }
} }
@ -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