-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathConfirmSavePopup.tsx
172 lines (163 loc) · 5.3 KB
/
ConfirmSavePopup.tsx
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-FileCopyrightText: Meta Platforms, Inc. and its affiliates
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0
import MuiDivider from '@mui/material/Divider';
import MuiTypography from '@mui/material/Typography';
import { compact, uniq } from 'lodash';
import { text } from '../../../shared/text';
import {
savePackageInfo,
unlinkAttributionAndCreateNew,
} from '../../state/actions/resource-actions/save-actions';
import { useAppDispatch, useAppSelector } from '../../state/hooks';
import {
getManualAttributions,
getManualAttributionsToResources,
getSelectedAttributionId,
getSelectedResourceId,
getTemporaryDisplayPackageInfo,
} from '../../state/selectors/resource-selectors';
import { maybePluralize } from '../../util/maybe-pluralize';
import { CardList } from '../CardList/CardList';
import { PackageCard } from '../PackageCard/PackageCard';
import { LinkedResourcesTree } from '../ResourceBrowser/LinkedResourcesTree/LinkedResourcesTree';
import { StyledNotificationPopup } from './ConfirmSavePopup.style';
interface Props {
attributionIdsToSave: Array<string>;
open: boolean;
onClose: () => void;
}
export const ConfirmSavePopup: React.FC<Props> = ({
attributionIdsToSave,
open,
onClose,
}) => {
const dispatch = useAppDispatch();
const attributions = useAppSelector(getManualAttributions);
const attributionsToResources = useAppSelector(
getManualAttributionsToResources,
);
const selectedAttributionId = useAppSelector(getSelectedAttributionId);
const selectedResourceId = useAppSelector(getSelectedResourceId);
const temporaryDisplayPackageInfo = useAppSelector(
getTemporaryDisplayPackageInfo,
);
const resourceIds = uniq(
compact(attributionIdsToSave.flatMap((id) => attributionsToResources[id])),
);
const hasMultipleResourcesWhichContainSelected =
resourceIds.length > 1 && resourceIds.includes(selectedResourceId);
const areAllAttributionsPreselected = attributionIdsToSave.every(
(id) => attributions[id]?.preSelected,
);
const handleSave = () => {
attributionIdsToSave.forEach((attributionId) => {
dispatch(
savePackageInfo(
null,
attributionId,
attributionId === selectedAttributionId
? temporaryDisplayPackageInfo
: attributions[attributionId],
attributionId !== selectedAttributionId,
),
);
});
onClose();
};
const handleSaveOnResource = () => {
attributionIdsToSave.forEach((attributionId) => {
dispatch(
unlinkAttributionAndCreateNew(
selectedResourceId,
attributionId === selectedAttributionId
? temporaryDisplayPackageInfo
: attributions[attributionId],
),
);
});
onClose();
};
return (
<StyledNotificationPopup
content={renderContent()}
header={
areAllAttributionsPreselected
? text.saveAttributionsPopup.titleConfirm
: text.saveAttributionsPopup.titleSave
}
centerLeftButtonConfig={{
onClick: handleSave,
color: 'error',
buttonText:
resourceIds.length > 1
? areAllAttributionsPreselected
? text.saveAttributionsPopup.confirmGlobally
: text.saveAttributionsPopup.saveGlobally
: areAllAttributionsPreselected
? text.saveAttributionsPopup.confirm
: text.saveAttributionsPopup.save,
}}
leftButtonConfig={
hasMultipleResourcesWhichContainSelected
? {
onClick: handleSaveOnResource,
buttonText: areAllAttributionsPreselected
? text.saveAttributionsPopup.confirmLocally
: text.saveAttributionsPopup.saveLocally,
}
: undefined
}
rightButtonConfig={{
onClick: onClose,
buttonText: text.buttons.cancel,
color: 'secondary',
}}
isOpen={open}
aria-label={'confirm save popup'}
width={580}
/>
);
function renderContent() {
return (
<>
<MuiTypography>
{(areAllAttributionsPreselected
? text.saveAttributionsPopup.confirmAttributions
: text.saveAttributionsPopup.saveAttributions)({
attributions: maybePluralize(
attributionIdsToSave.length,
text.packageLists.attribution,
),
resources: maybePluralize(
resourceIds.length,
text.saveAttributionsPopup.resource,
{ showOne: true },
),
})}
</MuiTypography>
<CardList
data={attributionIdsToSave}
renderItemContent={(attributionId, { index }) => {
if (!attributionId || !(attributionId in attributions)) {
return null;
}
return (
<>
<PackageCard packageInfo={attributions[attributionId]} />
{index + 1 !== attributionIdsToSave.length && <MuiDivider />}
</>
);
}}
/>
<LinkedResourcesTree
readOnly
disableHighlightSelected={!hasMultipleResourcesWhichContainSelected}
resourceIds={resourceIds}
sx={{ minHeight: '100px' }}
/>
</>
);
}
};