Browse Source

refactor(gui-v2): add table rename validation

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/2830/head
Pranav C 2 years ago
parent
commit
c58e09e08c
  1. 49
      packages/nc-gui-v2/components/dlg/TableRename.vue
  2. 5
      packages/nc-gui-v2/utils/validation.ts

49
packages/nc-gui-v2/components/dlg/TableRename.vue

@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { onMounted, watchEffect } from '@vue/runtime-core' import { watchEffect } from '@vue/runtime-core'
import { Form } from 'ant-design-vue' import { Form } from 'ant-design-vue'
import type { TableType } from 'nocodb-sdk' import type { TableType } from 'nocodb-sdk'
import { useToast } from 'vue-toastification' import { useToast } from 'vue-toastification'
@ -31,24 +31,33 @@ const { loadTables } = useProject()
const { project, tables } = useProject() const { project, tables } = useProject()
const prefix = computed(() => project?.value?.prefix || '') const prefix = computed(() => project?.value?.prefix || '')
const validateDuplicateAlias = (v: string) => {
return (tables?.value || []).every((t) => t.title !== (v || '')) || 'Duplicate table alias'
}
const validateLeadingOrTrailingWhiteSpace = (v: string) => {
return !/^\s+|\s+$/.test(v) || 'Leading or trailing whitespace not allowed in table name'
}
const validateDuplicate = (v: string) => {
return (tables?.value || []).every((t) => t.table_name.toLowerCase() !== (v || '').toLowerCase()) || 'Duplicate table name'
}
const inputEl = $ref<any>() const inputEl = $ref<any>()
let loading = $ref(false)
const useForm = Form.useForm const useForm = Form.useForm
const formState = reactive({ const formState = reactive({
title: '', title: '',
}) })
const validators = computed(() => { const validators = computed(() => {
return { return {
title: [validateTableName, validateDuplicateAlias], title: [
table_name: [validateTableName], validateTableName,
{
validator: (rule: any, value: any, callback: (errMsg?: string) => void) => {
if (/^\s+|\s+$/.test(value)) {
callback('Leading or trailing whitespace not allowed in table name')
}
if (
!(tables?.value || []).every(
(t) => t.id === tableMeta.id || t.table_name.toLowerCase() !== (value || '').toLowerCase(),
)
) {
callback('Duplicate table alias')
}
callback()
},
},
],
} }
}) })
const { resetFields, validate, validateInfos } = useForm(formState, validators) const { resetFields, validate, validateInfos } = useForm(formState, validators)
@ -64,38 +73,36 @@ watchEffect(() => {
}) })
const renameTable = async () => { const renameTable = async () => {
loading = true
try { try {
await $api.dbTable.update(tableMeta?.id as string, { await $api.dbTable.update(tableMeta?.id as string, {
title: formState.title, title: formState.title,
}) })
dialogShow.value = false
loadTables() loadTables()
updateTab({ id: tableMeta?.id }, { title: formState.title }) updateTab({ id: tableMeta?.id }, { title: formState.title })
toast.success('Table renamed successfully') toast.success('Table renamed successfully')
$e('a:table:rename') $e('a:table:rename')
dialogShow.value = false
} catch (e) { } catch (e) {
toast.error(await extractSdkResponseErrorMsg(e)) toast.error(await extractSdkResponseErrorMsg(e))
} }
loading = false
} }
</script> </script>
<template> <template>
<a-modal v-model:visible="dialogShow" @keydown.esc="dialogShow = false" @finish="renameTable" title="Rename Table"> <a-modal v-model:visible="dialogShow" title="Rename Table" @keydown.esc="dialogShow = false" @finish="renameTable">
<template #footer> <template #footer>
<a-button key="back" @click="dialogShow = false">{{ $t('general.cancel') }}</a-button> <a-button key="back" @click="dialogShow = false">{{ $t('general.cancel') }}</a-button>
<a-button key="submit" type="primary" @click="renameTable">{{ $t('general.submit') }}</a-button> <a-button key="submit" type="primary" :loading="loading" @click="renameTable">{{ $t('general.submit') }}</a-button>
</template> </template>
<div class="pl-10 pr-10 pt-5"> <div class="pl-10 pr-10 pt-5">
<a-form :model="formState" name="create-new-table-form"> <a-form :model="formState" name="create-new-table-form">
<!-- hint="Enter table name" --> <!-- hint="Enter table name" -->
<div class="mb-2">Table Name</div> <div class="mb-2">Table Name</div>
<a-form-item v-bind="validateInfos.title"> <a-form-item v-bind="validateInfos.title">
<a-input <a-input ref="inputEl" v-model:value="formState.title" hide-details :placeholder="$t('msg.info.enterTableName')" />
ref="inputEl"
v-model:value="formState.title"
hide-details
:placeholder="$t('msg.info.enterTableName')"
/>
</a-form-item> </a-form-item>
</a-form> </a-form>
</div> </div>

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

@ -78,7 +78,12 @@ export const projectTitleValidator = {
callback() callback()
}, },
} }
export const fieldRequiredValidator = { export const fieldRequiredValidator = {
required: true, required: true,
message: 'Field is required', message: 'Field is required',
} }
export const getRequiredValidator = (field = 'Field') => ({
required: true,
message: `${field} is required`,
})

Loading…
Cancel
Save