Skip to content

Commit

Permalink
refactoring after review
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Dec 20, 2024
1 parent a489015 commit 49f257c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/common/constants/complexLookup.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
10 changes: 7 additions & 3 deletions src/common/helpers/complexLookup.helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AuthorityValidationTarget,
COMPLEX_LOOKUPS_LINKED_FIELDS_MAPPING,
EMPTY_LINKED_DROPDOWN_OPTION_SUFFIX,
} from '@common/constants/complexLookup.constants';
Expand Down Expand Up @@ -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,
};
};
8 changes: 4 additions & 4 deletions src/common/hooks/useComplexLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<UserValueContents>) => void;
}) => {
const { selectedEntriesService } = useServicesContext() as Required<ServicesParams>;
Expand Down Expand Up @@ -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]),
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/ComplexLookupField/ComplexLookupField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const ComplexLookupField: FC<Props> = ({ value = undefined, id, entry, on
entry,
value,
lookupConfig,
baseLabelType: layout?.baseLabelType,
authority: layout?.baseLabelType,
onChange,
});

Expand Down
4 changes: 2 additions & 2 deletions src/components/ComplexLookupField/ModalComplexLookup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -35,7 +35,7 @@ export const ModalComplexLookup: FC<ModalComplexLookupProps> = memo(
onClose,
value,
assignEntityName = ComplexLookupType.Authorities,
baseLabelType = 'creator',
baseLabelType = Authority.Creator,
}) => {
const {
api,
Expand Down
4 changes: 2 additions & 2 deletions src/configs/complexLookup/complexLookup.config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion src/types/complexLookup.d.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -42,7 +43,7 @@ type ComplexLookupApiEntryConfig = {
marcPreview?: string;
validation?: string;
};
validationTarget?: Record<string, string>;
validationTarget?: Record<string, AuthorityValidationTargetType>;
sourceKey?: string;
searchQuery: {
filter?: string;
Expand Down

0 comments on commit 49f257c

Please sign in to comment.