mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
4.3 KiB
152 lines
4.3 KiB
2 years ago
|
<script setup lang="ts">
|
||
2 years ago
|
import { watchEffect } from '@vue/runtime-core'
|
||
2 years ago
|
import { Form, message } from 'ant-design-vue'
|
||
2 years ago
|
import type { TableType } from 'nocodb-sdk'
|
||
2 years ago
|
import { useI18n } from 'vue-i18n'
|
||
2 years ago
|
import { useMetas, useProject, useTabs } from '#imports'
|
||
2 years ago
|
import { extractSdkResponseErrorMsg, validateTableName } from '~/utils'
|
||
2 years ago
|
import { useNuxtApp } from '#app'
|
||
2 years ago
|
|
||
|
interface Props {
|
||
2 years ago
|
modelValue?: boolean
|
||
|
tableMeta: TableType
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const { modelValue = false, tableMeta } = defineProps<Props>()
|
||
|
|
||
|
const emit = defineEmits(['update:modelValue', 'updated'])
|
||
|
|
||
|
const { t } = useI18n()
|
||
|
|
||
2 years ago
|
const { $e, $api } = useNuxtApp()
|
||
2 years ago
|
|
||
|
const { setMeta } = useMetas()
|
||
|
|
||
2 years ago
|
const dialogShow = computed({
|
||
|
get() {
|
||
2 years ago
|
return modelValue
|
||
2 years ago
|
},
|
||
|
set(v) {
|
||
2 years ago
|
emit('update:modelValue', v)
|
||
|
},
|
||
|
})
|
||
2 years ago
|
|
||
2 years ago
|
const { updateTab } = useTabs()
|
||
2 years ago
|
const { loadTables, tables, project, isMysql, isMssql, isPg } = useProject()
|
||
2 years ago
|
|
||
2 years ago
|
const inputEl = $ref<any>()
|
||
2 years ago
|
let loading = $ref(false)
|
||
2 years ago
|
const useForm = Form.useForm
|
||
2 years ago
|
const formState = reactive({
|
||
2 years ago
|
title: '',
|
||
|
})
|
||
2 years ago
|
const validators = computed(() => {
|
||
|
return {
|
||
2 years ago
|
title: [
|
||
|
validateTableName,
|
||
2 years ago
|
{
|
||
|
validator: (rule: any, value: any) => {
|
||
|
return new Promise<void>((resolve, reject) => {
|
||
|
let tableNameLengthLimit = 255
|
||
|
if (isMysql) {
|
||
|
tableNameLengthLimit = 64
|
||
|
} else if (isPg) {
|
||
|
tableNameLengthLimit = 63
|
||
|
} else if (isMssql) {
|
||
|
tableNameLengthLimit = 128
|
||
|
}
|
||
|
const projectPrefix = project?.value?.prefix || ''
|
||
|
if ((projectPrefix + value).length > tableNameLengthLimit) {
|
||
|
return reject(new Error(`Table name exceeds ${tableNameLengthLimit} characters`))
|
||
|
}
|
||
|
resolve()
|
||
|
})
|
||
|
},
|
||
|
},
|
||
2 years ago
|
{
|
||
2 years ago
|
validator: (rule: any, value: any) => {
|
||
|
return new Promise<void>((resolve, reject) => {
|
||
|
if (/^\s+|\s+$/.test(value)) {
|
||
|
return reject(new Error('Leading or trailing whitespace not allowed in table name'))
|
||
|
}
|
||
|
if (
|
||
|
!(tables?.value || []).every(
|
||
|
(t) => t.id === tableMeta.id || t.table_name.toLowerCase() !== (value || '').toLowerCase(),
|
||
|
)
|
||
|
) {
|
||
|
return reject(new Error('Duplicate table alias'))
|
||
|
}
|
||
|
resolve()
|
||
|
})
|
||
2 years ago
|
},
|
||
|
},
|
||
|
],
|
||
2 years ago
|
}
|
||
|
})
|
||
2 years ago
|
const { validateInfos } = useForm(formState, validators)
|
||
2 years ago
|
|
||
|
watchEffect(() => {
|
||
2 years ago
|
if (tableMeta?.title) formState.title = tableMeta?.title
|
||
2 years ago
|
// todo: replace setTimeout and follow better approach
|
||
|
nextTick(() => {
|
||
2 years ago
|
const input = inputEl?.$el
|
||
|
input.setSelectionRange(0, formState.title.length)
|
||
|
input.focus()
|
||
|
})
|
||
|
})
|
||
2 years ago
|
|
||
|
const renameTable = async () => {
|
||
2 years ago
|
loading = true
|
||
2 years ago
|
try {
|
||
|
await $api.dbTable.update(tableMeta?.id as string, {
|
||
2 years ago
|
project_id: tableMeta?.project_id,
|
||
2 years ago
|
table_name: formState.title,
|
||
2 years ago
|
})
|
||
2 years ago
|
dialogShow.value = false
|
||
2 years ago
|
loadTables()
|
||
|
updateTab({ id: tableMeta?.id }, { title: formState.title })
|
||
2 years ago
|
|
||
2 years ago
|
// update metas
|
||
|
setMeta(await $api.dbTable.read(tableMeta?.id as string))
|
||
|
|
||
2 years ago
|
// Table renamed successfully
|
||
|
message.success(t('msg.success.tableRenamed'))
|
||
2 years ago
|
$e('a:table:rename')
|
||
2 years ago
|
dialogShow.value = false
|
||
2 years ago
|
} catch (e: any) {
|
||
|
message.error(await extractSdkResponseErrorMsg(e))
|
||
2 years ago
|
}
|
||
2 years ago
|
loading = false
|
||
2 years ago
|
}
|
||
2 years ago
|
</script>
|
||
|
|
||
|
<template>
|
||
2 years ago
|
<a-modal
|
||
|
v-model:visible="dialogShow"
|
||
|
:title="$t('activity.renameTable')"
|
||
2 years ago
|
:mask-closable="false"
|
||
2 years ago
|
@keydown.esc="dialogShow = false"
|
||
|
@finish="renameTable"
|
||
|
>
|
||
2 years ago
|
<template #footer>
|
||
2 years ago
|
<a-button key="back" @click="dialogShow = false">{{ $t('general.cancel') }}</a-button>
|
||
2 years ago
|
<a-button key="submit" type="primary" :loading="loading" @click="renameTable">{{ $t('general.submit') }}</a-button>
|
||
2 years ago
|
</template>
|
||
|
<div class="pl-10 pr-10 pt-5">
|
||
|
<a-form :model="formState" name="create-new-table-form">
|
||
|
<!-- hint="Enter table name" -->
|
||
2 years ago
|
<div class="mb-2">{{ $t('msg.info.enterTableName') }}</div>
|
||
2 years ago
|
<a-form-item v-bind="validateInfos.title">
|
||
2 years ago
|
<a-input
|
||
|
ref="inputEl"
|
||
|
v-model:value="formState.title"
|
||
|
hide-details
|
||
|
:placeholder="$t('msg.info.enterTableName')"
|
||
|
@keydown.enter="renameTable"
|
||
|
/>
|
||
2 years ago
|
</a-form-item>
|
||
|
</a-form>
|
||
2 years ago
|
</div>
|
||
|
</a-modal>
|
||
|
</template>
|