cozy-sharing 18.0.0 → 20.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/dist/SharingProvider.js +16 -9
  3. package/dist/components/Recipient/LinkRecipient.js +20 -4
  4. package/dist/components/Recipient/LinkRecipientPermissions.js +18 -129
  5. package/dist/components/Recipient/actions/editPermissionLink.js +71 -0
  6. package/dist/components/Recipient/actions/readOnlyPermissionLink.js +63 -0
  7. package/dist/components/ShareByLink.js +45 -185
  8. package/dist/components/ShareDialogCozyToCozy.js +2 -6
  9. package/dist/components/ShareDialogOnlyByLink.js +2 -8
  10. package/dist/components/ShareDialogTwoStepsConfirmationContainer.js +1 -3
  11. package/dist/components/ShareModal/EditableSharingModal.js +1 -7
  12. package/dist/components/ShareModal.js +0 -12
  13. package/dist/components/ShareRestrictionModal/BoxDate.js +77 -0
  14. package/dist/components/ShareRestrictionModal/BoxEditingRights.js +83 -0
  15. package/dist/components/ShareRestrictionModal/BoxPassword.js +155 -0
  16. package/dist/components/ShareRestrictionModal/ShareRestrictionContentModal.js +118 -0
  17. package/dist/components/ShareRestrictionModal/ShareRestrictionModal.js +256 -0
  18. package/dist/components/ShareRestrictionModal/helpers.js +310 -0
  19. package/dist/components/ShareRestrictionModal/helpers.spec.js +232 -0
  20. package/dist/components/WhoHasAccess.js +1 -5
  21. package/dist/helpers/permissions.js +15 -0
  22. package/locales/en.json +30 -0
  23. package/locales/fr.json +31 -0
  24. package/package.json +11 -7
  25. package/src/SharingProvider.jsx +10 -5
  26. package/src/components/Recipient/LinkRecipient.jsx +28 -4
  27. package/src/components/Recipient/LinkRecipientPermissions.jsx +28 -137
  28. package/src/components/Recipient/actions/editPermissionLink.js +63 -0
  29. package/src/components/Recipient/actions/readOnlyPermissionLink.js +59 -0
  30. package/src/components/ShareByLink.jsx +20 -96
  31. package/src/components/ShareDialogCozyToCozy.jsx +1 -5
  32. package/src/components/ShareDialogOnlyByLink.jsx +0 -6
  33. package/src/components/ShareDialogTwoStepsConfirmationContainer.jsx +0 -2
  34. package/src/components/ShareModal/EditableSharingModal.jsx +1 -7
  35. package/src/components/ShareModal.jsx +0 -12
  36. package/src/components/ShareRestrictionModal/BoxDate.jsx +76 -0
  37. package/src/components/ShareRestrictionModal/BoxEditingRights.jsx +91 -0
  38. package/src/components/ShareRestrictionModal/BoxPassword.jsx +126 -0
  39. package/src/components/ShareRestrictionModal/ShareRestrictionContentModal.jsx +124 -0
  40. package/src/components/ShareRestrictionModal/ShareRestrictionModal.jsx +186 -0
  41. package/src/components/ShareRestrictionModal/helpers.js +227 -0
  42. package/src/components/ShareRestrictionModal/helpers.spec.js +159 -0
  43. package/src/components/WhoHasAccess.jsx +1 -5
  44. package/src/helpers/permissions.js +12 -0
  45. package/dist/components/EditLinkPermissionDialog.js +0 -72
  46. package/dist/components/EditLinkPermissionDialog.spec.js +0 -48
  47. package/src/components/EditLinkPermissionDialog.jsx +0 -89
  48. package/src/components/EditLinkPermissionDialog.spec.jsx +0 -53
@@ -0,0 +1,227 @@
1
+ import { getSharingLink } from 'cozy-client/dist/models/sharing'
2
+ import minilog from 'cozy-minilog'
3
+
4
+ const log = minilog('ShareRestrictionModal/helpers')
5
+
6
+ export const WRITE_PERMS = ['GET', 'POST', 'PUT', 'PATCH']
7
+ export const READ_ONLY_PERMS = ['GET']
8
+
9
+ /**
10
+ * Copy a string to the clipboard
11
+ *
12
+ * @param {string} value - The string saved to the clipboard
13
+ * @param {object} [options]
14
+ * @param {number} options.t - Translation function
15
+ * @param {Function} options.showAlert - Function to display an alert
16
+ */
17
+ export const copyToClipboard = async (value, { t, showAlert } = {}) => {
18
+ if (!value) return false
19
+ try {
20
+ await navigator.clipboard.writeText(value)
21
+ showAlert({
22
+ message: t('copyReminderContent.success'),
23
+ severity: 'success',
24
+ variant: 'filled'
25
+ })
26
+ } catch (error) {
27
+ showAlert({
28
+ message: t('copyReminderContent.error'),
29
+ severity: 'error',
30
+ variant: 'filled'
31
+ })
32
+ log.error(
33
+ "Error in 'copyToClipboard' function when trying to copy to clipboard",
34
+ error
35
+ )
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Make a TTL string from a date
41
+ * @param {Date|string} selectedDate
42
+ * @returns {string} - TTL string in seconds (e.g. '1234s')
43
+ */
44
+ export const makeTTL = selectedDate => {
45
+ if (!selectedDate) return
46
+ try {
47
+ let selectedDateConverted = selectedDate
48
+ if (typeof selectedDate === 'string') {
49
+ selectedDateConverted = new Date(selectedDate)
50
+ if (selectedDateConverted?.toString() === 'Invalid Date') {
51
+ throw new Error(`Invalid date: ${selectedDate}`)
52
+ }
53
+ }
54
+ if (selectedDateConverted instanceof Date) {
55
+ const now = new Date()
56
+ const ttl =
57
+ selectedDateConverted > now
58
+ ? `${Math.round((selectedDateConverted - now) / 1000)}s`
59
+ : undefined
60
+ return ttl
61
+ }
62
+ throw new Error(`Invalid date: ${selectedDate}`)
63
+ } catch (error) {
64
+ log.error(error)
65
+ return
66
+ }
67
+ }
68
+
69
+ /**
70
+ * forwardFile - Triggers the download of one or multiple files by the browser
71
+ * @param {object} options
72
+ * @param {import('cozy-client/types/CozyClient').default} options.client
73
+ * @param {import('cozy-client/types/types').IOCozyFile} options.file File to download
74
+ * @param {Function} options.t i18n function
75
+ * @param {string} options.ttl Time to live of the sharing link
76
+ * @param {string} options.password Password of the sharing link
77
+ * @param {Function} options.showAlert - Function to display an alert
78
+ */
79
+ export const forwardFile = async ({
80
+ client,
81
+ file,
82
+ t,
83
+ ttl,
84
+ password,
85
+ showAlert
86
+ }) => {
87
+ try {
88
+ // We currently support only one file at a time
89
+ const url = await getSharingLink(client, [file._id], { ttl, password })
90
+ const isZipFile = file.class === 'zip'
91
+ const shareData = {
92
+ title: t('shareFile.title', {
93
+ name: file.name,
94
+ smart_count: isZipFile ? 2 : 1
95
+ }),
96
+ text: t('shareFile.text', {
97
+ name: file.name,
98
+ smart_count: isZipFile ? 2 : 1
99
+ }),
100
+ url
101
+ }
102
+ navigator.share(shareData)
103
+ } catch (error) {
104
+ showAlert({
105
+ message: t('shareFile.error'),
106
+ severity: 'error',
107
+ variant: 'filled'
108
+ })
109
+ }
110
+ }
111
+
112
+ /**
113
+ * createPermissions - Create the permissions of a file
114
+ * @param {object} options
115
+ * @param {import('cozy-client/types/types').IOCozyFile} options.file File to update permissions
116
+ * @param {Function} options.t i18n function
117
+ * @param {Date|string} options.ttl - Time to live of the sharing link
118
+ * @param {string} options.password - Password
119
+ * @param {'readOnly'|'write'} options.editingRights - Editing rights
120
+ * @param {string} options.documentType - Type of the document
121
+ * @param {Function} options.shareByLink - Function to create permissions
122
+ * @param {Function} options.showAlert - Function to display an alert
123
+ */
124
+ export const createPermissions = async ({
125
+ file,
126
+ t,
127
+ ttl,
128
+ password,
129
+ editingRights,
130
+ documentType,
131
+ shareByLink,
132
+ showAlert
133
+ }) => {
134
+ try {
135
+ const verbs = editingRights === 'readOnly' ? READ_ONLY_PERMS : WRITE_PERMS
136
+ return shareByLink(file, { verbs, ttl, password })
137
+ } catch (err) {
138
+ showAlert({
139
+ message: t(`${documentType}.share.shareByLink.permserror`),
140
+ severity: 'error',
141
+ variant: 'filled'
142
+ })
143
+ log.error(
144
+ "Error in 'readOnlyPermissionLink' function when trying to change permission",
145
+ err
146
+ )
147
+ }
148
+ }
149
+
150
+ /**
151
+ * updatePermissions - Updates the permissions of a file
152
+ * @param {object} options
153
+ * @param {import('cozy-client/types/types').IOCozyFile} options.file File to update permissions
154
+ * @param {Function} options.t i18n function
155
+ * @param {boolean} options.dateToggle - Expiration date toggle
156
+ * @param {Date|null} options.selectedDate - Expiration date
157
+ * @param {boolean} options.passwordToggle - Password toggle
158
+ * @param {string} options.password - Password
159
+ * @param {string} options.documentType - Type of the document
160
+ * @param {'readOnly'|'write'} options.editingRights - Editing rights
161
+ * @param {Function} options.updateDocumentPermissions - Function to update permissions
162
+ * @param {Function} options.showAlert - Function to display an alert
163
+ */
164
+ export const updatePermissions = async ({
165
+ file,
166
+ t,
167
+ dateToggle,
168
+ selectedDate,
169
+ passwordToggle,
170
+ password,
171
+ editingRights,
172
+ documentType,
173
+ updateDocumentPermissions,
174
+ showAlert
175
+ }) => {
176
+ try {
177
+ const expiresAt = dateToggle ? selectedDate?.toISOString() || undefined : ''
178
+ const ensurePassword = passwordToggle ? password || undefined : ''
179
+ const verbs = editingRights === 'readOnly' ? READ_ONLY_PERMS : WRITE_PERMS
180
+ return updateDocumentPermissions(file, {
181
+ verbs,
182
+ expiresAt,
183
+ password: ensurePassword
184
+ })
185
+ } catch (err) {
186
+ showAlert({
187
+ message: t(`${documentType}.share.shareByLink.permserror`),
188
+ severity: 'error',
189
+ variant: 'filled'
190
+ })
191
+ log.error(
192
+ "Error in 'updateDocumentPermissions' function when trying to change permission",
193
+ err
194
+ )
195
+ }
196
+ }
197
+
198
+ /**
199
+ * revokePermissions - Revokes the permissions of a file
200
+ * @param {object} options
201
+ * @param {import('cozy-client/types/types').IOCozyFile} options.file File to revoke permissions
202
+ * @param {Function} options.t i18n function
203
+ * @param {string} options.documentType - Type of the document
204
+ * @param {Function} options.revokeSharingLink - Function to revoke permissions
205
+ * @param {Function} options.showAlert - Function to display an alert
206
+ */
207
+ export const revokePermissions = async ({
208
+ file,
209
+ t,
210
+ documentType,
211
+ revokeSharingLink,
212
+ showAlert
213
+ }) => {
214
+ try {
215
+ return revokeSharingLink(file)
216
+ } catch (err) {
217
+ showAlert({
218
+ message: t(`${documentType}.share.error.revoke`),
219
+ severity: 'error',
220
+ variant: 'filled'
221
+ })
222
+ log.error(
223
+ "Error in 'revokePermissions' function when trying to revoke sharing link",
224
+ err
225
+ )
226
+ }
227
+ }
@@ -0,0 +1,159 @@
1
+ import { makeTTL, revokePermissions, updatePermissions } from './helpers'
2
+
3
+ describe('ShareRestrictionModal/helpers', () => {
4
+ describe('makeTTL', () => {
5
+ it('sould return undefined', () => {
6
+ expect(makeTTL()).toBeUndefined()
7
+ expect(makeTTL(123)).toBeUndefined()
8
+ expect(makeTTL('abc')).toBeUndefined()
9
+ expect(makeTTL(new Date('2023-01-01T00:00:00.000Z'))).toBeUndefined()
10
+ expect(makeTTL(new Date('abc'))).toBeUndefined()
11
+ expect(makeTTL('2023-01-01T00:00:00.000Z')).toBeUndefined()
12
+ })
13
+ it('sould return TTL in seconds', () => {
14
+ expect(makeTTL(new Date('2100-01-01T00:00:00.000Z'))).toBeDefined()
15
+ expect(makeTTL('2100-01-01T00:00:00.000Z')).toBeDefined()
16
+ })
17
+ })
18
+
19
+ describe('updatePermissions', () => {
20
+ const documentType = 'Files'
21
+ it('should call updateDocumentPermissions with "readOnly" permission', async () => {
22
+ const updateDocumentPermissions = jest.fn()
23
+ const file = { _id: '123' }
24
+
25
+ await updatePermissions({
26
+ file,
27
+ t: jest.fn(),
28
+ dateToggle: false,
29
+ selectedDate: null,
30
+ passwordToggle: false,
31
+ password: '',
32
+ editingRights: 'readOnly',
33
+ documentType,
34
+ updateDocumentPermissions,
35
+ showAlert: jest.fn()
36
+ })
37
+
38
+ expect(updateDocumentPermissions).toHaveBeenCalledWith(file, {
39
+ expiresAt: '',
40
+ password: '',
41
+ verbs: ['GET']
42
+ })
43
+ })
44
+
45
+ it('should call updateDocumentPermissions with "write" permissions', async () => {
46
+ const updateDocumentPermissions = jest.fn()
47
+ const file = { _id: '123' }
48
+
49
+ await updatePermissions({
50
+ file,
51
+ t: jest.fn(),
52
+ dateToggle: false,
53
+ selectedDate: null,
54
+ passwordToggle: false,
55
+ password: '',
56
+ editingRights: 'write',
57
+ documentType,
58
+ updateDocumentPermissions,
59
+ showAlert: jest.fn()
60
+ })
61
+
62
+ expect(updateDocumentPermissions).toHaveBeenCalledWith(file, {
63
+ expiresAt: '',
64
+ password: '',
65
+ verbs: ['GET', 'POST', 'PUT', 'PATCH']
66
+ })
67
+ })
68
+
69
+ it('should call updateDocumentPermissions with empty date & password if their switches are false', async () => {
70
+ const updateDocumentPermissions = jest.fn()
71
+ const file = { _id: '123' }
72
+
73
+ await updatePermissions({
74
+ file,
75
+ t: jest.fn(),
76
+ dateToggle: false,
77
+ selectedDate: null,
78
+ passwordToggle: false,
79
+ password: '',
80
+ editingRights: 'readOnly',
81
+ documentType,
82
+ updateDocumentPermissions,
83
+ showAlert: jest.fn()
84
+ })
85
+
86
+ expect(updateDocumentPermissions).toHaveBeenCalledWith(file, {
87
+ expiresAt: '',
88
+ password: '',
89
+ verbs: ['GET']
90
+ })
91
+ })
92
+
93
+ it('should call updateDocumentPermissions with "undefined" date & password if their switches are true but the permission doesn\'t yet have these values', async () => {
94
+ const updateDocumentPermissions = jest.fn()
95
+ const file = { _id: '123' }
96
+
97
+ await updatePermissions({
98
+ file,
99
+ t: jest.fn(),
100
+ dateToggle: true,
101
+ selectedDate: null,
102
+ passwordToggle: true,
103
+ password: '',
104
+ editingRights: 'readOnly',
105
+ documentType,
106
+ updateDocumentPermissions,
107
+ showAlert: jest.fn()
108
+ })
109
+
110
+ expect(updateDocumentPermissions).toHaveBeenCalledWith(file, {
111
+ expiresAt: undefined,
112
+ password: undefined,
113
+ verbs: ['GET']
114
+ })
115
+ })
116
+
117
+ it('should call updateDocumentPermissions with expected date & password if their switches are true', async () => {
118
+ const updateDocumentPermissions = jest.fn()
119
+ const file = { _id: '123' }
120
+
121
+ await updatePermissions({
122
+ file,
123
+ t: jest.fn(),
124
+ dateToggle: true,
125
+ selectedDate: new Date('2100-01-01T00:00:00.000Z'),
126
+ passwordToggle: true,
127
+ password: '1234',
128
+ editingRights: 'readOnly',
129
+ documentType,
130
+ updateDocumentPermissions,
131
+ showAlert: jest.fn()
132
+ })
133
+
134
+ expect(updateDocumentPermissions).toHaveBeenCalledWith(file, {
135
+ expiresAt: '2100-01-01T00:00:00.000Z',
136
+ password: '1234',
137
+ verbs: ['GET']
138
+ })
139
+ })
140
+ })
141
+
142
+ describe('revokePermissions', () => {
143
+ const documentType = 'Files'
144
+ it('should call revokeSharingLink', async () => {
145
+ const revokeSharingLink = jest.fn()
146
+ const file = { _id: '123' }
147
+
148
+ await revokePermissions({
149
+ file,
150
+ t: jest.fn(),
151
+ documentType,
152
+ revokeSharingLink,
153
+ showAlert: jest.fn()
154
+ })
155
+
156
+ expect(revokeSharingLink).toHaveBeenCalledWith(file)
157
+ })
158
+ })
159
+ })
@@ -37,9 +37,7 @@ const WhoHasAccess = ({
37
37
  onRevokeSelf,
38
38
  verifyRecipient,
39
39
  link,
40
- permissions,
41
- onUpdateShareLinkPermissions,
42
- onRevokeLink
40
+ permissions
43
41
  }) => {
44
42
  const previousLink = usePrevious(link)
45
43
  const linkHasBeenJustCreated = link && previousLink === null
@@ -58,8 +56,6 @@ const WhoHasAccess = ({
58
56
  onRevokeSelf={onRevokeSelf}
59
57
  link={link}
60
58
  permissions={permissions}
61
- onChangePermissions={onUpdateShareLinkPermissions}
62
- onDisable={onRevokeLink}
63
59
  fadeIn={linkHasBeenJustCreated}
64
60
  />
65
61
  )}
@@ -14,3 +14,15 @@ export const checkIsReadOnlyPermissions = permissions => {
14
14
  ).length > 0
15
15
  )
16
16
  }
17
+
18
+ export const checkIsPermissionHasExpiresDate = permissions => {
19
+ return Boolean(permissions?.[0]?.attributes?.expires_at)
20
+ }
21
+
22
+ export const getPermissionExpiresDate = permissions => {
23
+ return permissions?.[0]?.attributes?.expires_at
24
+ }
25
+
26
+ export const checkIsPermissionHasPassword = permissions => {
27
+ return Boolean(permissions?.[0]?.attributes?.password)
28
+ }
@@ -1,72 +0,0 @@
1
- import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
- import PropTypes from 'prop-types';
3
- import React, { useState } from 'react';
4
- import Button from 'cozy-ui/transpiled/react/Buttons';
5
- import { ConfirmDialog } from 'cozy-ui/transpiled/react/CozyDialogs';
6
- import FormControlLabel from 'cozy-ui/transpiled/react/FormControlLabel';
7
- import RadioGroup from 'cozy-ui/transpiled/react/RadioGroup';
8
- import Radio from 'cozy-ui/transpiled/react/Radios';
9
- import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n';
10
-
11
- var EditLinkPermissionDialog = function EditLinkPermissionDialog(_ref) {
12
- var open = _ref.open,
13
- onClose = _ref.onClose,
14
- document = _ref.document,
15
- onPermissionsSelected = _ref.onPermissionsSelected;
16
-
17
- var _useI18n = useI18n(),
18
- t = _useI18n.t;
19
-
20
- var _useState = useState(true),
21
- _useState2 = _slicedToArray(_useState, 2),
22
- isReadOnlyPermissions = _useState2[0],
23
- setIsReadOnlyPermissions = _useState2[1];
24
-
25
- var onChange = function onChange(event) {
26
- setIsReadOnlyPermissions(event.target.value === 'true');
27
- };
28
-
29
- var onConfirm = function onConfirm() {
30
- var verbs = isReadOnlyPermissions ? ['GET'] : ['GET', 'POST', 'PUT', 'PATCH'];
31
- onPermissionsSelected({
32
- verbs: verbs
33
- });
34
- onClose();
35
- };
36
-
37
- return /*#__PURE__*/React.createElement(ConfirmDialog, {
38
- open: open,
39
- onClose: onClose,
40
- title: t('Share.permissionLink.title'),
41
- content: /*#__PURE__*/React.createElement(RadioGroup, {
42
- "aria-label": "radio",
43
- value: isReadOnlyPermissions.toString(),
44
- onChange: onChange
45
- }, /*#__PURE__*/React.createElement(FormControlLabel, {
46
- value: "true",
47
- label: (document === null || document === void 0 ? void 0 : document.type) === 'directory' ? t("Share.permissionLink.seeFolder") : t("Share.permissionLink.seeFile"),
48
- control: /*#__PURE__*/React.createElement(Radio, null)
49
- }), /*#__PURE__*/React.createElement(FormControlLabel, {
50
- value: "false",
51
- label: (document === null || document === void 0 ? void 0 : document.type) === 'directory' ? t("Share.permissionLink.modifyFolder") : t("Share.permissionLink.modifyFile"),
52
- control: /*#__PURE__*/React.createElement(Radio, null)
53
- })),
54
- actions: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
55
- variant: "secondary",
56
- label: t('Share.permissionLink.cancel'),
57
- onClick: onClose
58
- }), /*#__PURE__*/React.createElement(Button, {
59
- variant: "primary",
60
- label: t('Share.permissionLink.confirm'),
61
- onClick: onConfirm
62
- }))
63
- });
64
- };
65
-
66
- EditLinkPermissionDialog.propTypes = {
67
- open: PropTypes.bool.isRequired,
68
- onClose: PropTypes.func.isRequired,
69
- document: PropTypes.object.isRequired,
70
- onPermissionsSelected: PropTypes.func.isRequired
71
- };
72
- export default EditLinkPermissionDialog;
@@ -1,48 +0,0 @@
1
- import _extends from "@babel/runtime/helpers/extends";
2
- import { render, fireEvent } from '@testing-library/react';
3
- import React from 'react';
4
- import { createMockClient } from 'cozy-client';
5
- import EditLinkPermissionDialog from './EditLinkPermissionDialog';
6
- import AppLike from '../../test/AppLike';
7
- describe('EditLinkPermissionDialog', function () {
8
- var client = createMockClient({});
9
-
10
- var setup = function setup(props) {
11
- return render( /*#__PURE__*/React.createElement(AppLike, {
12
- client: client
13
- }, /*#__PURE__*/React.createElement(EditLinkPermissionDialog, _extends({
14
- open: true,
15
- onClose: function onClose() {}
16
- }, props))));
17
- };
18
-
19
- it('should select read permissions', function () {
20
- var onPermissionsSelected = jest.fn();
21
- var props = {
22
- onPermissionsSelected: onPermissionsSelected
23
- };
24
-
25
- var _setup = setup(props),
26
- getByText = _setup.getByText;
27
-
28
- fireEvent.click(getByText('OK'));
29
- expect(onPermissionsSelected).toHaveBeenCalledWith({
30
- verbs: ['GET']
31
- });
32
- });
33
- it('should select write permissions', function () {
34
- var onPermissionsSelected = jest.fn();
35
- var props = {
36
- onPermissionsSelected: onPermissionsSelected
37
- };
38
-
39
- var _setup2 = setup(props),
40
- getByText = _setup2.getByText;
41
-
42
- fireEvent.click(getByText('Modify file'));
43
- fireEvent.click(getByText('OK'));
44
- expect(onPermissionsSelected).toHaveBeenCalledWith({
45
- verbs: ['GET', 'POST', 'PUT', 'PATCH']
46
- });
47
- });
48
- });
@@ -1,89 +0,0 @@
1
- import PropTypes from 'prop-types'
2
- import React, { useState } from 'react'
3
-
4
- import Button from 'cozy-ui/transpiled/react/Buttons'
5
- import { ConfirmDialog } from 'cozy-ui/transpiled/react/CozyDialogs'
6
- import FormControlLabel from 'cozy-ui/transpiled/react/FormControlLabel'
7
- import RadioGroup from 'cozy-ui/transpiled/react/RadioGroup'
8
- import Radio from 'cozy-ui/transpiled/react/Radios'
9
- import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'
10
-
11
- const EditLinkPermissionDialog = ({
12
- open,
13
- onClose,
14
- document,
15
- onPermissionsSelected
16
- }) => {
17
- const { t } = useI18n()
18
-
19
- const [isReadOnlyPermissions, setIsReadOnlyPermissions] = useState(true)
20
-
21
- const onChange = event => {
22
- setIsReadOnlyPermissions(event.target.value === 'true')
23
- }
24
-
25
- const onConfirm = () => {
26
- const verbs = isReadOnlyPermissions
27
- ? ['GET']
28
- : ['GET', 'POST', 'PUT', 'PATCH']
29
- onPermissionsSelected({ verbs })
30
- onClose()
31
- }
32
-
33
- return (
34
- <ConfirmDialog
35
- open={open}
36
- onClose={onClose}
37
- title={t('Share.permissionLink.title')}
38
- content={
39
- <RadioGroup
40
- aria-label="radio"
41
- value={isReadOnlyPermissions.toString()}
42
- onChange={onChange}
43
- >
44
- <FormControlLabel
45
- value="true"
46
- label={
47
- document?.type === 'directory'
48
- ? t(`Share.permissionLink.seeFolder`)
49
- : t(`Share.permissionLink.seeFile`)
50
- }
51
- control={<Radio />}
52
- />
53
- <FormControlLabel
54
- value="false"
55
- label={
56
- document?.type === 'directory'
57
- ? t(`Share.permissionLink.modifyFolder`)
58
- : t(`Share.permissionLink.modifyFile`)
59
- }
60
- control={<Radio />}
61
- />
62
- </RadioGroup>
63
- }
64
- actions={
65
- <>
66
- <Button
67
- variant="secondary"
68
- label={t('Share.permissionLink.cancel')}
69
- onClick={onClose}
70
- />
71
- <Button
72
- variant="primary"
73
- label={t('Share.permissionLink.confirm')}
74
- onClick={onConfirm}
75
- />
76
- </>
77
- }
78
- />
79
- )
80
- }
81
-
82
- EditLinkPermissionDialog.propTypes = {
83
- open: PropTypes.bool.isRequired,
84
- onClose: PropTypes.func.isRequired,
85
- document: PropTypes.object.isRequired,
86
- onPermissionsSelected: PropTypes.func.isRequired
87
- }
88
-
89
- export default EditLinkPermissionDialog
@@ -1,53 +0,0 @@
1
- import { render, fireEvent } from '@testing-library/react'
2
- import React from 'react'
3
-
4
- import { createMockClient } from 'cozy-client'
5
-
6
- import EditLinkPermissionDialog from './EditLinkPermissionDialog'
7
- import AppLike from '../../test/AppLike'
8
-
9
- describe('EditLinkPermissionDialog', () => {
10
- const client = createMockClient({})
11
-
12
- const setup = props => {
13
- return render(
14
- <AppLike client={client}>
15
- <EditLinkPermissionDialog open onClose={() => {}} {...props} />
16
- </AppLike>
17
- )
18
- }
19
-
20
- it('should select read permissions', () => {
21
- const onPermissionsSelected = jest.fn()
22
-
23
- const props = {
24
- onPermissionsSelected
25
- }
26
-
27
- const { getByText } = setup(props)
28
-
29
- fireEvent.click(getByText('OK'))
30
-
31
- expect(onPermissionsSelected).toHaveBeenCalledWith({
32
- verbs: ['GET']
33
- })
34
- })
35
-
36
- it('should select write permissions', () => {
37
- const onPermissionsSelected = jest.fn()
38
-
39
- const props = {
40
- onPermissionsSelected
41
- }
42
-
43
- const { getByText } = setup(props)
44
-
45
- fireEvent.click(getByText('Modify file'))
46
-
47
- fireEvent.click(getByText('OK'))
48
-
49
- expect(onPermissionsSelected).toHaveBeenCalledWith({
50
- verbs: ['GET', 'POST', 'PUT', 'PATCH']
51
- })
52
- })
53
- })