From 01336581e5e0177c5d1b7c6b9f91dbe9c96bd47a Mon Sep 17 00:00:00 2001 From: JF-Cozy Date: Wed, 19 Aug 2020 17:04:48 +0200 Subject: [PATCH] refactor: Add useQuery in ContactCardModal --- src/components/Modals/ContactCardModal.jsx | 114 ++++++++---------- .../Modals/ContactCardModal.spec.jsx | 13 +- src/helpers/queries.js | 21 ++++ 3 files changed, 80 insertions(+), 68 deletions(-) create mode 100644 src/helpers/queries.js diff --git a/src/components/Modals/ContactCardModal.jsx b/src/components/Modals/ContactCardModal.jsx index 80812ca95..4c1568253 100644 --- a/src/components/Modals/ContactCardModal.jsx +++ b/src/components/Modals/ContactCardModal.jsx @@ -1,11 +1,10 @@ -import React from 'react' +import React, { useState } from 'react' import { PropTypes } from 'prop-types' import { translate } from 'cozy-ui/transpiled/react/I18n' import Modal, { ModalHeader, ModalContent } from 'cozy-ui/transpiled/react/Modal' -import { DOCTYPE_CONTACTS } from '../../helpers/doctypes' import { getConnectedAccounts } from '../../helpers/contacts' import withContactsMutations from '../../connections/allContacts' @@ -14,83 +13,66 @@ import SpinnerContact from '../Components/Spinner' import ContactFormModal from './ContactFormModal' import ContactGroups from '../ContactCard/ContactGroups' import Button from 'cozy-ui/transpiled/react/Button' -import { Query } from 'cozy-client' +import { useQuery } from 'cozy-client' import { flow } from 'lodash' +import { queryContactById, queryAllGroups } from '../../helpers/queries' -export class ContactCardModal extends React.Component { - state = { - editMode: false, - shouldDisplayConfirmDeleteModal: false - } +const ContactCardModal = props => { + const [editMode, setEditMode] = useState(false) + const [ + shouldDisplayConfirmDeleteModal, + setShouldDisplayConfirmDeleteModal + ] = useState(false) - toggleConfirmDeleteModal = () => { - this.setState(state => ({ - shouldDisplayConfirmDeleteModal: !state.shouldDisplayConfirmDeleteModal - })) + const toggleConfirmDeleteModal = () => { + setShouldDisplayConfirmDeleteModal(!shouldDisplayConfirmDeleteModal) } - deleteContact = async (contactParam = null) => { - const { contact, deleteContact, onDeleteContact, onClose } = this.props + const deleteContact = async (contactParam = null) => { + const { contact, deleteContact, onDeleteContact, onClose } = props onClose && onClose() await deleteContact(contactParam ? contactParam : contact) onDeleteContact && onDeleteContact(contactParam ? contactParam : contact) } - toggleEditMode = () => { - this.setState(state => ({ - editMode: !state.editMode - })) + const toggleEditMode = () => { + setEditMode(!editMode) } - render() { - const { onClose, t, id } = this.props - const { editMode, shouldDisplayConfirmDeleteModal } = this.state + const { onClose, t, id } = props - return ( - - client.get(DOCTYPE_CONTACTS, id)}> - {({ data: contact, fetchStatus: fetchContactStatus }) => { - return ( - - client - .find('io.cozy.contacts.groups') - .where({ - trashed: { $exists: false } - }) - .sortBy([{ name: 'asc' }]) - .indexFields(['name']) - } - > - {({ data: allGroups, fetchStatus: allGroupsContactStatus }) => { - if ( - fetchContactStatus !== 'loaded' || - allGroupsContactStatus !== 'loaded' - ) { - return - } - return ( - - ) - }} - - ) - }} - - - ) - } + const resultContactById = useQuery( + queryContactById.definition(id), + queryContactById.options + ) + const resultAllGroups = useQuery( + queryAllGroups.definition, + queryAllGroups.options + ) + + const contact = + resultContactById.data !== null ? resultContactById.data[0] : {} + const allGroups = resultAllGroups.data !== null ? resultAllGroups.data : [] + + return ( + + {resultContactById.fetchStatus !== 'loaded' || + resultAllGroups.fetchStatus !== 'loaded' ? ( + + ) : ( + + )} + + ) } export const DumbContactCardModal = ({ diff --git a/src/components/Modals/ContactCardModal.spec.jsx b/src/components/Modals/ContactCardModal.spec.jsx index c36313e2e..9ac0fb7b8 100644 --- a/src/components/Modals/ContactCardModal.spec.jsx +++ b/src/components/Modals/ContactCardModal.spec.jsx @@ -1,9 +1,11 @@ import React from 'react' -import { ContactCardModal, DumbContactCardModal } from './ContactCardModal' +import ContactCardModal, { DumbContactCardModal } from './ContactCardModal' import { render } from '@testing-library/react' -import { createMockClient } from 'cozy-client' import AppLike from '../../tests/Applike' import { contactWithGroup as contact, groups } from '../../helpers/testData' +import { createMockClient, useQuery } from 'cozy-client' + +jest.mock('cozy-client/dist/hooks/useQuery', () => jest.fn()) const client = createMockClient({}) const setup = ({ @@ -31,6 +33,13 @@ describe('ContactCardModal', () => { onClose: jest.fn, deleteContact: jest.fn } + useQuery.mockReturnValue({ + data: [], + fetchStatus: 'pending', + hasMore: true, + fetchMore: jest.fn() + }) + const jsx = ( diff --git a/src/helpers/queries.js b/src/helpers/queries.js new file mode 100644 index 000000000..beda321e9 --- /dev/null +++ b/src/helpers/queries.js @@ -0,0 +1,21 @@ +import { Q } from 'cozy-client' +import { DOCTYPE_CONTACTS } from './doctypes' + +export const queryContactById = { + definition: id => () => Q(DOCTYPE_CONTACTS).getById(id), + options: { + as: 'contactById' + } +} +export const queryAllGroups = { + definition: () => + Q('io.cozy.contacts.groups') + .where({ + trashed: { $exists: false } + }) + .sortBy([{ name: 'asc' }]) + .indexFields(['name']), + options: { + as: 'allGroups' + } +}