-
Notifications
You must be signed in to change notification settings - Fork 44
/
GroupSettings.jsx
287 lines (261 loc) · 7.82 KB
/
GroupSettings.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import React, { Component } from 'react'
import { withRouter } from 'react-router'
import { sortBy, flowRight as compose } from 'lodash'
import { Query, withClient } from 'cozy-client'
import { translate, withBreakpoints } from 'cozy-ui/transpiled/react'
import Button from 'cozy-ui/transpiled/react/Button'
import Toggle from 'cozy-ui/transpiled/react/Toggle'
import Alerter from 'cozy-ui/transpiled/react/Alerter'
import { Media, Img } from 'cozy-ui/transpiled/react/Media'
import BarTheme from 'ducks/bar/BarTheme'
import { getAccountInstitutionLabel } from 'ducks/account/helpers'
import { GROUP_DOCTYPE, accountsConn } from 'doctypes'
import { getGroupLabel, renamedGroup } from 'ducks/groups/helpers'
import Loading from 'components/Loading'
import BackButton from 'components/BackButton'
import Table from 'components/Table'
import { PageTitle } from 'components/Title'
import { Padded } from 'components/Spacing'
import { logException } from 'lib/sentry'
import { useI18n } from 'cozy-ui/transpiled/react/I18n'
import styles from 'ducks/settings/GroupsSettings.styl'
const makeNewGroup = (client, t) => {
const obj = client.makeNewDocument('io.cozy.bank.groups')
obj.label = t('Groups.new-group')
return obj
}
export const AccountLine = props => {
const { account, group, toggleAccount } = props
return (
<tr>
<td className={styles.GrpStg__accntLabel}>
{account.shortLabel || account.label}
</td>
<td className={styles.GrpStg__accntBank}>
{getAccountInstitutionLabel(account)}
</td>
<td className={styles.GrpStg__accntNumber}>{account.number}</td>
<td className={styles.GrpStg__accntToggle}>
{group ? (
<Toggle
id={account._id}
checked={group.accounts.existsById(account._id)}
onToggle={toggleAccount.bind(null, account._id, group)}
/>
) : (
<Toggle id={account._id} disabled />
)}
</td>
</tr>
)
}
const AccountsList = props => {
const { t } = useI18n()
const { accounts, group, toggleAccount } = props
return (
<Table className={styles.GrpStg__table}>
<thead>
<tr>
<th className={styles.GrpStg__accntLabel}>{t('Groups.label')}</th>
<th className={styles.GrpStg__accntBank}>{t('Groups.bank')}</th>
<th className={styles.GrpStg__accntNumber}>
{t('Groups.account-number')}
</th>
<th className={styles.GrpStg__accntToggle}>{t('Groups.included')}</th>
</tr>
</thead>
<tbody>
{accounts &&
sortBy(accounts, ['institutionLabel', 'label']).map(account => (
<AccountLine
account={account}
group={group}
toggleAccount={toggleAccount}
key={account._id}
/>
))}
</tbody>
</Table>
)
}
const updateOrCreateGroup = async (client, group, router, successCallback) => {
const isNew = !group.id
try {
const response = await client.save(group)
if (response && response.data) {
const doc = response.data
if (isNew) {
router.push(`/settings/groups/${doc.id}`)
}
}
} finally {
successCallback && successCallback()
}
}
export class DumbGroupSettings extends Component {
state = { modifying: false, saving: false }
handleRename = () => {
const label = this.inputRef.value
this.rename(label)
}
rename(label) {
this.setState({ saving: true })
const { group } = this.props
const updatedGroup = renamedGroup(group, label)
return this.updateOrCreate(updatedGroup, () => {
this.setState({ saving: false, modifying: false })
})
}
async updateOrCreate(group, cb) {
const { router, client } = this.props
await updateOrCreateGroup(client, group, router, cb)
}
toggleAccount = async (accountId, group, enabled) => {
const accounts = group.accounts
if (enabled) {
accounts.addById(accountId)
} else {
accounts.removeById(accountId)
}
this.updateOrCreate(group)
}
onRemove = async () => {
const { group, router, client, t } = this.props
try {
await client.destroy(group)
router.push('/settings/groups')
} catch (err) {
logException(err)
Alerter.error(t('Groups.deletion_error'))
}
}
modifyName = () => {
this.setState({ modifying: true })
}
saveInputRef = ref => {
this.inputRef = ref
}
render() {
const {
t,
group,
breakpoints: { isMobile }
} = this.props
const { modifying, saving } = this.state
// When deleting the group, there's a re-render between the deletion and the redirection. So we need to handle this case
if (!group) {
return null
}
return (
<Padded>
{isMobile && <BackButton to="/settings/groups" arrow />}
<PageTitle>
{!isMobile && <BackButton to="/settings/groups" arrow />}
{getGroupLabel(group, t)}
</PageTitle>
<h3>{t('Groups.label')}</h3>
<form
className={styles.GrpStg__form}
onSubmit={e => e.preventDefault()}
>
<Media>
<Img>
{!modifying ? (
getGroupLabel(group, t)
) : (
<input
ref={this.saveInputRef}
autoFocus
type="text"
defaultValue={getGroupLabel(group, t)}
/>
)}
</Img>
<Img>
{modifying ? (
<Button
className={styles['save-button']}
disabled={saving}
theme="regular"
onClick={this.handleRename}
label={t('Groups.save')}
busy={saving}
/>
) : (
<Button
theme="text"
onClick={this.modifyName}
label={t('Groups.rename')}
/>
)}
</Img>
</Media>
</form>
<h3>{t('Groups.accounts')}</h3>
<Query query={accountsConn.query} as={accountsConn.as}>
{({ data: accounts, fetchStatus }) => {
if (fetchStatus === 'loading') {
return <Loading />
}
return (
<AccountsList
accounts={accounts}
group={group}
toggleAccount={this.toggleAccount}
/>
)
}}
</Query>
<p>
<Button
theme="danger-outline"
onClick={this.onRemove}
label={t('Groups.delete')}
/>
</p>
</Padded>
)
}
}
export const GroupSettings = compose(
translate(),
withBreakpoints()
)(DumbGroupSettings)
const enhance = Component =>
compose(
withRouter,
withClient
)(Component)
const ExistingGroupSettings = enhance(props => (
<Query query={client => client.get(GROUP_DOCTYPE, props.routeParams.groupId)}>
{({ data: group, fetchStatus }) =>
fetchStatus === 'loading' || fetchStatus === 'pending' ? (
<>
<BarTheme theme="primary" />
<Loading />
</>
) : (
<>
<BarTheme theme="primary" />
<GroupSettings group={group} {...props} />
</>
)
}
</Query>
))
export default ExistingGroupSettings
/**
* We create NewGroupSettings else react-router will reuse
* the existing <GroupSettings /> when a new account is created and we navigate
* to the new group settings. We could do something in componentDidUpdate
* to refetch the group but it seems easier to do that to force the usage
* of a brand new component
*/
export const NewGroupSettings = enhance(
translate()(props => (
<>
<BarTheme theme="primary" />
<GroupSettings {...props} group={makeNewGroup(props.client, props.t)} />
</>
))
)