From 18bf1344e1c081a65525fb115baa467effa586ea Mon Sep 17 00:00:00 2001 From: Azri Kahar <42867097+azrikahar@users.noreply.github.com> Date: Sat, 15 Oct 2022 12:15:27 +0800 Subject: [PATCH] Set special flags when configuring db-only fields (#15640) Co-authored-by: ian --- .../store/alterations/standard.ts | 20 +++---------- .../fields/components/field-select.vue | 4 ++- app/src/utils/get-special-for-type.test.ts | 28 +++++++++++++++++++ app/src/utils/get-special-for-type.ts | 16 +++++++++++ 4 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 app/src/utils/get-special-for-type.test.ts create mode 100644 app/src/utils/get-special-for-type.ts diff --git a/app/src/modules/settings/routes/data-model/field-detail/store/alterations/standard.ts b/app/src/modules/settings/routes/data-model/field-detail/store/alterations/standard.ts index a7e9510991..6db3b68eba 100644 --- a/app/src/modules/settings/routes/data-model/field-detail/store/alterations/standard.ts +++ b/app/src/modules/settings/routes/data-model/field-detail/store/alterations/standard.ts @@ -1,6 +1,7 @@ import { HelperFunctions, State, StateUpdates } from '../types'; import { getInterface } from '@/interfaces'; import { set } from 'lodash'; +import { getSpecialForType } from '@/utils/get-special-for-type'; export function applyChanges(updates: StateUpdates, _state: State, helperFn: HelperFunctions) { const { hasChanged } = helperFn; @@ -13,22 +14,9 @@ export function applyChanges(updates: StateUpdates, _state: State, helperFn: Hel function setSpecialForType(updates: StateUpdates) { const type = updates.field?.type; - switch (type) { - case 'json': - case 'csv': - case 'boolean': - set(updates, 'field.meta.special', ['cast-' + type]); - break; - case 'uuid': - case 'hash': - case 'geometry': - set(updates, 'field.meta.special', [type]); - break; - case undefined: - break; - default: - set(updates, 'field.meta.special', null); - } + if (type === undefined) return; + const special = getSpecialForType(type); + set(updates, 'field.meta.special', special); } function updateInterface(updates: StateUpdates, fn: HelperFunctions) { diff --git a/app/src/modules/settings/routes/data-model/fields/components/field-select.vue b/app/src/modules/settings/routes/data-model/fields/components/field-select.vue index 4f861b302d..6ee05f6614 100644 --- a/app/src/modules/settings/routes/data-model/fields/components/field-select.vue +++ b/app/src/modules/settings/routes/data-model/fields/components/field-select.vue @@ -152,6 +152,7 @@ import { getInterface } from '@/interfaces'; import { useRouter } from 'vue-router'; import { cloneDeep } from 'lodash'; import { getLocalTypeForField } from '@/utils/get-local-type'; +import { getSpecialForType } from '@/utils/get-special-for-type'; import { notify } from '@/utils/notify'; import { unexpectedError } from '@/utils/unexpected-error'; import { Field } from '@directus/shared/types'; @@ -309,7 +310,8 @@ export default defineComponent({ async function openFieldDetail() { if (!props.field.meta) { - await fieldsStore.updateField(props.field.collection, props.field.field, { meta: {} }); + const special = getSpecialForType(props.field.type); + await fieldsStore.updateField(props.field.collection, props.field.field, { meta: { special } }); } router.push(`/settings/data-model/${props.field.collection}/${props.field.field}`); diff --git a/app/src/utils/get-special-for-type.test.ts b/app/src/utils/get-special-for-type.test.ts new file mode 100644 index 0000000000..3ec8304f1a --- /dev/null +++ b/app/src/utils/get-special-for-type.test.ts @@ -0,0 +1,28 @@ +import { TYPES } from '@directus/shared/constants'; +import { expect, test } from 'vitest'; +import { getSpecialForType } from './get-special-for-type'; + +const castPrefixedSpecials = ['json', 'csv', 'boolean']; +const nonPrefixedSpecials = ['uuid', 'hash', 'geometry']; + +test('Returns cast-prefixed special array for json, csv, and boolean field types', () => { + const types = TYPES.filter((type) => castPrefixedSpecials.includes(type)); + for (const type of types) { + expect(getSpecialForType(type)).toStrictEqual(['cast-' + type]); + } +}); + +test('Returns special array for uuid, hash, and geometry field types', () => { + const types = TYPES.filter((type) => nonPrefixedSpecials.includes(type)); + for (const type of types) { + expect(getSpecialForType(type)).toStrictEqual([type]); + } +}); + +test('Returns null for other field types', () => { + const specials = [...castPrefixedSpecials, ...nonPrefixedSpecials]; + const types = TYPES.filter((type) => !specials.includes(type)); + for (const type of types) { + expect(getSpecialForType(type)).toStrictEqual(null); + } +}); diff --git a/app/src/utils/get-special-for-type.ts b/app/src/utils/get-special-for-type.ts new file mode 100644 index 0000000000..a5f953e700 --- /dev/null +++ b/app/src/utils/get-special-for-type.ts @@ -0,0 +1,16 @@ +import { Type } from '@directus/shared/types'; + +export function getSpecialForType(type: Type): string[] | null { + switch (type) { + case 'json': + case 'csv': + case 'boolean': + return ['cast-' + type]; + case 'uuid': + case 'hash': + case 'geometry': + return [type]; + default: + return null; + } +}