Skip to content

Commit

Permalink
feat(Viewer): Use contact formatting from cozy-client
Browse files Browse the repository at this point in the history
  • Loading branch information
zatteo committed Mar 12, 2024
1 parent 98b6510 commit eb9c596
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
20 changes: 14 additions & 6 deletions react/Viewer/Panel/QualificationListItemContact.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React, { useRef, useState } from 'react'
import PropTypes from 'prop-types'

import {
getTranslatedNameForContact,
formatContactValue
} from 'cozy-client/dist/models/paper'

import ListItem from '../../ListItem'
import ListItemSecondaryAction from '../../ListItemSecondaryAction'
import IconButton from '../../IconButton'
Expand All @@ -13,7 +18,7 @@ import { useI18n } from '../../providers/I18n'
import ActionMenuWrapper from './ActionMenuWrapper'

const QualificationListItemContact = ({ file }) => {
const { t } = useI18n()
const { lang } = useI18n()
const actionBtnRef = useRef()
const [optionFile, setOptionFile] = useState({
name: '',
Expand All @@ -27,7 +32,7 @@ const QualificationListItemContact = ({ file }) => {
return { name, value }
})

const { contactName, isLoadingContacts } = useReferencedContactName(file)
const { contacts, isLoadingContacts } = useReferencedContactName(file)

if (isLoadingContacts) {
return (
Expand All @@ -37,21 +42,24 @@ const QualificationListItemContact = ({ file }) => {
)
}

if (!isLoadingContacts && !contactName) {
const formattedTitle = getTranslatedNameForContact({ lang })
const formattedValue = formatContactValue(contacts)

if (!isLoadingContacts && !formattedValue) {
return null
}

return (
<>
<ListItem className={'u-ph-2'}>
<QualificationListItemText
primary={t('Viewer.panel.qualification.contact')}
secondary={contactName}
primary={formattedTitle}
secondary={formattedValue}
/>
<ListItemSecondaryAction>
<IconButton
ref={actionBtnRef}
onClick={() => toggleActionsMenu('contact', contactName)}
onClick={() => toggleActionsMenu('contact', formattedValue)}
>
<Icon icon={Dots} />
</IconButton>
Expand Down
13 changes: 2 additions & 11 deletions react/Viewer/hooks/useReferencedContactName.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { getReferencedBy, useQuery, models, isQueryLoading } from 'cozy-client'
import { getReferencedBy, useQuery, isQueryLoading } from 'cozy-client'

import { buildContactByIdsQuery } from '../queries'

const {
contact: { getDisplayName }
} = models

const useReferencedContactName = file => {
const contactIds = getReferencedBy(file, 'io.cozy.contacts').map(
ref => ref.id
Expand All @@ -25,11 +21,6 @@ const useReferencedContactName = file => {
isContactByIdsQueryEnabled &&
(isQueryLoading(contactsQueryResult) || contactsQueryResult.hasMore)

const contactName =
contacts && contacts.length > 0
? contacts.map(contact => `${getDisplayName(contact)}`).join(', ')
: ''

return { contactName, isLoadingContacts }
return { contacts, isLoadingContacts }
}
export default useReferencedContactName

0 comments on commit eb9c596

Please sign in to comment.