From 6842841d9789675190987f14c46309091787d384 Mon Sep 17 00:00:00 2001 From: arc-alex Date: Thu, 17 Oct 2024 15:34:58 +0200 Subject: [PATCH] automatically add existing custom fields to import map --- .../sources-panel/import-map/ImportMapForm.tsx | 12 ++++++++++-- .../sources-panel/import-map/importMapUtils.ts | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/apps/client/src/features/app-settings/panel/sources-panel/import-map/ImportMapForm.tsx b/apps/client/src/features/app-settings/panel/sources-panel/import-map/ImportMapForm.tsx index f04ad54037..5c82ccbcad 100644 --- a/apps/client/src/features/app-settings/panel/sources-panel/import-map/ImportMapForm.tsx +++ b/apps/client/src/features/app-settings/panel/sources-panel/import-map/ImportMapForm.tsx @@ -5,12 +5,19 @@ import { IoAdd } from '@react-icons/all-files/io5/IoAdd'; import { IoTrash } from '@react-icons/all-files/io5/IoTrash'; import { ImportMap } from 'ontime-utils'; +import useCustomFields from '../../../../../common/hooks-query/useCustomFields'; import { isAlphanumeric } from '../../../../../common/utils/regex'; import * as Panel from '../../PanelUtils'; import useGoogleSheet from '../useGoogleSheet'; import { useSheetStore } from '../useSheetStore'; -import { convertToImportMap, getPersistedOptions, NamedImportMap, persistImportMap } from './importMapUtils'; +import { + addExistingCustomFields, + convertToImportMap, + getPersistedOptions, + NamedImportMap, + persistImportMap, +} from './importMapUtils'; import style from '../SourcesPanel.module.scss'; @@ -24,7 +31,8 @@ interface ImportMapFormProps { export default function ImportMapForm(props: ImportMapFormProps) { const { hasErrors, isSpreadsheet, onCancel, onSubmitExport, onSubmitImport } = props; - const namedImportMap = getPersistedOptions(); + const { data } = useCustomFields(); + const namedImportMap = addExistingCustomFields(getPersistedOptions(), data); const { revoke } = useGoogleSheet(); const { control, diff --git a/apps/client/src/features/app-settings/panel/sources-panel/import-map/importMapUtils.ts b/apps/client/src/features/app-settings/panel/sources-panel/import-map/importMapUtils.ts index a3fb757151..de7bf97302 100644 --- a/apps/client/src/features/app-settings/panel/sources-panel/import-map/importMapUtils.ts +++ b/apps/client/src/features/app-settings/panel/sources-panel/import-map/importMapUtils.ts @@ -1,3 +1,4 @@ +import { CustomFields } from 'ontime-types'; import { ImportCustom, ImportMap } from 'ontime-utils'; export type NamedImportMap = typeof namedImportMap; @@ -82,3 +83,19 @@ export function getPersistedOptions(): NamedImportMap { return namedImportMap; } } + +export function addExistingCustomFields(importMap: NamedImportMap, customFields: CustomFields): NamedImportMap { + const custom = importMap.custom; + Object.entries(customFields).forEach(([key, { label }]) => { + if ( + !custom.some((ic) => { + return ic.ontimeName === key; + }) + ) { + // const ontimeName = key as unknown as string; + custom.push({ ontimeName: key, importName: label }); + } + }); + + return importMap; +}