-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComparison.tsx
More file actions
144 lines (135 loc) · 5.35 KB
/
Comparison.tsx
File metadata and controls
144 lines (135 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { FormattedMessage, useIntl } from 'react-intl';
import { Pagination } from '@components/Pagination';
import { useState } from 'react';
import { useInputsState, useSearchState, useUIState } from '@src/store';
import { Button, ButtonType } from '@components/Button';
import Times16 from '@src/assets/times-16.svg?react';
import TimesCircle12 from '@src/assets/times-circle-12.svg?react';
import Transfer16 from '@src/assets/transfer-16.svg?react';
import GeneralSearch from '@src/assets/general-search.svg?react';
import { Preview } from '@components/Preview';
import { PreviewActionsDropdown } from '@components/PreviewActionsDropdown';
import { ResourceType } from '@common/constants/record.constants';
import { generateEditResourceUrl } from '@common/helpers/navigation.helper';
import { useNavigateToEditPage } from '@common/hooks/useNavigateToEditPage';
import './Comparison.scss';
export const Comparison = () => {
const { formatMessage } = useIntl();
const { previewContent, setPreviewContent, resetPreviewContent } = useInputsState();
const { setSelectedInstances, resetSelectedInstances } = useSearchState();
const { resetFullDisplayComponentType } = useUIState();
const { navigateToEditPage } = useNavigateToEditPage();
const [currentPage, setCurrentPage] = useState(0);
const handleCloseComparison = () => {
resetPreviewContent();
resetFullDisplayComponentType();
resetSelectedInstances();
};
const handleRemoveComparisonItem = (id: string) => {
if (currentPage === totalPages - 1) {
setCurrentPage(prevValue => {
const previousPage = prevValue - 1;
return previousPage >= 0 ? previousPage : 0;
});
}
setPreviewContent(prev => {
const updatedPreviewContent = prev.filter(({ id: prevId }) => prevId !== id);
if (!updatedPreviewContent.length) {
resetFullDisplayComponentType();
}
return updatedPreviewContent;
});
setSelectedInstances(prev => prev.filter(prevId => prevId !== id));
};
const handleNavigateToOwnEditPage = (id: string) => navigateToEditPage(generateEditResourceUrl(id));
const totalPages = (previewContent.length > 1 ? previewContent.length : 2) - 1;
return (
<section className="comparison">
<header>
<div className="heading">
<Button
data-testid="close-comparison-section"
type={ButtonType.Icon}
onClick={handleCloseComparison}
className="nav-close"
ariaLabel={formatMessage({ id: 'ld.aria.comparison.close' })}
>
<Times16 />
</Button>
<h2>
<Transfer16 />
<FormattedMessage id="ld.compareResources" />
</h2>
<span className="empty-block" />
</div>
<div className="subheading">
<span>
<FormattedMessage id="ld.nResourcesSelected" values={{ n: previewContent.length }} />
</span>
<Button
data-testid="clear-comparison-selections"
type={ButtonType.Icon}
onClick={handleCloseComparison}
disabled={!previewContent.length}
className="nav-close"
>
<TimesCircle12 />
<FormattedMessage id="ld.clearSelections" />
</Button>
</div>
</header>
<div className="comparison-contents">
{previewContent
.slice(currentPage, currentPage + 2)
.map(({ initKey, base, userValues, id, title, referenceIds }) => (
<section key={id} className="entry">
<div className="entry-header">
<div className="entry-header-controls">
<Button
data-testid={`remove-comparison-entry-${id}`}
type={ButtonType.Icon}
onClick={() => handleRemoveComparisonItem(id)}
className="nav-close"
ariaLabel={formatMessage({ id: 'ld.aria.comparison.removeEntry' })}
>
<Times16 />
</Button>
<PreviewActionsDropdown
ownId={id}
referenceId={referenceIds?.[0]?.id as unknown as string}
entityType={ResourceType.instance}
handleNavigateToEditPage={() => handleNavigateToOwnEditPage(id)}
/>
</div>
<h3>{title}</h3>
</div>
<Preview
altInitKey={initKey}
altSchema={base}
altUserValues={userValues}
forceRenderAllTopLevelEntities
/>
</section>
))}
{previewContent.length <= 1 && (
<div className="insufficient-resource-amt" data-testid="insufficient-resource-amt">
<GeneralSearch />
<FormattedMessage
id={!previewContent.length ? 'ld.chooseTwoResourcesCompare' : 'ld.chooseOneResourceCompare'}
/>
</div>
)}
</div>
<nav aria-label={formatMessage({ id: 'ld.selectedResources' })}>
<Pagination
currentPage={currentPage}
onNextPageClick={() => setCurrentPage(prev => prev + 1)}
onPrevPageClick={() => setCurrentPage(prev => prev - 1)}
totalPages={totalPages}
totalResultsCount={totalPages}
showCount
/>
</nav>
</section>
);
};