Skip to content

Commit

Permalink
refactor: Add useQuery in ContactCardModal
Browse files Browse the repository at this point in the history
  • Loading branch information
JF-Cozy committed Aug 19, 2020
1 parent f45fd89 commit 0133658
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 68 deletions.
114 changes: 48 additions & 66 deletions src/components/Modals/ContactCardModal.jsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 (
<Modal into="body" dismissAction={onClose} size="xlarge" mobileFullscreen>
<Query query={client => client.get(DOCTYPE_CONTACTS, id)}>
{({ data: contact, fetchStatus: fetchContactStatus }) => {
return (
<Query
query={client =>
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 <SpinnerContact size="xxlarge" />
}
return (
<DumbContactCardModal
editMode={editMode}
contact={contact}
allGroups={allGroups}
t={t}
toggleConfirmDeleteModal={this.toggleConfirmDeleteModal}
toggleEditMode={this.toggleEditMode}
shouldDisplayConfirmDeleteModal={
shouldDisplayConfirmDeleteModal
}
deleteContact={this.deleteContact}
/>
)
}}
</Query>
)
}}
</Query>
</Modal>
)
}
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 (
<Modal into="body" dismissAction={onClose} size="xlarge" mobileFullscreen>
{resultContactById.fetchStatus !== 'loaded' ||
resultAllGroups.fetchStatus !== 'loaded' ? (
<SpinnerContact size="xxlarge" />
) : (
<DumbContactCardModal
editMode={editMode}
contact={contact}
allGroups={allGroups}
t={t}
toggleConfirmDeleteModal={toggleConfirmDeleteModal}
toggleEditMode={toggleEditMode}
shouldDisplayConfirmDeleteModal={shouldDisplayConfirmDeleteModal}
deleteContact={deleteContact}
/>
)}
</Modal>
)
}

export const DumbContactCardModal = ({
Expand Down
13 changes: 11 additions & 2 deletions src/components/Modals/ContactCardModal.spec.jsx
Original file line number Diff line number Diff line change
@@ -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 = ({
Expand Down Expand Up @@ -31,6 +33,13 @@ describe('ContactCardModal', () => {
onClose: jest.fn,
deleteContact: jest.fn
}
useQuery.mockReturnValue({
data: [],
fetchStatus: 'pending',
hasMore: true,
fetchMore: jest.fn()
})

const jsx = (
<AppLike client={client}>
<ContactCardModal {...props} />
Expand Down
21 changes: 21 additions & 0 deletions src/helpers/queries.js
Original file line number Diff line number Diff line change
@@ -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'
}
}

0 comments on commit 0133658

Please sign in to comment.