| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { Mail } |
| 5 | import { useState } |
| 6 | import { toast } |
| 7 | import { Button } |
| 8 | import { |
| 9 | Dialog, |
| 10 | DialogContent, |
| 11 | DialogDescription, |
| 12 | DialogHeader, |
| 13 | DialogTitle, |
| 14 | } from '@/components/ui/dialog' |
| 15 | import { Input } |
| 16 | import { Label } |
| 17 | import { authClient } |
| 18 | import { analyticsService } |
| 19 | |
| 20 | export default function InviteClientDialog |
| 21 | open, |
| 22 | onOpenChange, |
| 23 | organizationId, |
| 24 | }: { |
| 25 | open: boolean |
| 26 | onOpenChange: (open |
| 27 | organizationId: string |
| 28 | }) { |
| 29 | const router = |
| 30 | const [email, |
| 31 | const [isInviting, |
| 32 | |
| 33 | const handleInvite = |
| 34 | if (!email.trim |
| 35 | return |
| 36 | } |
| 37 | setIsInviting(true) |
| 38 | try { |
| 39 | const result = |
| 40 | email: email.trim |
| 41 | role: 'client' as |
| 42 | organizationId, |
| 43 | }) |
| 44 | if (result.error |
| 45 | const msg = |
| 46 | if (msg.toLowerCase |
| 47 | toast.error( |
| 48 | 'This user is already in the workspace. Use "Assign to Project" instead.' |
| 49 | ) |
| 50 | } else { |
| 51 | toast.error(msg |
| 52 | } |
| 53 | } else { |
| 54 | toast.success(`Invitation sent to |
| 55 | setEmail('') |
| 56 | onOpenChange(false) |
| 57 | router.refresh() |
| 58 | analyticsService.track('client_added |
| 59 | } |
| 60 | } catch { |
| 61 | toast.error('Failed to send invitation |
| 62 | } finally { |
| 63 | setIsInviting(false) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return ( |
| 68 | <Dialog onOpenChange={ |
| 69 | <DialogContent className=' |
| 70 | <DialogHeader> |
| 71 | <DialogTitle>Invite Client</DialogTitle |
| 72 | <DialogDescription> |
| 73 | Send an invitation email. They'll join the workspace as a |
| 74 | client once they accept. |
| 75 | </DialogDescription> |
| 76 | </DialogHeader> |
| 77 | <div className='space-y-4 |
| 78 | <div className='space-y-2 |
| 79 | <Label>Email address</Label |
| 80 | <Input |
| 81 | autoFocus |
| 82 | onChange={(e) |
| 83 | onKeyDown={(e) |
| 84 | if (e.key |
| 85 | handleInvite() |
| 86 | } |
| 87 | }} |
| 88 | placeholder='client@example.com' |
| 89 | type='email' |
| 90 | value={email} |
| 91 | /> |
| 92 | </div> |
| 93 | <div className='flex justify-end gap-2 |
| 94 | <Button onClick={ |
| 95 | Cancel |
| 96 | </Button> |
| 97 | <Button |
| 98 | disabled={!email. |
| 99 | loading={isInviting} |
| 100 | onClick={handleInvite} |
| 101 | > |
| 102 | <Mail className=' |
| 103 | Send Invite |
| 104 | </Button> |
| 105 | </div> |
| 106 | </div> |
| 107 | </DialogContent> |
| 108 | </Dialog> |
| 109 | ) |
| 110 | } |