Skip to content

Commit

Permalink
fix: Don't display "Me" in the SharingRecipients.
Browse files Browse the repository at this point in the history
  • Loading branch information
Crash-- committed Nov 20, 2020
1 parent d3b7062 commit f291aeb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
22 changes: 16 additions & 6 deletions packages/cozy-sharing/src/components/Recipient.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@ export const MAX_DISPLAYED_RECIPIENTS = 3
const DEFAULT_DISPLAY_NAME = 'Share.contacts.defaultDisplayName'

/**
* Exclude me from the list of recipients if I'm the owner of the share
* Exclude me from the list of recipients.
* @typedef {object} Recipient
* @param {array<Recipient>} recipients - List of recipients
* @param {boolean} isOwner - Indicates if I'm the owner or not
* @param {object} client - CozyClient instance
* @returns {array<Recipient>} List of recipients without me if I'm the owner
*/
const excludeMeAsOwnerFromRecipients = ({ recipients, isOwner }) => {
return recipients.filter(recipient =>
isOwner ? recipient.status !== 'owner' : recipient
)
export const excludeMeAsOwnerFromRecipients = ({
recipients,
isOwner,
client
}) => {
return recipients.filter(recipient => {
if (isOwner) {
return recipient.status !== 'owner'
}
return recipient.instance !== client.options.uri
})
}

export const RecipientsAvatars = ({
Expand All @@ -44,11 +52,13 @@ export const RecipientsAvatars = ({
isOwner,
showMeAsOwner
}) => {
const client = useClient()
const filteredRecipients = showMeAsOwner
? recipients.slice().reverse() // we slice first to clone the original array because reverser() mutates it
: excludeMeAsOwnerFromRecipients({
recipients,
isOwner
isOwner,
client
}).reverse()
// we reverse the recipients array because we use `flex-direction: row-reverse` to display them correctly

Expand Down
40 changes: 39 additions & 1 deletion packages/cozy-sharing/src/components/Recipient.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import Recipient from './Recipient'
import Recipient, { excludeMeAsOwnerFromRecipients } from './Recipient'
import AppLike from '../../test/AppLike'
import { createMockClient } from 'cozy-client'
import { render, fireEvent } from '@testing-library/react'
Expand Down Expand Up @@ -96,3 +96,41 @@ describe('Recipient component', () => {
expect(onRevokeSelf).not.toBeCalled()
})
})

describe('excludeMeAsOwnerFromRecipients', () => {
test('excludeMeAsOwnerFromRecipients behavior', () => {
const recipients = [
{
status: 'owner',
instance: 'http://foo1.cozy.bar'
},
{ status: 'pending', instance: 'http://foo2.cozy.bar' },
{
status: 'pending',
instance: 'http://foo3.cozy.bar'
}
]
const client = { options: { uri: 'http://foo2.cozy.bar' } }
expect(
excludeMeAsOwnerFromRecipients({ recipients, isOwner: true, client })
).toEqual([
{ status: 'pending', instance: 'http://foo2.cozy.bar' },
{
status: 'pending',
instance: 'http://foo3.cozy.bar'
}
])
expect(
excludeMeAsOwnerFromRecipients({ recipients, isOwner: false, client })
).toEqual([
{
status: 'owner',
instance: 'http://foo1.cozy.bar'
},
{
status: 'pending',
instance: 'http://foo3.cozy.bar'
}
])
})
})

0 comments on commit f291aeb

Please sign in to comment.