Browse Source

refactor: use switch case instead of nested ternary for readability

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/4696/head
Pranav C 2 years ago
parent
commit
2b1ca8d7a1
  1. 67
      packages/nc-gui/components/dashboard/settings/data-sources/CreateBase.vue

67
packages/nc-gui/components/dashboard/settings/data-sources/CreateBase.vue

@ -8,7 +8,7 @@ import {
DefaultConnection, DefaultConnection,
SQLiteConnection, SQLiteConnection,
SSLUsage, SSLUsage,
clientTypes, clientTypes as _clientTypes,
computed, computed,
extractSdkResponseErrorMsg, extractSdkResponseErrorMsg,
fieldRequiredValidator, fieldRequiredValidator,
@ -21,6 +21,7 @@ import {
readFile, readFile,
ref, ref,
useApi, useApi,
useGlobal,
useI18n, useI18n,
useNuxtApp, useNuxtApp,
watch, watch,
@ -30,6 +31,8 @@ const { connectionType } = defineProps<{ connectionType: ClientType }>()
const emit = defineEmits(['baseCreated']) const emit = defineEmits(['baseCreated'])
const { appInfo } = useGlobal()
const { project, loadProject } = useProject() const { project, loadProject } = useProject()
const useForm = Form.useForm const useForm = Form.useForm
@ -68,23 +71,29 @@ const customFormState = ref<ProjectCreateForm>({
extraParameters: [], extraParameters: [],
}) })
const clientTypes = computed(() => {
return _clientTypes.filter((type) => {
return appInfo.value?.ee || type.value !== ClientType.SNOWFLAKE
})
})
const validators = computed(() => { const validators = computed(() => {
return { let clientValidations: Record<string, any[]> = {
'title': [ 'dataSource.connection.host': [fieldRequiredValidator()],
{ 'dataSource.connection.port': [fieldRequiredValidator()],
required: true, 'dataSource.connection.user': [fieldRequiredValidator()],
message: 'Base name is required', 'dataSource.connection.password': [fieldRequiredValidator()],
}, 'dataSource.connection.database': [fieldRequiredValidator()],
projectTitleValidator, }
],
'extraParameters': [extraParameterValidator], switch (formState.dataSource.client) {
'dataSource.client': [fieldRequiredValidator()], case ClientType.SQLITE:
...(formState.dataSource.client === ClientType.SQLITE clientValidations = {
? {
'dataSource.connection.connection.filename': [fieldRequiredValidator()], 'dataSource.connection.connection.filename': [fieldRequiredValidator()],
} }
: formState.dataSource.client === ClientType.SNOWFLAKE break
? { case ClientType.SNOWFLAKE:
clientValidations = {
'dataSource.connection.account': [fieldRequiredValidator()], 'dataSource.connection.account': [fieldRequiredValidator()],
'dataSource.connection.username': [fieldRequiredValidator()], 'dataSource.connection.username': [fieldRequiredValidator()],
'dataSource.connection.password': [fieldRequiredValidator()], 'dataSource.connection.password': [fieldRequiredValidator()],
@ -92,18 +101,24 @@ const validators = computed(() => {
'dataSource.connection.database': [fieldRequiredValidator()], 'dataSource.connection.database': [fieldRequiredValidator()],
'dataSource.connection.schema': [fieldRequiredValidator()], 'dataSource.connection.schema': [fieldRequiredValidator()],
} }
: { break
'dataSource.connection.host': [fieldRequiredValidator()], case ClientType.PG:
'dataSource.connection.port': [fieldRequiredValidator()], case ClientType.MSSQL:
'dataSource.connection.user': [fieldRequiredValidator()], clientValidations['dataSource.searchPath.0'] = [fieldRequiredValidator()]
'dataSource.connection.password': [fieldRequiredValidator()], break
'dataSource.connection.database': [fieldRequiredValidator()],
...([ClientType.PG, ClientType.MSSQL].includes(formState.dataSource.client)
? {
'dataSource.searchPath.0': [fieldRequiredValidator()],
} }
: {}),
}), return {
'title': [
{
required: true,
message: 'Project name is required',
},
projectTitleValidator,
],
'extraParameters': [extraParameterValidator],
'dataSource.client': [fieldRequiredValidator()],
...clientValidations,
} }
}) })

Loading…
Cancel
Save