@@ -14,9 +14,11 @@ import Select from '../components/Select'
1414import Button from '../components/Button'
1515import List from '../components/List'
1616import { NotificationContext } from '../components/Notifications'
17+ import Modal from '../components/Modal'
1718
1819const Account = ( { token, invites = [ ] , user, userRole } ) => {
1920 const [ invitesList , setInvitesList ] = useState ( invites )
21+ const [ showInviteModal , setShowInviteModal ] = useState ( false )
2022
2123 const { addNotification } = useContext ( NotificationContext )
2224
@@ -29,16 +31,17 @@ const Account = ({ token, invites = [], user, userRole }) => {
2931 const form = new FormData ( e . target )
3032
3133 try {
32- const inviteRes = await fetch (
33- `${ SQ_API_URL } /account/generate-invite?role=${
34- form . get ( 'role' ) || 'user'
35- } `,
36- {
37- headers : {
38- Authorization : `Bearer ${ token } ` ,
39- } ,
40- }
41- )
34+ const inviteRes = await fetch ( `${ SQ_API_URL } /account/generate-invite` , {
35+ method : 'POST' ,
36+ headers : {
37+ 'Content-Type' : 'application/json' ,
38+ Authorization : `Bearer ${ token } ` ,
39+ } ,
40+ body : JSON . stringify ( {
41+ role : form . get ( 'role' ) || 'user' ,
42+ email : form . get ( 'email' ) ,
43+ } ) ,
44+ } )
4245
4346 if ( inviteRes . status !== 200 ) {
4447 const reason = await inviteRes . text ( )
@@ -52,9 +55,11 @@ const Account = ({ token, invites = [], user, userRole }) => {
5255 return currentInvitesList
5356 } )
5457
55- addNotification ( 'success' , 'Invite generated successfully' )
58+ addNotification ( 'success' , 'Invite sent successfully' )
59+
60+ setShowInviteModal ( false )
5661 } catch ( e ) {
57- addNotification ( 'error' , `Could not generate invite: ${ e . message } ` )
62+ addNotification ( 'error' , `Could not send invite: ${ e . message } ` )
5863 console . error ( e )
5964 }
6065 }
@@ -109,43 +114,42 @@ const Account = ({ token, invites = [], user, userRole }) => {
109114 mb = { 4 }
110115 >
111116 < Text as = "h2" > Invites</ Text >
112- < form onSubmit = { handleGenerateInvite } >
113- < Box
114- display = "flex"
115- alignItems = "center"
116- border = "1px solid"
117- borderColor = "border"
118- borderRadius = { 1 }
119- p = { 2 }
120- pl = { 4 }
117+ < Box
118+ display = "flex"
119+ alignItems = "center"
120+ border = "1px solid"
121+ borderColor = "border"
122+ borderRadius = { 1 }
123+ p = { 2 }
124+ pl = { 4 }
125+ >
126+ < Text color = "grey" mr = { 4 } >
127+ { user . remainingInvites || 0 } remaining
128+ </ Text >
129+ < Button
130+ onClick = { ( ) => setShowInviteModal ( true ) }
131+ disabled = { ( user . remainingInvites || 0 ) < 1 }
121132 >
122- < Text color = "grey" mr = { 4 } >
123- { user . remainingInvites || 0 } remaining
124- </ Text >
125- { userRole === 'admin' && (
126- < Select name = "role" required mr = { 3 } >
127- < option value = "user" > Role: user</ option >
128- < option value = "admin" > Role: admin</ option >
129- </ Select >
130- ) }
131- < Button disabled = { ( user . remainingInvites || 0 ) < 1 } >
132- Generate invite
133- </ Button >
134- </ Box >
135- </ form >
133+ Send invite
134+ </ Button >
135+ </ Box >
136136 </ Box >
137137 < List
138138 data = { invitesList }
139139 columns = { [
140140 {
141- header : 'Token' ,
142- accessor : 'token' ,
143- cell : ( { value } ) => (
144- < Text fontFamily = "monospace" >
145- ...{ value . slice ( value . length - 16 ) }
141+ header : 'Email' ,
142+ accessor : 'email' ,
143+ cell : ( { value, row } ) => (
144+ < Text
145+ _css = { {
146+ textDecoration : row . claimed ? 'line-through' : 'none' ,
147+ } }
148+ >
149+ { value }
146150 </ Text >
147151 ) ,
148- gridWidth : '1fr ' ,
152+ gridWidth : '1.75fr ' ,
149153 } ,
150154 {
151155 header : 'Claimed' ,
@@ -158,7 +162,7 @@ const Account = ({ token, invites = [], user, userRole }) => {
158162 accessor : 'validUntil' ,
159163 cell : ( { value } ) => (
160164 < Text
161- style = { {
165+ _css = { {
162166 textDecoration : value < Date . now ( ) ? 'line-through' : 'none' ,
163167 } }
164168 >
@@ -173,7 +177,7 @@ const Account = ({ token, invites = [], user, userRole }) => {
173177 cell : ( { value } ) => (
174178 < Text > { moment ( value ) . format ( 'HH:mm Do MMM YYYY' ) } </ Text >
175179 ) ,
176- gridWidth : '1fr ' ,
180+ gridWidth : '1.2fr ' ,
177181 } ,
178182 {
179183 header : 'Role' ,
@@ -184,7 +188,7 @@ const Account = ({ token, invites = [], user, userRole }) => {
184188 gridWidth : '0.6fr' ,
185189 } ,
186190 {
187- header : 'Copy' ,
191+ header : 'Copy link ' ,
188192 cell : ( { row } ) => {
189193 return (
190194 < Button
@@ -207,7 +211,7 @@ const Account = ({ token, invites = [], user, userRole }) => {
207211 )
208212 } ,
209213 rightAlign : true ,
210- gridWidth : '42px ' ,
214+ gridWidth : '80px ' ,
211215 } ,
212216 ] }
213217 mb = { 5 }
@@ -233,6 +237,34 @@ const Account = ({ token, invites = [], user, userRole }) => {
233237 />
234238 < Button > Change password</ Button >
235239 </ form >
240+ { showInviteModal && (
241+ < Modal close = { ( ) => setShowInviteModal ( false ) } >
242+ < Text mb = { 5 } >
243+ Enter an email address to send an invite. The invited user will need
244+ to sign up with the same email address. Once the invite is
245+ generated, you can also copy a direct invite link.
246+ </ Text >
247+ < form onSubmit = { handleGenerateInvite } >
248+ < Input name = "email" type = "email" label = "Email" mb = { 4 } required />
249+ { userRole === 'admin' && (
250+ < Select name = "role" mb = { 4 } required >
251+ < option value = "user" > Role: user</ option >
252+ < option value = "admin" > Role: admin</ option >
253+ </ Select >
254+ ) }
255+ < Box display = "flex" justifyContent = "flex-end" >
256+ < Button
257+ onClick = { ( ) => setShowInviteModal ( false ) }
258+ variant = "secondary"
259+ mr = { 3 }
260+ >
261+ Cancel
262+ </ Button >
263+ < Button > Send invite</ Button >
264+ </ Box >
265+ </ form >
266+ </ Modal >
267+ ) }
236268 </ >
237269 )
238270}
0 commit comments