-
Notifications
You must be signed in to change notification settings - Fork 5
/
UsersCell.tsx
92 lines (79 loc) · 2.05 KB
/
UsersCell.tsx
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
import { useState } from 'react'
import type { FindUsersByOrganizationId } from 'types/graphql'
import { Link, routes } from '@redwoodjs/router'
import type { CellSuccessProps, CellFailureProps } from '@redwoodjs/web'
import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'
import Users from 'src/components/User/Users'
import { UPDATE_USER_MUTATION } from '../EditUserCell'
export const QUERY = gql`
query FindUsersByOrganizationId($organizationId: Int!) {
usersByOrganization(organizationId: $organizationId) {
id
email
name
agency {
id
name
}
role
isActive
createdAt
updatedAt
}
}
`
export const Loading = () => <div>Loading...</div>
export const Empty = () => {
return (
<div className="rw-text-center">
{'No users yet. '}
<Link to={routes.newUser()} className="rw-link">
{'Create one?'}
</Link>
</div>
)
}
export const Failure = ({ error }: CellFailureProps) => (
<div className="rw-cell-error">{error?.message}</div>
)
export const Success = ({
usersByOrganization,
queryResult: { refetch },
}: CellSuccessProps<FindUsersByOrganizationId>) => {
const [usersUpdating, setUsersUpdating] = useState(new Set())
const [updateUserMutation] = useMutation(UPDATE_USER_MUTATION, {
onCompleted: () => {
toast.success('User updated')
},
onError: (error) => {
toast.error(error.message)
},
})
const updateUser = async (user) => {
const { email, name, role, isActive } = user
setUsersUpdating(usersUpdating.add(user.id))
await updateUserMutation({
variables: {
id: user.id,
input: {
name,
email,
role,
agencyId: user.agency.id,
isActive,
},
},
})
usersUpdating.delete(user.id)
setUsersUpdating(new Set(usersUpdating))
refetch()
}
return (
<Users
usersByOrganization={usersByOrganization}
updateUser={updateUser}
usersUpdating={usersUpdating}
/>
)
}