mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +00:00
* fix: update example data in JSON and XML import dialogs Replace placeholder data (Tove/Jani notes) with clearer contact records (name/email/phone) that better demonstrate the expected import format for new users. Fixes #2953 * changelog: add entry for import dialog example data fix Addresses review from @stribny: PR was missing a changelog entry. Added a bug-type entry under changelog/entries/unreleased/bug/ referencing issue #2953. --------- Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
218 lines
6.0 KiB
Vue
218 lines
6.0 KiB
Vue
<template>
|
|
<div>
|
|
<div class="control">
|
|
<template v-if="values.filename === ''">
|
|
<label class="control__label control__label--small">{{
|
|
$t('tableXMLImporter.fileLabel')
|
|
}}</label>
|
|
<div class="control__description">
|
|
{{ $t('tableXMLImporter.fileDescription') }}
|
|
<pre>
|
|
<data>
|
|
<record>
|
|
<name>John Doe</name>
|
|
<email>john@example.com</email>
|
|
<phone>123-456-7890</phone>
|
|
</record>
|
|
<record>
|
|
<name>Jane Smith</name>
|
|
<email>jane@example.com</email>
|
|
<phone>987-654-3210</phone>
|
|
</record>
|
|
</data></pre
|
|
>
|
|
</div>
|
|
</template>
|
|
<div class="control__elements">
|
|
<div class="file-upload margin-top-1">
|
|
<input
|
|
v-show="false"
|
|
ref="file"
|
|
type="file"
|
|
accept=".xml"
|
|
@change="select($event)"
|
|
/>
|
|
<Button
|
|
tag="a"
|
|
type="upload"
|
|
size="large"
|
|
class="file-upload__button"
|
|
:loading="state !== null"
|
|
icon="iconoir-cloud-upload"
|
|
@click.prevent="$refs.file.click($event)"
|
|
>
|
|
{{ $t('tableXMLImporter.chooseButton') }}
|
|
</Button>
|
|
<div v-if="state === null" class="file-upload__file">
|
|
{{ values.filename }}
|
|
</div>
|
|
<template v-else>
|
|
<ProgressBar
|
|
:value="fileLoadingProgress"
|
|
:show-value="state === 'loading'"
|
|
:status="
|
|
state === 'loading' ? $t('importer.loading') : stateTitle
|
|
"
|
|
/>
|
|
</template>
|
|
</div>
|
|
<div v-if="v$.values.filename.$error" class="error">
|
|
{{ v$.values.filename.$errors[0]?.$message }}
|
|
</div>
|
|
|
|
<div v-if="values.filename !== ''" class="control margin-top-1">
|
|
<slot name="upsertMapping" />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<Alert v-if="error !== ''" type="error">
|
|
<template #title> {{ $t('common.wrong') }} </template>
|
|
{{ error }}
|
|
</Alert>
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
import { required, helpers } from '@vuelidate/validators'
|
|
import { useVuelidate } from '@vuelidate/core'
|
|
import { useRuntimeConfig } from '#imports'
|
|
|
|
import form from '@baserow/modules/core/mixins/form'
|
|
import importer from '@baserow/modules/database/mixins/importer'
|
|
import { XMLParser } from '@baserow/modules/database/utils/xml'
|
|
|
|
export default {
|
|
name: 'TableXMLImporter',
|
|
mixins: [form, importer],
|
|
emits: ['changed', 'data', 'getData'],
|
|
setup() {
|
|
const config = useRuntimeConfig()
|
|
return { v$: useVuelidate({ $lazy: true }), config }
|
|
},
|
|
data() {
|
|
return {
|
|
values: {
|
|
filename: '',
|
|
},
|
|
rawData: null,
|
|
}
|
|
},
|
|
validations() {
|
|
return {
|
|
values: {
|
|
filename: {
|
|
required: helpers.withMessage(
|
|
this.$t('error.requiredField'),
|
|
required
|
|
),
|
|
},
|
|
},
|
|
}
|
|
},
|
|
methods: {
|
|
/**
|
|
* Method that is called when a file has been chosen. It will check if the file is
|
|
* not larger than 15MB. Otherwise it will take a long time and possibly a crash
|
|
* if so many entries have to be loaded into memory. If the file is valid, the
|
|
* contents will be loaded into memory and the reload method will be called which
|
|
* parses the content.
|
|
*/
|
|
select(event) {
|
|
if (event.target.files.length === 0) {
|
|
return
|
|
}
|
|
|
|
const file = event.target.files[0]
|
|
const maxSize =
|
|
parseInt(this.config.public.baserowMaxImportFileSizeMb, 10) *
|
|
1024 *
|
|
1024
|
|
|
|
if (file.size > maxSize) {
|
|
this.handleImporterError(
|
|
this.$t('tableXMLImporter.limitFileSize', {
|
|
limit: this.config.public.baserowMaxImportFileSizeMb,
|
|
})
|
|
)
|
|
this.values.filename = ''
|
|
} else {
|
|
this.resetImporterState()
|
|
this.fileLoadingProgress = 0
|
|
|
|
this.$emit('changed')
|
|
this.state = 'loading'
|
|
this.values.filename = file.name
|
|
const reader = new FileReader()
|
|
reader.addEventListener('progress', (event) => {
|
|
this.fileLoadingProgress = (event.loaded / event.total) * 100
|
|
})
|
|
reader.addEventListener('load', (event) => {
|
|
this.rawData = event.target.result
|
|
this.fileLoadingProgress = 100
|
|
this.reload()
|
|
})
|
|
reader.readAsText(event.target.files[0], 'utf-8')
|
|
}
|
|
},
|
|
async reload() {
|
|
const fileName = this.values.filename
|
|
this.resetImporterState()
|
|
this.values.filename = fileName
|
|
this.state = 'parsing'
|
|
await this.$ensureRender()
|
|
|
|
const xmlParser = new XMLParser()
|
|
xmlParser.parse(this.rawData)
|
|
|
|
await this.$ensureRender()
|
|
xmlParser.loadXML(6)
|
|
|
|
await this.$ensureRender()
|
|
const [rawHeader, xmlData, errors] = xmlParser.transform()
|
|
|
|
if (errors.length > 0) {
|
|
this.handleImporterError(
|
|
this.$t('tableXMLImporter.processingError', {
|
|
errors: errors.join('\n'),
|
|
})
|
|
)
|
|
return
|
|
}
|
|
|
|
if (xmlData.length === 0) {
|
|
this.handleImporterError(this.$t('tableXMLImporter.emptyError'))
|
|
return
|
|
}
|
|
|
|
const limit = parseInt(this.config.public.initialTableDataLimit, 10)
|
|
if (limit && xmlData.length > limit) {
|
|
this.handleImporterError(
|
|
this.$t('tableXMLImporter.limitError', { limit })
|
|
)
|
|
return
|
|
}
|
|
|
|
const header = this.prepareHeader(rawHeader, xmlData)
|
|
const getData = async () => {
|
|
await this.$ensureRender()
|
|
xmlParser.loadXML()
|
|
|
|
await this.$ensureRender()
|
|
const [, xmlData, errors] = xmlParser.transform()
|
|
|
|
if (errors.length > 0) {
|
|
throw new Error(errors)
|
|
}
|
|
|
|
return xmlData
|
|
}
|
|
this.state = null
|
|
const previewData = this.getPreview(header, xmlData)
|
|
|
|
this.$emit('getData', getData)
|
|
this.$emit('data', { header, previewData })
|
|
},
|
|
},
|
|
}
|
|
</script>
|