From 2b1ca8d7a1236af2da4711c7dbc12f3085dd991d Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 27 Dec 2022 14:18:03 +0530 Subject: [PATCH] refactor: use switch case instead of nested ternary for readability Signed-off-by: Pranav C --- .../settings/data-sources/CreateBase.vue | 69 +++++++++++-------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/packages/nc-gui/components/dashboard/settings/data-sources/CreateBase.vue b/packages/nc-gui/components/dashboard/settings/data-sources/CreateBase.vue index 9fe262966e..7bc8a9597f 100644 --- a/packages/nc-gui/components/dashboard/settings/data-sources/CreateBase.vue +++ b/packages/nc-gui/components/dashboard/settings/data-sources/CreateBase.vue @@ -8,7 +8,7 @@ import { DefaultConnection, SQLiteConnection, SSLUsage, - clientTypes, + clientTypes as _clientTypes, computed, extractSdkResponseErrorMsg, fieldRequiredValidator, @@ -21,6 +21,7 @@ import { readFile, ref, useApi, + useGlobal, useI18n, useNuxtApp, watch, @@ -30,6 +31,8 @@ const { connectionType } = defineProps<{ connectionType: ClientType }>() const emit = defineEmits(['baseCreated']) +const { appInfo } = useGlobal() + const { project, loadProject } = useProject() const useForm = Form.useForm @@ -68,42 +71,54 @@ const customFormState = ref({ extraParameters: [], }) +const clientTypes = computed(() => { + return _clientTypes.filter((type) => { + return appInfo.value?.ee || type.value !== ClientType.SNOWFLAKE + }) +}) + const validators = computed(() => { + let clientValidations: Record = { + 'dataSource.connection.host': [fieldRequiredValidator()], + 'dataSource.connection.port': [fieldRequiredValidator()], + 'dataSource.connection.user': [fieldRequiredValidator()], + 'dataSource.connection.password': [fieldRequiredValidator()], + 'dataSource.connection.database': [fieldRequiredValidator()], + } + + switch (formState.dataSource.client) { + case ClientType.SQLITE: + clientValidations = { + 'dataSource.connection.connection.filename': [fieldRequiredValidator()], + } + break + case ClientType.SNOWFLAKE: + clientValidations = { + 'dataSource.connection.account': [fieldRequiredValidator()], + 'dataSource.connection.username': [fieldRequiredValidator()], + 'dataSource.connection.password': [fieldRequiredValidator()], + 'dataSource.connection.warehouse': [fieldRequiredValidator()], + 'dataSource.connection.database': [fieldRequiredValidator()], + 'dataSource.connection.schema': [fieldRequiredValidator()], + } + break + case ClientType.PG: + case ClientType.MSSQL: + clientValidations['dataSource.searchPath.0'] = [fieldRequiredValidator()] + break + } + return { 'title': [ { required: true, - message: 'Base name is required', + message: 'Project name is required', }, projectTitleValidator, ], 'extraParameters': [extraParameterValidator], 'dataSource.client': [fieldRequiredValidator()], - ...(formState.dataSource.client === ClientType.SQLITE - ? { - 'dataSource.connection.connection.filename': [fieldRequiredValidator()], - } - : formState.dataSource.client === ClientType.SNOWFLAKE - ? { - 'dataSource.connection.account': [fieldRequiredValidator()], - 'dataSource.connection.username': [fieldRequiredValidator()], - 'dataSource.connection.password': [fieldRequiredValidator()], - 'dataSource.connection.warehouse': [fieldRequiredValidator()], - 'dataSource.connection.database': [fieldRequiredValidator()], - 'dataSource.connection.schema': [fieldRequiredValidator()], - } - : { - 'dataSource.connection.host': [fieldRequiredValidator()], - 'dataSource.connection.port': [fieldRequiredValidator()], - 'dataSource.connection.user': [fieldRequiredValidator()], - 'dataSource.connection.password': [fieldRequiredValidator()], - 'dataSource.connection.database': [fieldRequiredValidator()], - ...([ClientType.PG, ClientType.MSSQL].includes(formState.dataSource.client) - ? { - 'dataSource.searchPath.0': [fieldRequiredValidator()], - } - : {}), - }), + ...clientValidations, } })