From d1ef2444d9b0793e42ec50e96cb1dd63cb40e82f Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Sat, 23 Jul 2022 19:10:09 +0800 Subject: [PATCH] feat(gui-v2): adapter & parseAndExtractData --- .../nc-gui-v2/components/dlg/FileImport.vue | 69 ++++++++++--------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/packages/nc-gui-v2/components/dlg/FileImport.vue b/packages/nc-gui-v2/components/dlg/FileImport.vue index ca961d6b9d..170e6d4da7 100644 --- a/packages/nc-gui-v2/components/dlg/FileImport.vue +++ b/packages/nc-gui-v2/components/dlg/FileImport.vue @@ -10,6 +10,7 @@ import MdiLinkVariantIcon from '~icons/mdi/link-variant' import MdiCodeJSONIcon from '~icons/mdi/code-json' import { fieldRequiredValidator, importUrlValidator } from '~/utils/validation' import { extractSdkResponseErrorMsg } from '~/utils/errorUtils' +import { JSONTemplateAdapter, JSONUrlTemplateAdapter, ExcelTemplateAdapter, ExcelUrlTemplateAdapter } from '~/utils/parsers' import { useProject } from '#imports' const { t } = useI18n() @@ -32,19 +33,12 @@ const useForm = Form.useForm const importState = reactive({ fileList: [], - url: { - value: '', - parserConfig: { - maxRowsToParse: 500, - }, - }, - json: { - value: {}, - parserConfig: { - maxRowsToParse: 500, - normalizeNested: true, - importData: true, - }, + url: '', + jsonEditor: {}, + parserConfig: { + maxRowsToParse: 500, + normalizeNested: true, + importData: true, }, }) @@ -126,12 +120,12 @@ const handleImport = async () => { } else if (activeKey.value === 'url') { try { await validate() - await parseAndExtractData('url', importState.url.value, '') + await parseAndExtractData('url', importState.url, '') } catch (e: any) { toast.error(await extractSdkResponseErrorMsg(e)) } } else if (activeKey.value === 'json') { - await parseAndExtractData('string', JSON.stringify(importState.json.value), '') + await parseAndExtractData('jsonEditor', JSON.stringify(importState.jsonEditor), '') } } @@ -143,27 +137,34 @@ const populateUniqueTableName = () => { return `Sheet${c}` } +const getAdapter: any = (name: string, val: any) => { + if (importType === 'excel' || importType === 'csv') { + return { + file: new ExcelTemplateAdapter(name, val, importState.parserConfig), + url: new ExcelUrlTemplateAdapter(val, importState.parserConfig), + } + } else if (importType === 'json') { + return { + file: new JSONTemplateAdapter(name, val, importState.parserConfig), + url: new JSONUrlTemplateAdapter(val, importState.parserConfig), + jsonEditor: new JSONTemplateAdapter(name, val, importState.parserConfig), + } + } + return {} +} + const parseAndExtractData = async (type: string, val: any, name: string) => { try { let templateGenerator templateData.value = null importData.value = null - // switch (type) { - // case 'file': - // templateGenerator = new JSONTemplateAdapter(name, val, parserConfig) - // break - // case 'url': - // templateGenerator = new JSONUrlTemplateAdapter(val, this.$store, this.parserConfig, this.$api) - // break - // case 'string': - // templateGenerator = new JSONTemplateAdapter(name, val, this.parserConfig) - // break - // } - // await templateGenerator.init() - // templateGenerator.parse() - // templateData.value = templateGenerator.getTemplate() - // templateData.value.tables[0].table_name = this.populateUniqueTableName() - // importData.value = templateGenerator.getData() + templateGenerator = getAdapter(name, val)[type] + + await templateGenerator.init() + templateGenerator.parse() + templateData.value = templateGenerator.getTemplate() + templateData.value.tables[0].table_name = populateUniqueTableName() + importData.value = templateGenerator.getData() templateEditorModal.value = true } catch (e: any) { console.log(e) @@ -215,7 +216,7 @@ const parseAndExtractData = async (type: string, val: any, name: string) => {
- +
@@ -227,8 +228,8 @@ const parseAndExtractData = async (type: string, val: any, name: string) => {
- - + +