Browse Source

fix: import logic

pull/1931/head
Wing-Kam Wong 3 years ago
parent
commit
80bf6a5226
  1. 70
      packages/nc-gui/components/templates/createProjectFromTemplateBtn.vue

70
packages/nc-gui/components/templates/createProjectFromTemplateBtn.vue

@ -14,6 +14,7 @@
<script> <script>
import colors from '~/mixins/colors' import colors from '~/mixins/colors'
import { SqlUiFactory } from 'nocodb-sdk';
export default { export default {
name: 'CreateProjectFromTemplateBtn', name: 'CreateProjectFromTemplateBtn',
@ -44,6 +45,7 @@ export default {
data() { data() {
return { return {
projectCreation: false, projectCreation: false,
tableCreation: false,
loaderMessagesIndex: 0, loaderMessagesIndex: 0,
loaderMessages: [ loaderMessages: [
'Setting up new database configs', 'Setting up new database configs',
@ -77,7 +79,7 @@ export default {
// this.$emit('useTemplate', type) // this.$emit('useTemplate', type)
this.projectCreation = true // this.projectCreation = true
let interv let interv
try { try {
interv = setInterval(() => { interv = setInterval(() => {
@ -85,7 +87,7 @@ export default {
this.$store.commit('loader/MutMessage', this.loaderMessages[this.loaderMessagesIndex]) this.$store.commit('loader/MutMessage', this.loaderMessages[this.loaderMessagesIndex])
}, 1000) }, 1000)
let projectId, prefix let project
if (this.importToProject) { if (this.importToProject) {
this.$store.commit('loader/MutMessage', 'Importing excel template') this.$store.commit('loader/MutMessage', 'Importing excel template')
@ -107,58 +109,73 @@ export default {
projectId = this.$route.params.project_id projectId = this.$route.params.project_id
prefix = this.$store.getters['project/GtrProjectPrefix'] prefix = this.$store.getters['project/GtrProjectPrefix']
} else { } else {
// Create an empty project
try { try {
// Create an empty project project = await this.$api.project.create({
const result = await this.$api.project.create({
title: this.templateData.title, title: this.templateData.title,
external: false external: false
}) })
prefix = result.prefix
clearInterval(interv)
await this.$store.dispatch('project/ActLoadProjectInfo')
this.projectReloading = false
if (!this.edit && !this.allSchemas) { if (!this.edit && !this.allSchemas) {
this.$router.push({ this.$router.push({
path: `/nc/${result.id}`, path: `/nc/${project.id}`,
query: { query: {
new: 1 new: 1
} }
}) })
} }
this.projectCreation = true
} catch (e) {
this.projectCreation = false
this.$toast
.error(await this._extractSdkResponseErrorMsg(e))
.goAway(3000)
} finally {
clearInterval(interv)
}
this.projectCreated = true if (!this.projectCreation) {
// failed to create project
return
}
// Create tables // Create tables
try {
for (var t of this.templateData.tables) { for (var t of this.templateData.tables) {
const table = await this.$api.dbTable.create(result.id, { // enrich system fields if not provided
// e.g. id, created_at, updated_at
const systemColumns = SqlUiFactory
.create({client: 'sqlite3'})
.getNewTableColumns()
.filter(c => c.column_name != "title")
const table = await this.$api.dbTable.create(project.id, {
table_name: t.ref_table_name, table_name: t.ref_table_name,
title: '', title: '',
columns: t.columns, columns: [...t.columns, ...systemColumns],
}); });
console.log(table) console.log(table)
t.table_title = table.title t.table_title = table.title
} }
this.tableCreation = true
} catch (e) { } catch (e) {
this.$toast this.$toast
.error(await this._extractSdkResponseErrorMsg(e)) .error(await this._extractSdkResponseErrorMsg(e))
.goAway(3000) .goAway(3000)
this.tableCreation = false
} finally {
clearInterval(interv)
} }
} }
clearInterval(interv)
// Bulk import data if (this.tableCreation) {
if (this.importData) { // Bulk import data
this.$store.commit('loader/MutMessage', 'Importing excel data to project') if (this.importData) {
await this.importDataToProject(this.templateData.title, prefix) this.$store.commit('loader/MutMessage', 'Importing excel data to project')
await this.importDataToProject(this.templateData.title, project.prefix)
}
this.$store.commit('loader/MutMessage', null)
this.projectReloading = false
this.$emit('success')
} }
this.$store.commit('loader/MutMessage', null)
this.projectReloading = false
this.$emit('success')
} catch (e) { } catch (e) {
console.log(e) console.log(e)
this.$toast.error(e.message).goAway(3000) this.$toast.error(e.message).goAway(3000)
@ -166,6 +183,7 @@ export default {
clearInterval(interv) clearInterval(interv)
} }
this.projectCreation = false this.projectCreation = false
this.tableCreation = false
}, },
async importDataToProject(projectName, prefix) { async importDataToProject(projectName, prefix) {
let total = 0 let total = 0

Loading…
Cancel
Save