/
index.jsx
142 lines (132 loc) · 3.8 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { useState } from 'react'
import PropTypes from 'prop-types'
import cx from 'classnames'
import { Q, fetchPolicies, useClient, useQuery } from 'cozy-client'
import { DialogTitle, DialogContent } from '../Dialog'
import CozyTheme from '../providers/CozyTheme'
import {
TopAnchoredDialog,
DialogCloseButton,
useCozyDialog
} from '../CozyDialogs'
import useRealtime from '../hooks/useRealtime'
import useEventListener from '../hooks/useEventListener'
import useBreakpoints from '../providers/Breakpoints'
import Button from '../Buttons'
import PlusIcon from '../Icons/Plus'
import Icon from '../Icon'
import MobileHeader from './MobileHeader'
import ContactsListContent from './ContactsListContent'
import AddContactDialog from './AddContact/AddContactDialog'
import TextField from '../TextField'
import styles from './styles.styl'
const thirtySeconds = 30000
const olderThan30s = fetchPolicies.olderThan(thirtySeconds)
const contactsQuery = {
definition: Q('io.cozy.contacts').UNSAFE_noLimit(),
options: {
as: 'contacts',
fetchPolicy: olderThan30s
}
}
const ContactsListModal = ({
onItemClick,
placeholder,
addContactLabel,
emptyMessage,
dismissAction
}) => {
const [filter, setFilter] = useState('')
const [showAddDialog, setShowAddDialog] = useState(false)
const { isMobile } = useBreakpoints()
const { dialogProps, dialogTitleProps } = useCozyDialog({
size: 'large',
open: true,
onClose: dismissAction
})
const client = useClient()
const contacts = useQuery(contactsQuery.definition, contactsQuery.options)
useRealtime(
client,
{
'io.cozy.contacts': {
created: contacts.fetch,
updated: contacts.fetch
}
},
[]
)
useEventListener(document, 'resume', contacts.fetch)
const handleFilterChange = e => {
setFilter(e.target.value)
}
return (
<TopAnchoredDialog {...dialogProps}>
<CozyTheme variant={isMobile ? 'inverted' : 'normal'}>
<DialogCloseButton onClick={dismissAction} />
</CozyTheme>
<DialogTitle
{...dialogTitleProps}
className={cx(dialogTitleProps.className, {
'u-p-0 u-w-100': isMobile
})}
>
{isMobile ? (
<MobileHeader
filter={filter}
placeholder={placeholder}
onChange={handleFilterChange}
onDismiss={dismissAction}
/>
) : (
<TextField
variant="outlined"
type="text"
label={placeholder}
id={placeholder}
fullWidth
value={filter}
onChange={handleFilterChange}
autoFocus
/>
)}
</DialogTitle>
<DialogContent className="u-p-0">
<div className="dialogContentInner">
<div className={styles.ContactsListModal__addContactContainer}>
<Button
className={isMobile && 'u-mt-1'}
variant="secondary"
theme="secondary"
label={addContactLabel}
startIcon={<Icon icon={PlusIcon} />}
onClick={setShowAddDialog}
/>
</div>
<ContactsListContent
filter={filter}
contacts={contacts}
onItemClick={onItemClick}
emptyMessage={emptyMessage}
dismissAction={dismissAction}
/>
</div>
</DialogContent>
{showAddDialog && (
<AddContactDialog
onListClose={dismissAction}
onCreate={onItemClick}
onClose={() => setShowAddDialog(false)}
/>
)}
</TopAnchoredDialog>
)
}
ContactsListModal.propTypes = {
onItemClick: PropTypes.func,
placeholder: PropTypes.string,
addContactLabel: PropTypes.string,
emptyMessage: PropTypes.string,
dismissAction: PropTypes.func
}
export default ContactsListModal