Skip to content

Commit

Permalink
disable Assign button
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Dec 17, 2024
1 parent ae6acb1 commit 6e2798a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/common/hooks/useComplexLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const useComplexLookup = ({

const isValid = await validateMarcRecord(marcData);

if (!isValid) {
if (isValid) {
assignMarcRecord({ id, title, srsId, linkedFieldValue });
} else {
addFailedEntryId(id);
Expand Down
16 changes: 13 additions & 3 deletions src/common/hooks/useComplexLookupValidation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { useComplexLookup } from '@src/store';
import { useComplexLookupState } from '@src/store';

export const useComplexLookupValidation = () => {
const { addAuthorityAssignmentCheckFailedIdsItem, resetAuthorityAssignmentCheckFailedIds } = useComplexLookup();
const {
authorityAssignmentCheckFailedIds,
addAuthorityAssignmentCheckFailedIdsItem,
resetAuthorityAssignmentCheckFailedIds,
} = useComplexLookupState();

const addFailedEntryId = (id: string) => {
addAuthorityAssignmentCheckFailedIdsItem?.(id);
Expand All @@ -11,5 +15,11 @@ export const useComplexLookupValidation = () => {
resetAuthorityAssignmentCheckFailedIds();
};

return { addFailedEntryId, clearFailedEntryIds };
const checkFailedId = (id?: string) => {
if (!id) return false;

return authorityAssignmentCheckFailedIds?.includes(id);
};

return { addFailedEntryId, clearFailedEntryIds, checkFailedId };
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FC } from 'react';
import { FormattedDate, FormattedMessage, useIntl } from 'react-intl';
import { useSearchContext } from '@common/hooks/useSearchContext';
import { useComplexLookupValidation } from '@common/hooks/useComplexLookupValidation';
import { useMarcPreviewState, useUIState } from '@src/store';
import { SearchControlPane } from '@components/SearchControlPane';
import { MarcContent } from '@components/MarcContent';
Expand All @@ -17,6 +18,7 @@ export const MarcPreviewComplexLookup: FC<MarcPreviewComplexLookupProps> = ({ on
const { formatMessage } = useIntl();
const { isMarcPreviewOpen } = useUIState();
const { complexValue: marcPreviewData, metadata: marcPreviewMetadata } = useMarcPreviewState();
const { checkFailedId } = useComplexLookupValidation();

const renderCloseButton = () => (
<Button
Expand Down Expand Up @@ -47,6 +49,8 @@ export const MarcPreviewComplexLookup: FC<MarcPreviewComplexLookupProps> = ({ on
});
};

const isDisabledButton = checkFailedId(marcPreviewMetadata?.baseId);

return (
<>
{isMarcPreviewOpen && marcPreviewData ? (
Expand All @@ -61,6 +65,7 @@ export const MarcPreviewComplexLookup: FC<MarcPreviewComplexLookupProps> = ({ on
type={ButtonType.Highlighted}
onClick={onClickAssignButton}
ariaLabel={formatMessage({ id: 'ld.aria.marcAuthorityPreview.close' })}
disabled={isDisabledButton}
>
<FormattedMessage id="ld.assign" />
</Button>
Expand Down
4 changes: 4 additions & 0 deletions src/components/ComplexLookupField/formatters/Assign.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FormattedMessage } from 'react-intl';
import { useComplexLookupValidation } from '@common/hooks/useComplexLookupValidation';
import { AuthRefType } from '@common/constants/search.constants';
import { Button, ButtonType } from '@components/Button';

Expand All @@ -9,7 +10,9 @@ export const AssignFormatter = ({
row: SearchResultsTableRow;
onAssign: ({ id, title, linkedFieldValue }: ComplexLookupAssignRecordDTO) => void;
}) => {
const { checkFailedId } = useComplexLookupValidation();
const isAuthorized = row.authorized.label === AuthRefType.Authorized;
const isDisabled = checkFailedId(row.__meta.id);

return isAuthorized ? (
<Button
Expand All @@ -22,6 +25,7 @@ export const AssignFormatter = ({
})
}
data-testid={`assign-button-${row.__meta.id}`}
disabled={isDisabled}
>
<FormattedMessage id="ld.assign" />
</Button>
Expand Down
2 changes: 1 addition & 1 deletion src/store/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export const useInputsState = useInputsStore;
export const useConfigState = useConfigStore;
export const useUIState = useUIStore;
export const useSearchState = useSearchStore;
export const useComplexLookup = useComplexLookupStore;
export const useComplexLookupState = useComplexLookupStore;

0 comments on commit 6e2798a

Please sign in to comment.