Skip to content

Commit

Permalink
UILD-410: Full Display refactoring to reduce nesting of functions (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio authored Nov 22, 2024
1 parent 32aaa70 commit 65d0731
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
61 changes: 6 additions & 55 deletions src/components/FullDisplay/FullDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,66 +1,17 @@
import { useRecoilState } from 'recoil';
import { useRecoilValue } from 'recoil';
import { DOM_ELEMENTS } from '@common/constants/domElementsIdentifiers.constants';
import { Preview } from '@components/Preview';
import state from '@state';
import { Button, ButtonType } from '@components/Button';
import Times16 from '@src/assets/times-16.svg?react';
import { RESOURCE_TEMPLATE_IDS } from '@common/constants/bibframe.constants';
import { FormattedMessage } from 'react-intl';
import { generateEditResourceUrl } from '@common/helpers/navigation.helper';
import { useNavigateToEditPage } from '@common/hooks/useNavigateToEditPage';
import { useCallback } from 'react';
import './FullDisplay.scss';
import { PreviewContent } from './PreviewContent';

export const FullDisplay = () => {
const [previewContent, setPreviewContent] = useRecoilState(state.inputs.previewContent);
const { navigateToEditPage } = useNavigateToEditPage();

const renderPreviewContent = useCallback(
() =>
previewContent.map(({ id, base, userValues, initKey, title, entities }) => {
const resourceType = entities?.map(e => RESOURCE_TEMPLATE_IDS[e])?.[0];
const resourceTypeWithFallback = resourceType ?? '-';

return (
<div key={id}>
<div className="full-display-control-panel">
<Button
className="close"
data-testid="preview-remove"
onClick={() => setPreviewContent(previewContent.filter(entry => entry.id !== id))}
>
<Times16 />
</Button>
<div className="info">
<span className="title">{title}</span>
<span className="type">{resourceTypeWithFallback}</span>
</div>
<Button
data-testid="preview-fetch"
onClick={() => navigateToEditPage(generateEditResourceUrl(id))}
type={ButtonType.Highlighted}
>
<FormattedMessage id={`ld.edit${resourceType}`} defaultMessage="Edit" />
</Button>
</div>
{Object.keys(userValues).length ? (
<div data-testid="preview-contents-container" className="preview-contents-container">
<Preview altSchema={base} altUserValues={userValues} altInitKey={initKey} headless hideActions />
</div>
) : (
<div>
<FormattedMessage id="ld.resourceWithIdIsEmpty" values={{ id }} />
</div>
)}
</div>
);
}),
[navigateToEditPage, previewContent],
);
const previewContent = useRecoilValue(state.inputs.previewContent);

return (
!!previewContent.length && (
<div className={DOM_ELEMENTS.classNames.fullDisplayContainer}>{renderPreviewContent()}</div>
<div className={DOM_ELEMENTS.classNames.fullDisplayContainer}>
<PreviewContent />
</div>
)
);
};
51 changes: 51 additions & 0 deletions src/components/FullDisplay/PreviewContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useRecoilState } from 'recoil';
import { FormattedMessage } from 'react-intl';
import { RESOURCE_TEMPLATE_IDS } from '@common/constants/bibframe.constants';
import { generateEditResourceUrl } from '@common/helpers/navigation.helper';
import { useNavigateToEditPage } from '@common/hooks/useNavigateToEditPage';
import { Button, ButtonType } from '@components/Button';
import { Preview } from '@components/Preview';
import Times16 from '@src/assets/times-16.svg?react';
import state from '@state';
import './FullDisplay.scss';

export const PreviewContent = () => {
const [previewContent, setPreviewContent] = useRecoilState(state.inputs.previewContent);
const { navigateToEditPage } = useNavigateToEditPage();

return previewContent.map(({ id, base, userValues, initKey, title, entities }) => {
const resourceType = entities?.map(e => RESOURCE_TEMPLATE_IDS[e])?.[0];
const resourceTypeWithFallback = resourceType ?? '-';
const handleButtonClick = () => setPreviewContent(previewContent.filter(entry => entry.id !== id));

return (
<div key={id}>
<div className="full-display-control-panel">
<Button className="close" data-testid="preview-remove" onClick={handleButtonClick}>
<Times16 />
</Button>
<div className="info">
<span className="title">{title}</span>
<span className="type">{resourceTypeWithFallback}</span>
</div>
<Button
data-testid="preview-fetch"
onClick={() => navigateToEditPage(generateEditResourceUrl(id))}
type={ButtonType.Highlighted}
>
<FormattedMessage id={`ld.edit${resourceType}`} defaultMessage="Edit" />
</Button>
</div>
{Object.keys(userValues).length ? (
<div data-testid="preview-contents-container" className="preview-contents-container">
<Preview altSchema={base} altUserValues={userValues} altInitKey={initKey} headless hideActions />
</div>
) : (
<div>
<FormattedMessage id="ld.resourceWithIdIsEmpty" values={{ id }} />
</div>
)}
</div>
);
});
};

0 comments on commit 65d0731

Please sign in to comment.