|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Button } from "@/components/ui/button"; |
| 4 | +import { |
| 5 | + Dialog, |
| 6 | + DialogContent, |
| 7 | + DialogDescription, |
| 8 | + DialogFooter, |
| 9 | + DialogHeader, |
| 10 | + DialogTitle, |
| 11 | +} from "@/components/ui/dialog"; |
| 12 | +import { Label } from "@/components/ui/label"; |
| 13 | +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; |
| 14 | +import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command"; |
| 15 | +import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; |
| 16 | +import { Check, ChevronsUpDown } from "lucide-react"; |
| 17 | +import { useState } from "react"; |
| 18 | +import { toast } from "sonner"; |
| 19 | +import { Alert } from "@/components/ui/alert"; |
| 20 | +import { useAddUserToOrganization } from "@/api/admin/organizations"; |
| 21 | +import { useAdminOrganizations } from "@/api/admin/getAdminOrganizations"; |
| 22 | +import { cn } from "@/lib/utils"; |
| 23 | + |
| 24 | +interface AddToOrganizationDialogProps { |
| 25 | + userEmail: string; |
| 26 | + userId: string; |
| 27 | + open: boolean; |
| 28 | + onOpenChange: (open: boolean) => void; |
| 29 | +} |
| 30 | + |
| 31 | +export function AddToOrganizationDialog({ userEmail, userId, open, onOpenChange }: AddToOrganizationDialogProps) { |
| 32 | + const [organizationId, setOrganizationId] = useState<string>(""); |
| 33 | + const [role, setRole] = useState<"admin" | "member" | "owner">("member"); |
| 34 | + const [error, setError] = useState(""); |
| 35 | + const [comboboxOpen, setComboboxOpen] = useState(false); |
| 36 | + |
| 37 | + const { data: organizations, isLoading: isLoadingOrgs } = useAdminOrganizations(); |
| 38 | + const addUserToOrganization = useAddUserToOrganization(); |
| 39 | + |
| 40 | + const resetState = (open: boolean) => { |
| 41 | + onOpenChange(open); |
| 42 | + if (!open) { |
| 43 | + setError(""); |
| 44 | + setOrganizationId(""); |
| 45 | + setRole("member"); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + const handleAdd = async () => { |
| 50 | + if (!organizationId) { |
| 51 | + setError("Please select an organization"); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + await addUserToOrganization.mutateAsync({ |
| 57 | + email: userEmail, |
| 58 | + role, |
| 59 | + organizationId, |
| 60 | + }); |
| 61 | + |
| 62 | + toast.success("User added to organization successfully"); |
| 63 | + resetState(false); |
| 64 | + } catch (error: any) { |
| 65 | + setError(error.message || "Failed to add user to organization"); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + return ( |
| 70 | + <Dialog open={open} onOpenChange={resetState}> |
| 71 | + <DialogContent className="max-w-lg"> |
| 72 | + <DialogHeader> |
| 73 | + <DialogTitle>Add user to organization</DialogTitle> |
| 74 | + <DialogDescription> |
| 75 | + Add {userEmail} to an organization with a specific role. |
| 76 | + </DialogDescription> |
| 77 | + </DialogHeader> |
| 78 | + <div className="grid gap-4 py-4"> |
| 79 | + <div className="grid gap-2"> |
| 80 | + <Label htmlFor="organization">Organization</Label> |
| 81 | + <Popover open={comboboxOpen} onOpenChange={setComboboxOpen}> |
| 82 | + <PopoverTrigger asChild> |
| 83 | + <Button |
| 84 | + variant="outline" |
| 85 | + role="combobox" |
| 86 | + aria-expanded={comboboxOpen} |
| 87 | + className="w-full justify-between" |
| 88 | + disabled={isLoadingOrgs} |
| 89 | + > |
| 90 | + {organizationId |
| 91 | + ? organizations?.find(org => org.id === organizationId)?.name |
| 92 | + : isLoadingOrgs |
| 93 | + ? "Loading..." |
| 94 | + : "Select an organization..."} |
| 95 | + <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" /> |
| 96 | + </Button> |
| 97 | + </PopoverTrigger> |
| 98 | + <PopoverContent className="w-full p-0" align="start"> |
| 99 | + <Command |
| 100 | + filter={(value, search) => { |
| 101 | + if (value.toLowerCase().includes(search.toLowerCase())) return 1; |
| 102 | + return 0; |
| 103 | + }} |
| 104 | + > |
| 105 | + <CommandInput placeholder="Search organizations..." /> |
| 106 | + <CommandList> |
| 107 | + <CommandEmpty>No organization found.</CommandEmpty> |
| 108 | + <CommandGroup> |
| 109 | + {organizations?.map(org => ( |
| 110 | + <CommandItem |
| 111 | + key={org.id} |
| 112 | + value={`${org.name} ${org.id}`} |
| 113 | + onSelect={() => { |
| 114 | + setOrganizationId(org.id); |
| 115 | + setComboboxOpen(false); |
| 116 | + }} |
| 117 | + > |
| 118 | + <Check className={cn("mr-2 h-4 w-4", organizationId === org.id ? "opacity-100" : "opacity-0")} /> |
| 119 | + {org.name} |
| 120 | + </CommandItem> |
| 121 | + ))} |
| 122 | + </CommandGroup> |
| 123 | + </CommandList> |
| 124 | + </Command> |
| 125 | + </PopoverContent> |
| 126 | + </Popover> |
| 127 | + </div> |
| 128 | + <div className="grid gap-2"> |
| 129 | + <Label htmlFor="role">Role</Label> |
| 130 | + <Select value={role} onValueChange={value => setRole(value as "admin" | "member" | "owner")}> |
| 131 | + <SelectTrigger id="role"> |
| 132 | + <SelectValue placeholder="Select a role" /> |
| 133 | + </SelectTrigger> |
| 134 | + <SelectContent> |
| 135 | + <SelectItem value="owner">Owner</SelectItem> |
| 136 | + <SelectItem value="admin">Admin</SelectItem> |
| 137 | + <SelectItem value="member">Member</SelectItem> |
| 138 | + </SelectContent> |
| 139 | + </Select> |
| 140 | + </div> |
| 141 | + {error && <Alert variant="destructive">{error}</Alert>} |
| 142 | + </div> |
| 143 | + <DialogFooter> |
| 144 | + <Button variant="outline" onClick={() => resetState(false)}> |
| 145 | + Cancel |
| 146 | + </Button> |
| 147 | + <Button onClick={handleAdd} disabled={addUserToOrganization.isPending} variant="success"> |
| 148 | + {addUserToOrganization.isPending ? "Adding..." : "Add to Organization"} |
| 149 | + </Button> |
| 150 | + </DialogFooter> |
| 151 | + </DialogContent> |
| 152 | + </Dialog> |
| 153 | + ); |
| 154 | +} |
0 commit comments