-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathCollectionCard.tsx
155 lines (140 loc) · 4.92 KB
/
CollectionCard.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
import { useCallback, useContext } from 'react';
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
import {
ActionRow,
Dropdown,
Icon,
IconButton,
useToggle,
} from '@openedx/paragon';
import { MoreVert } from '@openedx/paragon/icons';
import { Link } from 'react-router-dom';
import { type CollectionHit } from '../../search-manager';
import { useComponentPickerContext } from '../common/context/ComponentPickerContext';
import { useLibraryContext } from '../common/context/LibraryContext';
import { useSidebarContext } from '../common/context/SidebarContext';
import { useLibraryRoutes } from '../routes';
import BaseComponentCard from './BaseComponentCard';
import { ToastContext } from '../../generic/toast-context';
import { useDeleteCollection, useRestoreCollection } from '../data/apiHooks';
import DeleteModal from '../../generic/delete-modal/DeleteModal';
import messages from './messages';
type CollectionMenuProps = {
collectionHit: CollectionHit,
};
const CollectionMenu = ({ collectionHit } : CollectionMenuProps) => {
const intl = useIntl();
const { showToast } = useContext(ToastContext);
const [isDeleteModalOpen, openDeleteModal, closeDeleteModal] = useToggle(false);
const { closeLibrarySidebar, sidebarComponentInfo } = useSidebarContext();
const restoreCollectionMutation = useRestoreCollection(collectionHit.contextKey, collectionHit.blockId);
const restoreCollection = useCallback(() => {
restoreCollectionMutation.mutateAsync()
.then(() => {
showToast(intl.formatMessage(messages.undoDeleteCollectionToastMessage));
}).catch(() => {
showToast(intl.formatMessage(messages.undoDeleteCollectionToastFailed));
});
}, []);
const deleteCollectionMutation = useDeleteCollection(collectionHit.contextKey, collectionHit.blockId);
const deleteCollection = useCallback(async () => {
if (sidebarComponentInfo?.id === collectionHit.blockId) {
// Close sidebar if current collection is open to avoid displaying
// deleted collection in sidebar
closeLibrarySidebar();
}
try {
await deleteCollectionMutation.mutateAsync();
showToast(
intl.formatMessage(messages.deleteCollectionSuccess),
{
label: intl.formatMessage(messages.undoDeleteCollectionToastAction),
onClick: restoreCollection,
},
);
} catch (e) {
showToast(intl.formatMessage(messages.deleteCollectionFailed));
} finally {
closeDeleteModal();
}
}, [sidebarComponentInfo?.id]);
return (
<>
<Dropdown id="collection-card-dropdown">
<Dropdown.Toggle
id="collection-card-menu-toggle"
as={IconButton}
src={MoreVert}
iconAs={Icon}
variant="primary"
alt={intl.formatMessage(messages.collectionCardMenuAlt)}
data-testid="collection-card-menu-toggle"
/>
<Dropdown.Menu>
<Dropdown.Item
as={Link}
to={`/library/${collectionHit.contextKey}/collection/${collectionHit.blockId}`}
>
<FormattedMessage {...messages.menuOpen} />
</Dropdown.Item>
<Dropdown.Item onClick={openDeleteModal}>
<FormattedMessage {...messages.deleteCollection} />
</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
<DeleteModal
isOpen={isDeleteModalOpen}
close={closeDeleteModal}
variant="warning"
category={collectionHit.type}
description={intl.formatMessage(messages.deleteCollectionConfirm, {
collectionTitle: collectionHit.displayName,
})}
onDeleteSubmit={deleteCollection}
/>
</>
);
};
type CollectionCardProps = {
collectionHit: CollectionHit,
};
const CollectionCard = ({ collectionHit } : CollectionCardProps) => {
const { componentPickerMode } = useComponentPickerContext();
const { showOnlyPublished } = useLibraryContext();
const { openCollectionInfoSidebar } = useSidebarContext();
const {
type: componentType,
blockId: collectionId,
formatted,
tags,
numChildren,
published,
} = collectionHit;
const numChildrenCount = showOnlyPublished ? (
published?.numChildren || 0
) : numChildren;
const { displayName = '', description = '' } = formatted;
const { navigateTo } = useLibraryRoutes();
const openCollection = useCallback(() => {
openCollectionInfoSidebar(collectionId);
if (!componentPickerMode) {
navigateTo({ collectionId });
}
}, [collectionId, navigateTo, openCollectionInfoSidebar]);
return (
<BaseComponentCard
componentType={componentType}
displayName={displayName}
description={description}
tags={tags}
numChildren={numChildrenCount}
actions={!componentPickerMode && (
<ActionRow>
<CollectionMenu collectionHit={collectionHit} />
</ActionRow>
)}
onSelect={openCollection}
/>
);
};
export default CollectionCard;