-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UILD-410: Full Display refactoring to reduce nesting of functions (#40)
- Loading branch information
1 parent
32aaa70
commit 65d0731
Showing
2 changed files
with
57 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}); | ||
}; |