Browse Source

feat(gui-v2): excel / csv validators

pull/2795/head
Wing-Kam Wong 2 years ago
parent
commit
c759b2b690
  1. 9
      packages/nc-gui-v2/components/dlg/QuickImport.vue
  2. 18
      packages/nc-gui-v2/utils/validation.ts

9
packages/nc-gui-v2/components/dlg/QuickImport.vue

@ -8,7 +8,7 @@ import MdiFileIcon from '~icons/mdi/file-plus-outline'
import MdiFileUploadOutlineIcon from '~icons/mdi/file-upload-outline'
import MdiLinkVariantIcon from '~icons/mdi/link-variant'
import MdiCodeJSONIcon from '~icons/mdi/code-json'
import { fieldRequiredValidator, importUrlValidator } from '~/utils/validation'
import { fieldRequiredValidator, importCsvUrlValidator, importExcelUrlValidator, importUrlValidator } from '~/utils/validation'
import { extractSdkResponseErrorMsg } from '~/utils/errorUtils'
import { ExcelTemplateAdapter, ExcelUrlTemplateAdapter, JSONTemplateAdapter, JSONUrlTemplateAdapter } from '~/utils/parsers'
import { useProject } from '#imports'
@ -47,7 +47,7 @@ const importState = reactive({
const validators = computed(() => {
return {
url: [fieldRequiredValidator, importUrlValidator],
url: [fieldRequiredValidator, importUrlValidator, importType === 'csv' ? importCsvUrlValidator : importExcelUrlValidator],
maxRowsToParse: [fieldRequiredValidator],
}
})
@ -122,7 +122,10 @@ async function parseAndExtractData(val: any, name: string) {
templateData.value = null
importData.value = null
const templateGenerator: any = getAdapter(name, val)
if (!templateGenerator) {
toast.error('Template Generator cannot be found!')
return
}
await templateGenerator.init()
templateGenerator.parse()
templateData.value = templateGenerator.getTemplate()

18
packages/nc-gui-v2/utils/validation.ts

@ -101,3 +101,21 @@ export const importUrlValidator = {
callback()
},
}
export const importCsvUrlValidator = {
validator: (rule: any, value: any, callback: (errMsg?: string) => void) => {
if (value && !/.*\.(csv)/.test(value)) {
callback('Target file is not an accepted file type. The accepted file type is .csv!')
}
callback()
},
}
export const importExcelUrlValidator = {
validator: (rule: any, value: any, callback: (errMsg?: string) => void) => {
if (value && !/.*\.(xls|xlsx|xlsm|ods|ots)/.test(value)) {
callback('Target file is not an accepted file type. The accepted file types are .xls, .xlsx, .xlsm, .ods, .ots!')
}
callback()
},
}

Loading…
Cancel
Save