-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
CollectionPermissionsModal.jsx
170 lines (150 loc) · 4.58 KB
/
CollectionPermissionsModal.jsx
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
import React, { useEffect, useCallback } from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { t } from "ttag";
import _ from "underscore";
import * as Urls from "metabase/lib/urls";
import Collections from "metabase/entities/collections";
import SnippetCollections from "metabase/entities/snippet-collections";
import { isPersonalCollectionChild } from "metabase/collections/utils";
import ModalContent from "metabase/components/ModalContent";
import Button from "metabase/components/Button";
import Link from "metabase/components/Link";
import { PermissionsTable } from "../PermissionsTable";
import Groups from "metabase/entities/groups";
import {
getDiff,
getIsDirty,
getCollectionsPermissionEditor,
} from "../../selectors/collection-permissions";
import {
initializeCollectionPermissions,
updateCollectionPermission,
saveCollectionPermissions,
} from "../../permissions";
import { permissionEditorPropTypes } from "../PermissionsEditor/PermissionsEditor";
const getDefaultTitle = namespace =>
namespace === "snippets"
? t`Permissions for this folder`
: t`Permissions for this collection`;
const propTypes = {
permissionEditor: PropTypes.shape(permissionEditorPropTypes),
namespace: PropTypes.string,
isDirty: PropTypes.bool,
onClose: PropTypes.func.isRequired,
collection: PropTypes.object,
collectionsList: PropTypes.arrayOf(PropTypes.object),
initialize: PropTypes.func.isRequired,
updateCollectionPermission: PropTypes.func.isRequired,
saveCollectionPermissions: PropTypes.func.isRequired,
};
const CollectionPermissionsModal = ({
permissionEditor,
isDirty,
onClose,
namespace,
collection,
collectionsList,
initialize,
updateCollectionPermission,
saveCollectionPermissions,
}) => {
useEffect(() => {
initialize(namespace);
}, [initialize, namespace]);
useEffect(() => {
const isPersonalCollectionLoaded =
collection &&
Array.isArray(collectionsList) &&
(collection.personal_owner_id ||
isPersonalCollectionChild(collection, collectionsList));
if (isPersonalCollectionLoaded || collection.archived) {
onClose();
}
}, [collectionsList, collection, onClose]);
const handleSave = async () => {
await saveCollectionPermissions(namespace);
onClose();
};
const modalTitle = collection?.name
? t`Permissions for ${collection.name}`
: getDefaultTitle(namespace);
const handlePermissionChange = useCallback(
(item, _permission, value, toggleState) => {
updateCollectionPermission({
groupId: item.id,
collection,
value,
shouldPropagate: toggleState,
});
},
[collection, updateCollectionPermission],
);
return (
<ModalContent
title={modalTitle}
onClose={onClose}
footer={[
...(namespace === "snippets"
? []
: [
<Link
key="all-permissions"
className="link"
to="/admin/permissions/collections"
>
{t`See all collection permissions`}
</Link>,
]),
<Button key="cancel" onClick={onClose}>{t`Cancel`}</Button>,
<Button key="save" primary disabled={!isDirty} onClick={handleSave}>
{t`Save`}
</Button>,
]}
>
<div className="relative" style={{ height: "50vh" }}>
{permissionEditor && (
<PermissionsTable
{...permissionEditor}
onChange={handlePermissionChange}
/>
)}
</div>
</ModalContent>
);
};
CollectionPermissionsModal.propTypes = propTypes;
const getCollectionEntity = props =>
props.namespace === "snippets" ? SnippetCollections : Collections;
const mapStateToProps = (state, props) => {
const collectionId = Urls.extractCollectionId(props.params.slug);
return {
permissionEditor: getCollectionsPermissionEditor(state, {
namespace: props.namespace,
params: { collectionId },
}),
collection: getCollectionEntity(props).selectors.getObject(state, {
entityId: collectionId,
}),
collectionsList: Collections.selectors.getList(state, {
entityQuery: { tree: true },
}),
diff: getDiff(state, props),
isDirty: getIsDirty(state, props),
};
};
const mapDispatchToProps = {
initialize: initializeCollectionPermissions,
updateCollectionPermission,
saveCollectionPermissions,
};
export default _.compose(
Collections.loadList({
query: () => ({ tree: true }),
}),
Groups.loadList(),
connect(
mapStateToProps,
mapDispatchToProps,
),
)(CollectionPermissionsModal);