Skip to content

Commit

Permalink
feat: Allow to add group with only more than one member
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Mar 22, 2024
1 parent 5de831d commit 177dc4a
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 1,523 deletions.
3 changes: 2 additions & 1 deletion packages/cozy-sharing/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module.exports = {
collectCoverageFrom: [
'src/**/*.{js,jsx}',
'!**/node_modules/**',
'!**/vendor/**'
'!**/vendor/**',
'!**/*.stories.{js,jsx,ts,tsx}'
],
snapshotSerializers: ['enzyme-to-json/serializer'],
testPathIgnorePatterns: ['node_modules', 'dist', '__tests__'],
Expand Down
20 changes: 3 additions & 17 deletions packages/cozy-sharing/src/components/ContactSuggestion.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 from 'react'

Expand All @@ -13,28 +12,15 @@ import { Contact, Group, getDisplayName, getInitials } from '../models'

const ContactModel = models.contact

export const ContactSuggestion = ({ contactOrGroup, contacts }) => {
export const ContactSuggestion = ({ contactOrGroup }) => {
const { t } = useI18n()
let avatarText, name, details
if (contactOrGroup._type === Group.doctype) {
name = contactOrGroup.name
const membersCount = contacts
.reduce((total, contact) => {
if (
get(contact, 'relationships.groups.data', [])
.map(group => group._id)
.includes(contactOrGroup._id)
) {
return total + 1
}

return total
}, 0)
.toString()
avatarText = 'G'
details = t('Share.members.count', {
smart_count: membersCount
smart_count: contactOrGroup.membersCount.toString()
})
avatarText = 'G'
} else {
name = getDisplayName(contactOrGroup)
avatarText = getInitials(contactOrGroup)
Expand Down
115 changes: 16 additions & 99 deletions packages/cozy-sharing/src/components/ContactSuggestion.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { mount } from 'enzyme'
import { render, screen } from '@testing-library/react'
import React from 'react'

import { ContactSuggestion } from './ContactSuggestion'
import AppLike from '../../test/AppLike'

const WrappingComponent = ({ children }) => <AppLike>{children}</AppLike>

describe('ContactSuggestion component', () => {
const fakeT = jest.fn((s, args) => {
if (s === 'Share.members.count') {
return `${args.smart_count} members`
}
const setup = ({ contactOrGroup }) => {
return render(<ContactSuggestion contactOrGroup={contactOrGroup} />, {
wrapper: AppLike
})
}

return s
})
it('should display contact suggestion for a contact', () => {
const jonSnow = {
_id: 'f3a4e501-abbd',
Expand All @@ -36,17 +33,9 @@ describe('ContactSuggestion component', () => {
],
_type: 'io.cozy.contacts'
}
const contacts = [jonSnow]
const props = {
contactOrGroup: jonSnow,
contacts,
t: fakeT
}
const jsx = <ContactSuggestion {...props} />
const wrapper = mount(jsx, {
wrappingComponent: WrappingComponent
})
expect(wrapper).toMatchSnapshot()
setup({ contactOrGroup: jonSnow })
expect(screen.getByText('Jon Snow')).toBeInTheDocument()
expect(screen.getByText('https://jonsnow.mycozy.cloud')).toBeInTheDocument()
})

it('should display contact suggestion for a contact without fullname (for example created by a share modal)', () => {
Expand All @@ -62,91 +51,19 @@ describe('ContactSuggestion component', () => {
name: undefined,
_type: 'io.cozy.contacts'
}
const contacts = [jonSnow]
const props = {
contactOrGroup: jonSnow,
contacts,
t: fakeT
}
const jsx = <ContactSuggestion {...props} />
const wrapper = mount(jsx, {
wrappingComponent: WrappingComponent
})
expect(wrapper).toMatchSnapshot()
setup({ contactOrGroup: jonSnow })
expect(screen.getByText('jon.snow@email.com')).toBeInTheDocument()
})

it('should display contact suggestion for a group', () => {
const jon = {
_id: 'f3a4e501-abbd',
fullname: 'Jon Snow',
name: {
givenName: 'Jon',
familyName: 'Snow'
},
email: [
{
address: 'jon.snow@email.com',
type: 'primary'
}
],
_type: 'io.cozy.contacts',
relationships: {
groups: {
data: [{ _id: '610718e6-2d7a', _type: 'io.cozy.contacts.groups' }]
}
}
}
const cersei = {
_id: '42dc490a-7878',
fullname: 'Cersei Lannister',
name: {
givenName: 'Cersei',
familyName: 'Lannister'
},
email: [
{
address: 'cersei@email.com',
type: 'primary'
}
],
_type: 'io.cozy.contacts',
relationships: { groups: { data: [] } }
}
const sam = {
_id: 'b5bed853-93da',
fullname: 'Samwell Tarly',
name: {
givenName: 'Samwell',
familyName: 'Tarly'
},
email: [
{
address: 'samwell@email.com',
type: 'primary'
}
],
_type: 'io.cozy.contacts',
relationships: {
groups: {
data: [{ _id: '610718e6-2d7a', _type: 'io.cozy.contacts.groups' }]
}
}
}
const contacts = [jon, cersei, sam]
const theNightsWatch = {
_id: '610718e6-2d7a',
name: "The Night's Watch",
_type: 'io.cozy.contacts.groups'
_type: 'io.cozy.contacts.groups',
membersCount: 2
}
const props = {
contactOrGroup: theNightsWatch,
contacts,
t: fakeT
}
const jsx = <ContactSuggestion {...props} />
const wrapper = mount(jsx, {
wrappingComponent: WrappingComponent
})
expect(wrapper).toMatchSnapshot()
setup({ contactOrGroup: theNightsWatch })
expect(screen.getByText("The Night's Watch")).toBeInTheDocument()
expect(screen.getByText('2 members')).toBeInTheDocument()
})
})
7 changes: 1 addition & 6 deletions packages/cozy-sharing/src/components/ShareAutosuggest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,7 @@ const ShareAutocomplete = ({
)

const renderSuggestion = contactOrGroup => (
<ContactSuggestion
contacts={contactsAndGroups.filter(
item => item._type === Contact.doctype
)}
contactOrGroup={contactOrGroup}
/>
<ContactSuggestion contactOrGroup={contactOrGroup} />
)

return (
Expand Down
20 changes: 19 additions & 1 deletion packages/cozy-sharing/src/components/ShareRecipientsInput.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import get from 'lodash/get'
import PropTypes from 'prop-types'
import React, { useState, useEffect } from 'react'

Expand Down Expand Up @@ -42,7 +43,24 @@ const ShareRecipientsInput = ({
if (contacts.hasMore || contacts.fetchStatus === 'loading') {
return contacts.data
} else {
return [...contacts.data, ...contactGroups.data]
const contactGroupsWithCount = contactGroups.data
.map(contactGroup => ({
...contactGroup,
membersCount: contacts.data.reduce((total, contact) => {
if (
get(contact, 'relationships.groups.data', [])
.map(group => group._id)
.includes(contactGroup._id)
) {
return total + 1
}

return total
}, 0)
}))
.filter(contactGroup => contactGroup.membersCount > 0)

return [...contacts.data, ...contactGroupsWithCount]
}
}

Expand Down

0 comments on commit 177dc4a

Please sign in to comment.