| 1 | import { useRouter } from '@bprogress/next/app' |
| 2 | import { useAction } |
| 3 | import { useState } |
| 4 | import { toast } |
| 5 | import { Button } |
| 6 | import { |
| 7 | Dialog, |
| 8 | DialogContent, |
| 9 | DialogHeader, |
| 10 | DialogTitle, |
| 11 | } from '@/components/ui/dialog' |
| 12 | import { |
| 13 | Select, |
| 14 | SelectContent, |
| 15 | SelectItem, |
| 16 | SelectTrigger, |
| 17 | SelectValue, |
| 18 | } from '@/components/ui/select' |
| 19 | import { assignClientToProjectAction } |
| 20 | import type { OrgProject |
| 21 | |
| 22 | export default function AssignProjectDialog |
| 23 | open, |
| 24 | onOpenChange, |
| 25 | memberId, |
| 26 | projects, |
| 27 | existingProjectIds, |
| 28 | }: { |
| 29 | open: boolean |
| 30 | onOpenChange: (open |
| 31 | memberId: string |
| 32 | projects: OrgProject |
| 33 | existingProjectIds: string |
| 34 | }) { |
| 35 | const router = |
| 36 | const [selectedProjectId, |
| 37 | |
| 38 | const availableProjects = |
| 39 | (p) => |
| 40 | ) |
| 41 | |
| 42 | const { execute, |
| 43 | onSuccess() { |
| 44 | toast.success('Client assigned to project |
| 45 | setSelectedProjectId('') |
| 46 | onOpenChange(false) |
| 47 | router.refresh() |
| 48 | }, |
| 49 | onError({ error |
| 50 | toast.error(error |
| 51 | }, |
| 52 | }) |
| 53 | |
| 54 | return ( |
| 55 | <Dialog onOpenChange={ |
| 56 | <DialogContent className=' |
| 57 | <DialogHeader> |
| 58 | <DialogTitle>Assign to Project</DialogTitle |
| 59 | </DialogHeader> |
| 60 | <div className='space-y-4 |
| 61 | {availableProjects.length === |
| 62 | <p className='text-muted-foreground text-sm |
| 63 | This client is already assigned to all projects. |
| 64 | </p> |
| 65 | ) : ( |
| 66 | <Select |
| 67 | onValueChange={setSelectedProjectId} |
| 68 | value={selectedProjectId} |
| 69 | > |
| 70 | <SelectTrigger> |
| 71 | <SelectValue placeholder=' |
| 72 | </SelectTrigger> |
| 73 | <SelectContent> |
| 74 | {availableProjects.map(( |
| 75 | <SelectItem key={ |
| 76 | {p.name} |
| 77 | </SelectItem> |
| 78 | ))} |
| 79 | </SelectContent> |
| 80 | </Select> |
| 81 | )} |
| 82 | <div className='flex justify-end gap-2 |
| 83 | <Button onClick={ |
| 84 | Cancel |
| 85 | </Button> |
| 86 | <Button |
| 87 | disabled={!selectedProjectId} |
| 88 | loading={isPending} |
| 89 | onClick={() => |
| 90 | execute({ memberId, |
| 91 | } |
| 92 | > |
| 93 | Assign |
| 94 | </Button> |
| 95 | </div> |
| 96 | </div> |
| 97 | </DialogContent> |
| 98 | </Dialog> |
| 99 | ) |
| 100 | } |