From 49f257cd6b2cea9857ed8bba0280507efce24f0c Mon Sep 17 00:00:00 2001 From: Siarhei Karol Date: Fri, 20 Dec 2024 15:52:35 +0300 Subject: [PATCH] refactoring after review --- src/common/constants/complexLookup.constants.ts | 8 ++++++++ src/common/helpers/complexLookup.helper.ts | 10 +++++++--- src/common/hooks/useComplexLookup.ts | 8 ++++---- .../ComplexLookupField/ComplexLookupField.tsx | 2 +- .../ComplexLookupField/ModalComplexLookup.tsx | 4 ++-- src/configs/complexLookup/complexLookup.config.ts | 4 ++-- .../common/helpers/complexLookup.helper.test.ts | 5 +++-- src/types/complexLookup.d.ts | 3 ++- 8 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/common/constants/complexLookup.constants.ts b/src/common/constants/complexLookup.constants.ts index e977f220..c02a2f05 100644 --- a/src/common/constants/complexLookup.constants.ts +++ b/src/common/constants/complexLookup.constants.ts @@ -50,3 +50,11 @@ export const COMPLEX_LOOKUPS_LINKED_FIELDS_MAPPING = { export const EMPTY_LINKED_DROPDOWN_OPTION_SUFFIX = 'empty'; export const VALUE_DIVIDER = ' ,'; export const __MOCK_URI_CHANGE_WHEN_IMPLEMENTING = '__MOCK_URI_CHANGE_WHEN_IMPLEMENTING'; + +export enum Authority { + Creator = 'creator', +} + +export enum AuthorityValidationTarget { + CreatorOfWork = 'CREATOR_OF_WORK', +} diff --git a/src/common/helpers/complexLookup.helper.ts b/src/common/helpers/complexLookup.helper.ts index 61faff6e..ad4a90c9 100644 --- a/src/common/helpers/complexLookup.helper.ts +++ b/src/common/helpers/complexLookup.helper.ts @@ -1,4 +1,5 @@ import { + AuthorityValidationTarget, COMPLEX_LOOKUPS_LINKED_FIELDS_MAPPING, EMPTY_LINKED_DROPDOWN_OPTION_SUFFIX, } from '@common/constants/complexLookup.constants'; @@ -67,14 +68,17 @@ export const getUpdatedSelectedEntries = ({ return selectedEntriesService.get(); }; -export const generateValidationRequestBody = (marcData: MarcDTO | null, target = 'CREATOR_OF_WORK') => { +export const generateValidationRequestBody = ( + marcData: MarcDTO | null, + target = AuthorityValidationTarget.CreatorOfWork, +) => { if (!marcData) return {}; const rawMarcEncoded = JSON.stringify(marcData?.parsedRecord?.content, null, 2); - const excapedString = rawMarcEncoded.replace(/\r/g, '\r').replace(/\n/g, '\n'); + const escapedString = rawMarcEncoded.replace(/\r/g, '\r').replace(/\n/g, '\n'); return { - rawMarc: excapedString, + rawMarc: escapedString, target, }; }; diff --git a/src/common/hooks/useComplexLookup.ts b/src/common/hooks/useComplexLookup.ts index 559b97cf..bb2463fb 100644 --- a/src/common/hooks/useComplexLookup.ts +++ b/src/common/hooks/useComplexLookup.ts @@ -6,7 +6,7 @@ import { getUpdatedSelectedEntries, updateLinkedFieldValue, } from '@common/helpers/complexLookup.helper'; -import { __MOCK_URI_CHANGE_WHEN_IMPLEMENTING } from '@common/constants/complexLookup.constants'; +import { __MOCK_URI_CHANGE_WHEN_IMPLEMENTING, Authority } from '@common/constants/complexLookup.constants'; import { AdvancedFieldType } from '@common/constants/uiControls.constants'; import { useInputsState, useMarcPreviewState, useProfileState, useStatusState, useUIState } from '@src/store'; import { UserNotificationFactory } from '@common/services/userNotification'; @@ -22,13 +22,13 @@ export const useComplexLookup = ({ entry, value, lookupConfig, - baseLabelType = 'creator', + authority = Authority.Creator, onChange, }: { entry: SchemaEntry; value?: UserValueContents[]; lookupConfig: ComplexLookupsConfigEntry; - baseLabelType?: string; + authority?: string; onChange: (uuid: string, contents: Array) => void; }) => { const { selectedEntriesService } = useServicesContext() as Required; @@ -84,7 +84,7 @@ export const useComplexLookup = ({ return makeRequest({ url: endpoints.validation ?? AUTHORITY_ASSIGNMENT_CHECK_API_ENDPOINT, method: 'POST', - body: generateValidationRequestBody(marcData, validationTarget?.[baseLabelType]), + body: generateValidationRequestBody(marcData, validationTarget?.[authority]), }); }; diff --git a/src/components/ComplexLookupField/ComplexLookupField.tsx b/src/components/ComplexLookupField/ComplexLookupField.tsx index 7c792720..17288843 100644 --- a/src/components/ComplexLookupField/ComplexLookupField.tsx +++ b/src/components/ComplexLookupField/ComplexLookupField.tsx @@ -30,7 +30,7 @@ export const ComplexLookupField: FC = ({ value = undefined, id, entry, on entry, value, lookupConfig, - baseLabelType: layout?.baseLabelType, + authority: layout?.baseLabelType, onChange, }); diff --git a/src/components/ComplexLookupField/ModalComplexLookup.tsx b/src/components/ComplexLookupField/ModalComplexLookup.tsx index bacd7f26..56b8991f 100644 --- a/src/components/ComplexLookupField/ModalComplexLookup.tsx +++ b/src/components/ComplexLookupField/ModalComplexLookup.tsx @@ -6,7 +6,7 @@ import { SEARCH_RESULTS_FORMATTER } from '@common/helpers/search/formatters'; import { SEARCH_QUERY_BUILDER } from '@common/helpers/search/queryBuilder'; import { IS_EMBEDDED_MODE } from '@common/constants/build.constants'; import { SearchSegment } from '@common/constants/search.constants'; -import { ComplexLookupType } from '@common/constants/complexLookup.constants'; +import { Authority, ComplexLookupType } from '@common/constants/complexLookup.constants'; import { useComplexLookupApi } from '@common/hooks/useComplexLookupApi'; import { useMarcData } from '@common/hooks/useMarcData'; import { COMPLEX_LOOKUPS_CONFIG } from '@src/configs'; @@ -35,7 +35,7 @@ export const ModalComplexLookup: FC = memo( onClose, value, assignEntityName = ComplexLookupType.Authorities, - baseLabelType = 'creator', + baseLabelType = Authority.Creator, }) => { const { api, diff --git a/src/configs/complexLookup/complexLookup.config.ts b/src/configs/complexLookup/complexLookup.config.ts index d985e627..08483a9c 100644 --- a/src/configs/complexLookup/complexLookup.config.ts +++ b/src/configs/complexLookup/complexLookup.config.ts @@ -1,5 +1,5 @@ import { SearchSegment } from '@common/constants/search.constants'; -import { ComplexLookupType, SearchableIndex } from '@common/constants/complexLookup.constants'; +import { AuthorityValidationTarget, ComplexLookupType, SearchableIndex } from '@common/constants/complexLookup.constants'; import { AUTHORITY_ASSIGNMENT_CHECK_API_ENDPOINT } from '@common/constants/api.constants'; import { COMPLEX_LOOKUP_FILTERS_CONFIG } from './complexLookupFilters.config'; import { COMPLEX_LOOKUP_SEARCH_BY_CONFIG } from './complexLookupSearchBy.config'; @@ -20,7 +20,7 @@ export const COMPLEX_LOOKUPS_CONFIG: ComplexLookupsConfig = { validation: AUTHORITY_ASSIGNMENT_CHECK_API_ENDPOINT, }, validationTarget: { - creator: 'CREATOR_OF_WORK', + creator: AuthorityValidationTarget.CreatorOfWork, }, sourceKey: 'authoritySourceFiles', searchQuery: { diff --git a/src/test/__tests__/common/helpers/complexLookup.helper.test.ts b/src/test/__tests__/common/helpers/complexLookup.helper.test.ts index f4b9c57c..70498d95 100644 --- a/src/test/__tests__/common/helpers/complexLookup.helper.test.ts +++ b/src/test/__tests__/common/helpers/complexLookup.helper.test.ts @@ -8,6 +8,7 @@ import { import * as ComplexLookupConstants from '@common/constants/complexLookup.constants'; import { AdvancedFieldType } from '@common/constants/uiControls.constants'; import { getMockedImportedConstant } from '@src/test/__mocks__/common/constants/constants.mock'; +import { AuthorityValidationTarget } from '@common/constants/complexLookup.constants'; const mockImportedConstant = getMockedImportedConstant(ComplexLookupConstants, 'COMPLEX_LOOKUPS_LINKED_FIELDS_MAPPING'); mockImportedConstant({ @@ -150,13 +151,13 @@ describe('complexLookup.helper', () => { expect(result).toEqual({ rawMarc: JSON.stringify(mockMarcContent, null, 2), - target: 'CREATOR_OF_WORK', + target: AuthorityValidationTarget.CreatorOfWork, }); }); it('returns correct request body with custom target', () => { const customTarget = 'CUSTOM_TARGET'; - const result = generateValidationRequestBody(mockMarcData, customTarget); + const result = generateValidationRequestBody(mockMarcData, customTarget as AuthorityValidationTargetType); expect(result).toEqual({ rawMarc: JSON.stringify(mockMarcContent, null, 2), diff --git a/src/types/complexLookup.d.ts b/src/types/complexLookup.d.ts index 3c53718d..e5560ebe 100644 --- a/src/types/complexLookup.d.ts +++ b/src/types/complexLookup.d.ts @@ -1,6 +1,7 @@ type SearchableIndexType = import('@common/constants/complexLookup.constants').SearchableIndex; type SearchableIndexQuerySelectorType = import('@common/constants/complexLookup.constants').SearchableIndexQuerySelector; +type AuthorityValidationTargetType = import('@common/constants/complexLookup.constants').AuthorityValidationTarget; type ComplexLookupLabels = { button: { @@ -42,7 +43,7 @@ type ComplexLookupApiEntryConfig = { marcPreview?: string; validation?: string; }; - validationTarget?: Record; + validationTarget?: Record; sourceKey?: string; searchQuery: { filter?: string;