Skip to content

Commit

Permalink
Merge pull request #1018 from cozy/fix/ver-1068
Browse files Browse the repository at this point in the history
fix: Creation of a contact when selecting a linked contact
  • Loading branch information
Merkur39 authored Nov 7, 2024
2 parents 26b30f5 + 233d86e commit 362c60b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import PropTypes from 'prop-types'
import React from 'react'
import { useForm } from 'react-final-form'

import { makeDisplayName } from 'cozy-client/dist/models/contact'
import ContactsListModal from 'cozy-ui/transpiled/react/ContactsListModal'

export const RelatedContactList = ({ name, onClose, contacts }) => {
Expand All @@ -11,7 +12,8 @@ export const RelatedContactList = ({ name, onClose, contacts }) => {
* @param {import('cozy-client/types/types').IOCozyContact} contact
*/
const onClickContactsListModal = contact => {
change(name, contact.displayName)
// Use `makeDisplayName` because if the contact is newly created, it has no `displayName` attribute. (Creation of a contact when selecting a linked contact)
change(name, makeDisplayName(contact))
change(`${name}Id`, contact._id)
onClose()
}
Expand Down
4 changes: 3 additions & 1 deletion src/components/ContactCard/ContactForm/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import isEqual from 'lodash/isEqual'
import merge from 'lodash/merge'

import { Association } from 'cozy-client'
import { makeDisplayName } from 'cozy-client/dist/models/contact'

import contactToFormValues from './contactToFormValues'
import { DOCTYPE_CONTACTS } from '../../../helpers/doctypes'
Expand Down Expand Up @@ -203,7 +204,8 @@ export const makeRelatedContact = contact => {
}

const relatedData = contact.related.data.reduce((acc, curr) => {
acc[curr._id] = curr.displayName
// Use `makeDisplayName` because if the contact is newly created, it has no `displayName` attribute. (Creation of a contact when selecting a linked contact)
acc[curr._id] = curr.displayName || makeDisplayName(curr)
return acc
}, {})

Expand Down
30 changes: 29 additions & 1 deletion src/components/ContactCard/ContactForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types'
import React from 'react'
import { Form } from 'react-final-form'

import { getHasManyItems } from 'cozy-client/dist/associations/HasMany'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

import ContactFormField from './ContactFormField'
Expand Down Expand Up @@ -104,4 +105,31 @@ ContactForm.propTypes = {
})
}

export default ContactForm
// Used to avoid unnecessary multiple rendering of ContactForm when creating a new contact in another way.
// These unnecessary renderings prevented the addition of a newly created linked contact. (Creation of a contact when selecting a linked contact)
export const isSameContactProp = (prevProps, nextProps) => {
if (!prevProps.contact?.relationships || !nextProps.contact?.relationships) {
return false
}

const prevContactIdsRelated = getHasManyItems(
prevProps.contact,
'related'
).map(r => r._id)
const nextContactIdsRelated = getHasManyItems(
nextProps.contact,
'related'
).map(r => r._id)

if (
prevContactIdsRelated.length !== nextContactIdsRelated.length ||
!prevContactIdsRelated.every(id => nextContactIdsRelated.includes(id))
) {
return false
}

return true
}

// export default ContactForm
export default React.memo(ContactForm, isSameContactProp)
74 changes: 73 additions & 1 deletion src/components/ContactCard/ContactForm/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react'

import { createMockClient } from 'cozy-client'

import ContactForm from './index'
import ContactForm, { isSameContactProp } from './index'
import { johnDoeContact as contact } from '../../../helpers/testData'
import AppLike from '../../../tests/Applike'

Expand Down Expand Up @@ -215,4 +215,76 @@ describe('ContactForm', () => {

expect(onSubmit).not.toBeCalled()
})

describe('isSameContactProp', () => {
it('should return true if contacts have the same "related" relationships', () => {
const prevProps = {
contact: {
relationships: {
related: {
data: [{ _id: '1' }, { _id: '2' }]
}
}
}
}
const nextProps = {
contact: {
relationships: {
related: {
data: [{ _id: '2' }, { _id: '1' }]
}
}
}
}
expect(isSameContactProp(prevProps, nextProps)).toBe(true)
})

it('should return false if contacts have different "related" relationships', () => {
const prevProps = {
contact: {
relationships: {
related: {
data: [{ _id: '1' }, { _id: '2' }]
}
}
}
}
const nextProps = {
contact: {
relationships: {
related: {
data: [{ _id: '1' }, { _id: '3' }]
}
}
}
}
expect(isSameContactProp(prevProps, nextProps)).toBe(false)
})

it('should return false if one of the contacts has no "related" relationships', () => {
const prevProps = {
contact: {
relationships: {
related: {
data: [{ _id: '1' }, { _id: '2' }]
}
}
}
}
const nextProps = {
contact: {}
}
expect(isSameContactProp(prevProps, nextProps)).toBe(false)
})

it('should return false if both contacts have no "related" relationships', () => {
const prevProps = {
contact: {}
}
const nextProps = {
contact: {}
}
expect(isSameContactProp(prevProps, nextProps)).toBe(false)
})
})
})

0 comments on commit 362c60b

Please sign in to comment.