-
Notifications
You must be signed in to change notification settings - Fork 331
/
ReviewRequestToJoinOrgModal.tsx
170 lines (150 loc) · 5.23 KB
/
ReviewRequestToJoinOrgModal.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
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
import graphql from 'babel-plugin-relay/macro'
import React, {useState, useMemo} from 'react'
import {PreloadedQuery, usePreloadedQuery, useFragment} from 'react-relay'
import DialogContainer from './DialogContainer'
import DialogTitle from './DialogTitle'
import PrimaryButton from './PrimaryButton'
import SecondaryButton from './SecondaryButton'
import {ReviewRequestToJoinOrgModalQuery} from '../__generated__/ReviewRequestToJoinOrgModalQuery.graphql'
import {ReviewRequestToJoinOrgModal_viewer$key} from '../__generated__/ReviewRequestToJoinOrgModal_viewer.graphql'
import Checkbox from './Checkbox'
import useAcceptRequestToJoinDomainMutation from '../mutations/useAcceptRequestToJoinDomainMutation'
const ReviewRequestToJoinOrgModalViewerFragment = graphql`
fragment ReviewRequestToJoinOrgModal_viewer on User
@argumentDefinitions(requestId: {type: "ID!"}) {
domainJoinRequest(requestId: $requestId) {
id
createdByEmail
createdBy
domain
teams {
id
name
isLead
teamMembers(sortBy: "preferredName") {
userId
}
organization {
name
activeDomain
}
...DashboardAvatars_team
}
}
}
`
const query = graphql`
query ReviewRequestToJoinOrgModalQuery($requestId: ID!) {
viewer {
...ReviewRequestToJoinOrgModal_viewer @arguments(requestId: $requestId)
}
}
`
interface Props {
closePortal: () => void
queryRef: PreloadedQuery<ReviewRequestToJoinOrgModalQuery>
}
// TODO: Create generic dialog components using tailwind https://github.com/ParabolInc/parabol/issues/8107
const ReviewRequestToJoinOrgModal = (props: Props) => {
const {closePortal, queryRef} = props
const [selectedTeamsIds, setSelectedTeamsIds] = useState<string[]>([])
const data = usePreloadedQuery<ReviewRequestToJoinOrgModalQuery>(query, queryRef)
const viewer = useFragment<ReviewRequestToJoinOrgModal_viewer$key>(
ReviewRequestToJoinOrgModalViewerFragment,
data.viewer
)
const {domainJoinRequest} = viewer
const teams = domainJoinRequest?.teams
const [commit, submitting] = useAcceptRequestToJoinDomainMutation()
const sortedTeams = useMemo(() => {
if (!teams) {
return []
}
return teams?.slice().sort((a, b) => a.organization.name.localeCompare(b.organization.name))
}, [teams])
if (!domainJoinRequest) {
return (
<DialogContainer>
<DialogTitle>{'Add teammate'}</DialogTitle>
<div className={'overflow-y-scroll p-6 text-sm leading-relaxed text-slate-700'}>
Request expired or deleted
</div>
<div className={'flex w-full justify-end px-6 pb-6'}>
<div className={'mr-2'}>
<SecondaryButton onClick={closePortal} size='small'>
Cancel
</SecondaryButton>
</div>
</div>
</DialogContainer>
)
}
const {createdBy, createdByEmail, id: requestId} = domainJoinRequest
const onAdd = () => {
commit(
{
variables: {
requestId,
teamIds: selectedTeamsIds
}
},
{
onSuccess: closePortal
}
)
}
return (
<DialogContainer>
<DialogTitle>{'Add teammate'}</DialogTitle>
<div className={'py-4 pl-6 text-base'}>
Which teams would you like to add <strong>{createdByEmail}</strong> to?
</div>
<div className={'overflow-y-scroll px-6 pb-6 text-sm leading-relaxed text-slate-700'}>
{sortedTeams.map((team) => {
const {id: teamId, name: teamName, organization, teamMembers} = team
const {name: orgName} = organization
// TODO: implement userId filter for teamMembers on API side
const isAlreadyMember = teamMembers.some((member) => member.userId === createdBy)
const active = selectedTeamsIds.includes(teamId) || isAlreadyMember
const handleClick = () => {
if (isAlreadyMember) return
if (active) {
setSelectedTeamsIds((prevSelectedTeamsIds) =>
prevSelectedTeamsIds.filter((id) => id !== teamId)
)
} else {
setSelectedTeamsIds((prevSelectedTeamsIds) => [...prevSelectedTeamsIds, teamId])
}
}
return (
<div className='mb-2 flex items-center text-base' key={teamId} onClick={handleClick}>
<Checkbox
active={active}
disabled={isAlreadyMember}
className={active && !isAlreadyMember ? `text-sky-500` : undefined}
/>
<label
className={`ml-2 ${
isAlreadyMember ? 'cursor-not-allowed opacity-[.38]' : 'cursor-pointer'
}`}
>
{teamName} | {orgName}
</label>
</div>
)
})}
</div>
<div className={'flex w-full justify-end p-4'}>
<div className={'mr-2'}>
<SecondaryButton onClick={closePortal} size='small' disabled={submitting}>
Cancel
</SecondaryButton>
</div>
<PrimaryButton size='small' onClick={onAdd} disabled={submitting}>
Add to teams
</PrimaryButton>
</div>
</DialogContainer>
)
}
export default ReviewRequestToJoinOrgModal