Browse Source

feat(gui-v2): excel / csv validators

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

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

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

@ -101,3 +101,21 @@ export const importUrlValidator = {
callback() 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