Skip to content

Commit

Permalink
feat: Extract logic from onRecipientPick to an helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Mar 21, 2024
1 parent 8604f13 commit 2d08da1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
38 changes: 10 additions & 28 deletions packages/cozy-sharing/src/components/ShareByEmail.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import get from 'lodash/get'
import PropTypes from 'prop-types'
import React, { useState } from 'react'

Expand All @@ -11,9 +10,11 @@ import { ShareRecipientsLimitModal } from './ShareRecipientsLimitModal'
import ShareSubmit from './Sharesubmit'
import ShareTypeSelect from './Sharetypeselect'
import { getOrCreateFromArray } from '../helpers/contacts'
import { hasReachRecipientsLimit } from '../helpers/recipients'
import {
spreadGroupAndMergeRecipients,
hasReachRecipientsLimit
} from '../helpers/recipients'
import { getSuccessMessage } from '../helpers/successMessage'
import { Group } from '../models'
import { contactsResponseType, groupsResponseType } from '../propTypes'
import { isReadOnlySharing } from '../state'
import styles from '../styles/share.styl'
Expand Down Expand Up @@ -46,31 +47,12 @@ export const ShareByEmail = ({
}

const onRecipientPick = recipient => {
let contactsToAdd
if (recipient._type === Group.doctype) {
const groupId = recipient.id
contactsToAdd = contacts.data.filter(contact => {
const contactGroupIds = get(
contact,
'relationships.groups.data',
[]
).map(group => group._id)

return contactGroupIds.includes(groupId)
})
} else {
contactsToAdd = [recipient]
}

const filtered = contactsToAdd
.filter(
contact =>
(contact.email && contact.email.length > 0) ||
(contact.cozy && contact.cozy.length > 0)
)
.filter(contact => !recipients.find(r => r === contact))

setRecipients([...recipients, ...filtered])
const mergedRecipients = spreadGroupAndMergeRecipients(
recipients,
recipient,
contacts
)
setRecipients(mergedRecipients)
}

const onRecipientRemove = recipient => {
Expand Down
41 changes: 41 additions & 0 deletions packages/cozy-sharing/src/helpers/recipients.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import get from 'lodash/get'

import flag from 'cozy-flags'

import { Group } from '../models'

export const filterAndReworkRecipients = (recipients, previousRecipients) => {
return recipients
.filter(recipient => recipient.status !== 'owner')
Expand Down Expand Up @@ -45,3 +49,40 @@ export const hasReachRecipientsLimit = (current, next) => {
}
return false
}

/**
* Merges the recipients list with a new recipient and their associated contacts.
* @param {object[]} recipients - The list of recipients.
* @param {object} recipient - The new recipient.
* @param {object[]} contacts - The list of contacts.
* @returns {object[]} - The merged recipients list.
*/
export const spreadGroupAndMergeRecipients = (
recipients,
newRecipient,
contacts
) => {
let contactsToAdd
if (newRecipient._type === Group.doctype) {
const groupId = newRecipient.id
contactsToAdd = contacts.data.filter(contact => {
const contactGroupIds = get(contact, 'relationships.groups.data', []).map(
group => group._id
)

return contactGroupIds.includes(groupId)
})
} else {
contactsToAdd = [newRecipient]
}

const filtered = contactsToAdd
.filter(
contact =>
(contact.email && contact.email.length > 0) ||
(contact.cozy && contact.cozy.length > 0)
)
.filter(contact => !recipients.find(r => r === contact))

return [...recipients, ...filtered]
}

0 comments on commit 2d08da1

Please sign in to comment.