Skip to content

Commit

Permalink
reset validated ids
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Dec 18, 2024
1 parent 6e2798a commit a79a586
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/common/hooks/useComplexLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const useComplexLookup = ({
const linkedField = getLinkedField({ schema, linkedEntry });
const { makeRequest } = useApi();
const { addStatusMessagesItem } = useStatusState();
const { addFailedEntryId } = useComplexLookupValidation();
const { addFailedEntryId, clearFailedEntryIds } = useComplexLookupValidation();

const handleDelete = (id?: string) => {
onChange(uuid, []);
Expand All @@ -68,6 +68,7 @@ export const useComplexLookup = ({
};

const closeModal = useCallback(() => {
clearFailedEntryIds();
setIsModalOpen(false);
}, []);

Expand Down Expand Up @@ -144,16 +145,16 @@ export const useComplexLookup = ({

if (isValid) {
assignMarcRecord({ id, title, srsId, linkedFieldValue });
clearFailedEntryIds();
reset();
closeModal();
} else {
addFailedEntryId(id);

addStatusMessagesItem?.(
UserNotificationFactory.createMessage(StatusType.error, 'ld.errorValidatingAuthorityRecord'),
);
}

reset();
closeModal();
};

const handleOnChangeBase = ({ target: { value } }: ChangeEvent<HTMLInputElement>) => {
Expand Down
10 changes: 9 additions & 1 deletion src/common/hooks/useComplexLookupSearchResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type Row } from '@components/Table';
import { useSearchContext } from '@common/hooks/useSearchContext';
import { ComplexLookupSearchResultsProps } from '@components/ComplexLookupField/ComplexLookupSearchResults';
import { useSearchState } from '@src/store';
import { useComplexLookupValidation } from './useComplexLookupValidation';

export const useComplexLookupSearchResults = ({
onTitleClick,
Expand All @@ -13,6 +14,7 @@ export const useComplexLookupSearchResults = ({
const { onAssignRecord } = useSearchContext();
const { data, sourceData } = useSearchState();
const { formatMessage } = useIntl();
const { checkFailedId } = useComplexLookupValidation();

const applyActionItems = useCallback(
(rows: Row[]): Row[] =>
Expand All @@ -23,7 +25,13 @@ export const useComplexLookupSearchResults = ({
formattedRow[key] = {
...row[key],
children: column.formatter
? column.formatter({ row, formatMessage, onAssign: onAssignRecord, onTitleClick })
? column.formatter({
row,
formatMessage,
onAssign: onAssignRecord,
onTitleClick,
validateId: checkFailedId,
})
: row[key].label,
};
});
Expand Down
6 changes: 3 additions & 3 deletions src/components/ComplexLookupField/formatters/Assign.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { FormattedMessage } from 'react-intl';
import { useComplexLookupValidation } from '@common/hooks/useComplexLookupValidation';
import { AuthRefType } from '@common/constants/search.constants';
import { Button, ButtonType } from '@components/Button';

export const AssignFormatter = ({
row,
onAssign,
validateId,
}: {
row: SearchResultsTableRow;
onAssign: ({ id, title, linkedFieldValue }: ComplexLookupAssignRecordDTO) => void;
validateId: (id: string) => boolean;
}) => {
const { checkFailedId } = useComplexLookupValidation();
const isAuthorized = row.authorized.label === AuthRefType.Authorized;
const isDisabled = checkFailedId(row.__meta.id);
const isDisabled = validateId(row.__meta.id);

return isAuthorized ? (
<Button
Expand Down

0 comments on commit a79a586

Please sign in to comment.