cozy-sharing 32.3.2 → 33.0.1

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 (30) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/components/FederatedFolder/FederatedFolderModal.js +2 -1
  3. package/dist/components/ShareAutosuggest.js +15 -5
  4. package/dist/components/ShareAutosuggest.spec.js +26 -2
  5. package/dist/components/ShareByEmail.js +3 -0
  6. package/dist/components/ShareByEmail.spec.js +2 -1
  7. package/dist/components/ShareDialogCozyToCozy.js +2 -1
  8. package/dist/components/ShareDialogCozyToCozy.spec.js +2 -2
  9. package/dist/components/ShareRecipientsInput.js +3 -0
  10. package/dist/components/SharedDrive/EditSharedDriveModal.js +32 -55
  11. package/dist/components/SharedDrive/SharedDriveForm.js +23 -5
  12. package/dist/components/SharedDrive/useSharedDrive.js +8 -37
  13. package/dist/components/SharedDrive/useSharedDrive.spec.js +8 -81
  14. package/dist/components/WhoHasAccess.js +4 -1
  15. package/dist/helpers/contacts.js +12 -0
  16. package/package.json +4 -4
  17. package/src/components/FederatedFolder/FederatedFolderModal.jsx +1 -0
  18. package/src/components/ShareAutosuggest.jsx +17 -11
  19. package/src/components/ShareAutosuggest.spec.jsx +20 -1
  20. package/src/components/ShareByEmail.jsx +3 -0
  21. package/src/components/ShareByEmail.spec.jsx +1 -0
  22. package/src/components/ShareDialogCozyToCozy.jsx +1 -0
  23. package/src/components/ShareDialogCozyToCozy.spec.jsx +1 -0
  24. package/src/components/ShareRecipientsInput.jsx +3 -0
  25. package/src/components/SharedDrive/EditSharedDriveModal.jsx +42 -47
  26. package/src/components/SharedDrive/SharedDriveForm.jsx +18 -2
  27. package/src/components/SharedDrive/useSharedDrive.js +15 -35
  28. package/src/components/SharedDrive/useSharedDrive.spec.js +4 -39
  29. package/src/components/WhoHasAccess.jsx +3 -1
  30. package/src/helpers/contacts.js +12 -0
@@ -1,17 +1,13 @@
1
1
  import { useState } from 'react'
2
2
 
3
3
  import { useClient } from 'cozy-client'
4
- import { useAlert } from 'cozy-ui/transpiled/react/providers/Alert'
5
- import { useI18n } from 'twake-i18n'
6
4
 
7
5
  import { getOrCreateFromArray } from '../../helpers/contacts'
8
6
  import { usePendingRecipients } from '../../hooks/usePendingRecipients'
9
7
  import { Contact } from '../../models'
10
8
 
11
- export const useSharedDrive = ({ onSuccess }) => {
9
+ export const useSharedDrive = () => {
12
10
  const client = useClient()
13
- const { t } = useI18n()
14
- const { showAlert } = useAlert()
15
11
 
16
12
  const [sharedDriveName, setSharedDriveName] = useState('')
17
13
  const {
@@ -28,36 +24,20 @@ export const useSharedDrive = ({ onSuccess }) => {
28
24
  const createContact = contact => client.create(Contact.doctype, contact)
29
25
 
30
26
  const onCreate = async () => {
31
- try {
32
- const contacts = await getOrCreateFromArray(
33
- client,
34
- pendingRecipients,
35
- createContact
36
- )
37
- const readWriteRecipients = selectedOption === 'readOnly' ? [] : contacts
38
- const readOnlyRecipients = selectedOption === 'readOnly' ? contacts : []
39
-
40
- await client.collection('io.cozy.sharings').createSharedDrive({
41
- name: sharedDriveName,
42
- description: sharedDriveName,
43
- recipients: readWriteRecipients,
44
- readOnlyRecipients
45
- })
46
-
47
- showAlert({
48
- message: t('SharedDrive.sharedDriveModal.successNotification'),
49
- severity: 'success',
50
- variant: 'filled'
51
- })
52
-
53
- onSuccess()
54
- } catch (_err) {
55
- showAlert({
56
- message: t('SharedDrive.sharedDriveModal.errorNotification'),
57
- severity: 'error',
58
- variant: 'filled'
59
- })
60
- }
27
+ const contacts = await getOrCreateFromArray(
28
+ client,
29
+ pendingRecipients,
30
+ createContact
31
+ )
32
+ const readWriteRecipients = selectedOption === 'readOnly' ? [] : contacts
33
+ const readOnlyRecipients = selectedOption === 'readOnly' ? contacts : []
34
+
35
+ await client.collection('io.cozy.sharings').createSharedDrive({
36
+ name: sharedDriveName,
37
+ description: sharedDriveName,
38
+ recipients: readWriteRecipients,
39
+ readOnlyRecipients
40
+ })
61
41
  }
62
42
 
63
43
  return {
@@ -2,12 +2,10 @@ import { renderHook, act } from '@testing-library/react-hooks'
2
2
 
3
3
  import { useSharedDrive } from './useSharedDrive'
4
4
 
5
- const mockShowAlert = jest.fn()
6
5
  const mockCreateSharedDrive = jest.fn()
7
6
  const mockCollection = jest.fn(() => ({
8
7
  createSharedDrive: mockCreateSharedDrive
9
8
  }))
10
- const mockT = jest.fn(key => key)
11
9
 
12
10
  jest.mock('cozy-client', () => ({
13
11
  useClient: jest.fn(() => ({
@@ -16,14 +14,6 @@ jest.mock('cozy-client', () => ({
16
14
  }))
17
15
  }))
18
16
 
19
- jest.mock('cozy-ui/transpiled/react/providers/Alert', () => ({
20
- useAlert: jest.fn(() => ({ showAlert: mockShowAlert }))
21
- }))
22
-
23
- jest.mock('twake-i18n', () => ({
24
- useI18n: jest.fn(() => ({ t: mockT }))
25
- }))
26
-
27
17
  jest.mock('../../models', () => ({
28
18
  Contact: { doctype: 'io.cozy.contacts' }
29
19
  }))
@@ -40,18 +30,14 @@ describe('useSharedDrive', () => {
40
30
  })
41
31
 
42
32
  it('returns empty initial state', () => {
43
- const { result } = renderHook(() =>
44
- useSharedDrive({ onSuccess: jest.fn() })
45
- )
33
+ const { result } = renderHook(() => useSharedDrive())
46
34
  expect(result.current.sharedDriveName).toBe('')
47
35
  expect(result.current.pendingRecipients).toEqual([])
48
36
  expect(result.current.selectedOption).toBe('readWrite')
49
37
  })
50
38
 
51
39
  it('handleSharedDriveNameChange updates the name', () => {
52
- const { result } = renderHook(() =>
53
- useSharedDrive({ onSuccess: jest.fn() })
54
- )
40
+ const { result } = renderHook(() => useSharedDrive())
55
41
  act(() => {
56
42
  result.current.handleSharedDriveNameChange({
57
43
  target: { value: 'My Drive' }
@@ -60,11 +46,10 @@ describe('useSharedDrive', () => {
60
46
  expect(result.current.sharedDriveName).toBe('My Drive')
61
47
  })
62
48
 
63
- it('onCreate calls createSharedDrive and onSuccess', async () => {
64
- const onSuccess = jest.fn()
49
+ it('onCreate calls createSharedDrive', async () => {
65
50
  mockCreateSharedDrive.mockResolvedValue({})
66
51
 
67
- const { result } = renderHook(() => useSharedDrive({ onSuccess }))
52
+ const { result } = renderHook(() => useSharedDrive())
68
53
 
69
54
  act(() => {
70
55
  result.current.handleSharedDriveNameChange({
@@ -83,25 +68,5 @@ describe('useSharedDrive', () => {
83
68
  recipients: [{ email: 'a@b.c' }],
84
69
  readOnlyRecipients: []
85
70
  })
86
- expect(mockShowAlert).toHaveBeenCalledWith(
87
- expect.objectContaining({ severity: 'success' })
88
- )
89
- expect(onSuccess).toHaveBeenCalledTimes(1)
90
- })
91
-
92
- it('onCreate shows error alert and does not call onSuccess on failure', async () => {
93
- const onSuccess = jest.fn()
94
- mockCreateSharedDrive.mockRejectedValue(new Error('network error'))
95
-
96
- const { result } = renderHook(() => useSharedDrive({ onSuccess }))
97
-
98
- await act(async () => {
99
- await result.current.onCreate()
100
- })
101
-
102
- expect(mockShowAlert).toHaveBeenCalledWith(
103
- expect.objectContaining({ severity: 'error' })
104
- )
105
- expect(onSuccess).not.toHaveBeenCalled()
106
71
  })
107
72
  })
@@ -29,6 +29,7 @@ const RecipientWaitingForConfirmationAlert = ({ recipientsToBeConfirmed }) => {
29
29
  const WhoHasAccess = ({
30
30
  isOwner = false,
31
31
  isSharedDrive = false,
32
+ showOwner = true,
32
33
  recipients,
33
34
  recipientsToBeConfirmed = [],
34
35
  document,
@@ -61,7 +62,7 @@ const WhoHasAccess = ({
61
62
  />
62
63
  )}
63
64
 
64
- <OwnerRecipient recipients={recipients} />
65
+ {showOwner && <OwnerRecipient recipients={recipients} />}
65
66
 
66
67
  <RecipientList
67
68
  recipients={recipients}
@@ -82,6 +83,7 @@ const WhoHasAccess = ({
82
83
 
83
84
  WhoHasAccess.propTypes = {
84
85
  isOwner: PropTypes.bool,
86
+ showOwner: PropTypes.bool,
85
87
  recipients: PropTypes.array.isRequired,
86
88
  recipientsToBeConfirmed: PropTypes.arrayOf(
87
89
  PropTypes.shape({
@@ -47,3 +47,15 @@ export const getContactsFromGroupId = (contacts = [], groupId) => {
47
47
  )
48
48
  })
49
49
  }
50
+
51
+ /**
52
+ * Returns true when a contact does not exist. A contact without
53
+ * an `_id` is always a placeholder that must be created before sharing.
54
+ *
55
+ * Used to decide whether the autosuggest input should allow creating
56
+ * a new contact on the fly or should block the pick (depending on the
57
+ * `enableCreateContact` flag).
58
+ */
59
+ export const isContactToBeCreated = contact => {
60
+ return !contact._id
61
+ }