| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { FolderPlus, Trash2 |
| 5 | import { useAction } |
| 6 | import { toast } |
| 7 | import { Avatar, AvatarFallback |
| 8 | import { Badge } |
| 9 | import { Button } |
| 10 | import { |
| 11 | Table, |
| 12 | TableBody, |
| 13 | TableCell, |
| 14 | TableHead, |
| 15 | TableHeader, |
| 16 | TableRow, |
| 17 | } from '@/components/ui/table' |
| 18 | import { |
| 19 | removeClientFromOrgAction, |
| 20 | removeClientFromProjectAction, |
| 21 | } from '../actions' |
| 22 | import type { OrgClient |
| 23 | |
| 24 | export default function ClientsTable |
| 25 | clients, |
| 26 | canManage, |
| 27 | onAssignProject, |
| 28 | }: { |
| 29 | clients: OrgClient |
| 30 | canManage: boolean |
| 31 | orgSlug: string |
| 32 | onAssignProject: ( |
| 33 | memberId: string |
| 34 | clientName: string |
| 35 | existingProjectIds: string |
| 36 | ) => |
| 37 | }) { |
| 38 | const router = |
| 39 | |
| 40 | const { execute: |
| 41 | removeClientFromProjectAction, |
| 42 | { |
| 43 | onSuccess() { |
| 44 | toast.success('Client removed from project |
| 45 | router.refresh() |
| 46 | }, |
| 47 | onError({ error |
| 48 | toast.error(error |
| 49 | }, |
| 50 | } |
| 51 | ) |
| 52 | |
| 53 | const { execute: |
| 54 | removeClientFromOrgAction, |
| 55 | { |
| 56 | onSuccess() { |
| 57 | toast.success('Client removed from workspace |
| 58 | router.refresh() |
| 59 | }, |
| 60 | onError({ error |
| 61 | toast.error(error |
| 62 | }, |
| 63 | } |
| 64 | ) |
| 65 | |
| 66 | if (clients.length |
| 67 | return null |
| 68 | } |
| 69 | |
| 70 | return ( |
| 71 | <Table> |
| 72 | <TableHeader> |
| 73 | <TableRow> |
| 74 | <TableHead>Client</TableHead |
| 75 | <TableHead>Projects</TableHead |
| 76 | <TableHead>Added</TableHead |
| 77 | {canManage && <TableHead |
| 78 | </TableRow> |
| 79 | </TableHeader> |
| 80 | <TableBody> |
| 81 | {clients.map(( |
| 82 | <TableRow key={ |
| 83 | <TableCell> |
| 84 | <div className='flex items-center gap-3 |
| 85 | <Avatar className=' |
| 86 | <AvatarImage |
| 87 | alt={client.userName |
| 88 | src={client.userImage |
| 89 | /> |
| 90 | <AvatarFallback> |
| 91 | {client.userName.substring |
| 92 | </AvatarFallback> |
| 93 | </Avatar> |
| 94 | <div> |
| 95 | <div className='font-medium text-sm |
| 96 | <div className='text-muted-foreground text-xs |
| 97 | {client.userEmail} |
| 98 | </div> |
| 99 | </div> |
| 100 | </div> |
| 101 | </TableCell> |
| 102 | <TableCell> |
| 103 | {client.projects.length |
| 104 | <span className='text-muted-foreground text-sm |
| 105 | No projects |
| 106 | </span> |
| 107 | ) : ( |
| 108 | <div className='flex flex-wrap gap-1.5 |
| 109 | {client.projects.map |
| 110 | <Badge |
| 111 | className='gap-1 pr-1 text-xs' |
| 112 | key={p.assignmentId |
| 113 | variant='secondary' |
| 114 | > |
| 115 | {p.projectName} |
| 116 | {canManage && ( |
| 117 | <button |
| 118 | className='ml-0.5 text-muted-foreground hover:text-foreground' |
| 119 | onClick={() => |
| 120 | executeRemoveFromProject({ |
| 121 | assignmentId: p.assignmentId |
| 122 | }) |
| 123 | } |
| 124 | type='button' |
| 125 | > |
| 126 | <X className=' |
| 127 | </button> |
| 128 | )} |
| 129 | </Badge> |
| 130 | ))} |
| 131 | </div> |
| 132 | )} |
| 133 | </TableCell> |
| 134 | <TableCell> |
| 135 | <span className='text-muted-foreground text-sm |
| 136 | {new Date(client |
| 137 | month: 'short', |
| 138 | day: 'numeric', |
| 139 | year: 'numeric', |
| 140 | })} |
| 141 | </span> |
| 142 | </TableCell> |
| 143 | {canManage && ( |
| 144 | <TableCell> |
| 145 | <div className='flex items-center gap-1 |
| 146 | <Button |
| 147 | onClick={() => |
| 148 | onAssignProject( |
| 149 | client.memberId, |
| 150 | client.userName, |
| 151 | client.projects.map |
| 152 | ) |
| 153 | } |
| 154 | title='Assign to project' |
| 155 | variant='ghost' |
| 156 | > |
| 157 | <FolderPlus className=' |
| 158 | </Button> |
| 159 | <Button |
| 160 | onClick={() => |
| 161 | executeRemoveFromOrg({ memberId: |
| 162 | } |
| 163 | title='Remove from workspace' |
| 164 | variant='ghost' |
| 165 | > |
| 166 | <Trash2 className=' |
| 167 | </Button> |
| 168 | </div> |
| 169 | </TableCell> |
| 170 | )} |
| 171 | </TableRow> |
| 172 | ))} |
| 173 | </TableBody> |
| 174 | </Table> |
| 175 | ) |
| 176 | } |