-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathConfirmDeletePopup.tsx
145 lines (136 loc) · 4.34 KB
/
ConfirmDeletePopup.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
// 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 {
deleteAttributionAndSave,
unlinkAttributionAndSave,
} from '../../state/actions/resource-actions/save-actions';
import { useAppDispatch, useAppSelector } from '../../state/hooks';
import {
getManualAttributions,
getManualAttributionsToResources,
getSelectedAttributionId,
getSelectedResourceId,
} 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 './ConfirmDeletePopup.style';
interface Props {
attributionIdsToDelete: Array<string>;
open: boolean;
onClose: () => void;
}
export const ConfirmDeletePopup: React.FC<Props> = ({
attributionIdsToDelete,
open,
onClose,
}) => {
const dispatch = useAppDispatch();
const attributions = useAppSelector(getManualAttributions);
const attributionsToResources = useAppSelector(
getManualAttributionsToResources,
);
const selectedAttributionId = useAppSelector(getSelectedAttributionId);
const selectedResourceId = useAppSelector(getSelectedResourceId);
const resourceIds = uniq(
compact(
attributionIdsToDelete.flatMap((id) => attributionsToResources[id]),
),
);
const isOptionToDeleteOnSelectedResourceOnlyAvailable =
resourceIds.length > 1 &&
attributionIdsToDelete.every((id) =>
attributionsToResources[id]?.includes(selectedResourceId),
);
const handleDelete = () => {
attributionIdsToDelete.forEach((attributionId) => {
dispatch(deleteAttributionAndSave(attributionId, selectedAttributionId));
});
onClose();
};
const handleDeleteOnResource = () => {
dispatch(
unlinkAttributionAndSave(selectedResourceId, attributionIdsToDelete),
);
onClose();
};
return (
<StyledNotificationPopup
content={renderContent()}
header={text.deleteAttributionsPopup.title}
centerLeftButtonConfig={{
onClick: handleDelete,
buttonText:
resourceIds.length > 1
? text.deleteAttributionsPopup.deleteGlobally
: text.deleteAttributionsPopup.delete,
color: 'error',
}}
leftButtonConfig={
isOptionToDeleteOnSelectedResourceOnlyAvailable
? {
onClick: handleDeleteOnResource,
buttonText: text.deleteAttributionsPopup.deleteLocally,
color: 'primary',
}
: undefined
}
rightButtonConfig={{
onClick: onClose,
buttonText: text.buttons.cancel,
color: 'secondary',
}}
isOpen={open}
aria-label={'confirm delete popup'}
width={580}
/>
);
function renderContent() {
return (
<>
<MuiTypography>
{text.deleteAttributionsPopup.deleteAttributions({
attributions: maybePluralize(
attributionIdsToDelete.length,
text.packageLists.attribution,
),
resources: maybePluralize(
resourceIds.length,
text.deleteAttributionsPopup.resource,
{ showOne: true },
),
})}
</MuiTypography>
<CardList
data={attributionIdsToDelete}
renderItemContent={(attributionId, { index }) => {
if (!attributionId || !(attributionId in attributions)) {
return null;
}
return (
<>
<PackageCard packageInfo={attributions[attributionId]} />
{index + 1 !== attributionIdsToDelete.length && <MuiDivider />}
</>
);
}}
/>
<LinkedResourcesTree
readOnly
disableHighlightSelected={
!isOptionToDeleteOnSelectedResourceOnlyAvailable
}
resourceIds={resourceIds}
sx={{ minHeight: '100px' }}
/>
</>
);
}
};