Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Creation of a contact when selecting a linked contact #1018

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand why we check only related things. It is because the only rerender that annoy us are when we created a related contact ?

Copy link
Member Author

@Merkur39 Merkur39 Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns are only due to the creation or update of contacts.
This can only happen directly by adding a linked contact, when we are in the creation form.

})
})
Loading