-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathTableActions.jsx
More file actions
139 lines (132 loc) · 4.37 KB
/
TableActions.jsx
File metadata and controls
139 lines (132 loc) · 4.37 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
import { getConfig } from '@edx/frontend-platform';
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
import {
Button,
DataTableContext,
Dropdown,
useToggle,
} from '@openedx/paragon';
import { Add, Tune } from '@openedx/paragon/icons';
import { FilesPageContext } from '@src/files-and-videos/files-page/FilesPageProvider';
import { isEmpty } from 'lodash';
import { PropTypes } from 'prop-types';
import React, { useContext, useEffect } from 'react';
import messages from '../messages';
import SortAndFilterModal from './sort-and-filter-modal';
const TableActions = ({
selectedFlatRows,
fileInputControl,
handleSort,
handleBulkDownload,
handleOpenDeleteConfirmation,
encodingsDownloadUrl,
fileType,
setInitialState,
}) => {
const intl = useIntl();
const [isSortOpen, openSort, closeSort] = useToggle(false);
const { state, clearSelection } = useContext(DataTableContext);
const { filePickerMode } = useContext(FilesPageContext);
// If window.opener is not available, show the user some error message.
const showFilePicker = filePickerMode; // && Boolean(window.opener);
// This useEffect saves DataTable state so it can persist after table re-renders due to data reload.
useEffect(() => {
setInitialState(state);
}, [state]);
const handleOpenFileSelector = () => {
fileInputControl.click();
clearSelection();
};
return (
<>
<Button variant="outline-primary" onClick={openSort} iconBefore={Tune}>
<FormattedMessage {...messages.sortButtonLabel} />
</Button>
<Dropdown className="mx-2">
<Dropdown.Toggle
id="actions-menu-toggle"
alt="actions-menu-toggle"
variant="outline-primary"
>
<FormattedMessage {...messages.actionsButtonLabel} />
</Dropdown.Toggle>
<Dropdown.Menu>
{encodingsDownloadUrl ?
(
<Dropdown.Item
download
href={`${getConfig().STUDIO_BASE_URL}${encodingsDownloadUrl}`}
>
<FormattedMessage {...messages.downloadEncodingsTitle} />
</Dropdown.Item>
) :
null}
<Dropdown.Item
onClick={() => handleBulkDownload(selectedFlatRows)}
disabled={isEmpty(selectedFlatRows)}
>
<FormattedMessage {...messages.downloadTitle} />
</Dropdown.Item>
<Dropdown.Divider />
<Dropdown.Item
data-testid="open-delete-confirmation-button"
onClick={() => handleOpenDeleteConfirmation(selectedFlatRows)}
disabled={isEmpty(selectedFlatRows)}
>
<FormattedMessage {...messages.deleteTitle} />
</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
<Button iconBefore={Add} onClick={handleOpenFileSelector}>
{intl.formatMessage(messages.addFilesButtonLabel, { fileType })}
</Button>
{showFilePicker && (
<Button
className="ml-2"
onClick={async () => {
window.opener.postMessage({
type: 'org.openedx.assets.selected.v1',
data: selectedFlatRows.map(({ original }) => original),
}, '*');
window.close();
}}
disabled={selectedFlatRows.length === 0}
>
Select File(s)
</Button>
)}
<SortAndFilterModal {...{ isSortOpen, closeSort, handleSort }} />
</>
);
};
TableActions.defaultProps = {
selectedFlatRows: null,
};
TableActions.propTypes = {
selectedFlatRows: PropTypes.arrayOf(
PropTypes.shape({
original: PropTypes.shape({
displayName: PropTypes.string.isRequired,
wrapperType: PropTypes.string.isRequired,
locked: PropTypes.bool,
externalUrl: PropTypes.string,
thumbnail: PropTypes.string,
id: PropTypes.string.isRequired,
portableUrl: PropTypes.string,
}).isRequired,
}),
),
fileInputControl: PropTypes.shape({
click: PropTypes.func.isRequired,
}).isRequired,
handleOpenDeleteConfirmation: PropTypes.func.isRequired,
handleBulkDownload: PropTypes.func.isRequired,
encodingsDownloadUrl: PropTypes.string,
handleSort: PropTypes.func.isRequired,
fileType: PropTypes.string.isRequired,
setInitialState: PropTypes.func.isRequired,
};
TableActions.defaultProps = {
encodingsDownloadUrl: null,
};
export default TableActions;