|
| 1 | +"use client" |
| 2 | + |
| 3 | +import * as React from "react" |
| 4 | +import { useTranslation } from "react-i18next" |
| 5 | +import { Badge } from "@/components/ui/badge" |
| 6 | +import { Button } from "@/components/ui/button" |
| 7 | +import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog" |
| 8 | +import { Spinner } from "@/components/ui/spinner" |
| 9 | +import { useGroups } from "@/hooks/use-groups" |
| 10 | +import { useMessage } from "@/lib/feedback/message" |
| 11 | +import { buildAddUsersToGroupsRequests } from "@/lib/user-group-memberships" |
| 12 | +import { UserEditGroups } from "./edit/groups" |
| 13 | + |
| 14 | +interface UserAddToGroupFormProps { |
| 15 | + open: boolean |
| 16 | + onOpenChange: (open: boolean) => void |
| 17 | + selectedUsers: string[] |
| 18 | + onSuccess: () => void |
| 19 | +} |
| 20 | + |
| 21 | +export function UserAddToGroupForm({ open, onOpenChange, selectedUsers, onSuccess }: UserAddToGroupFormProps) { |
| 22 | + const { t } = useTranslation() |
| 23 | + const message = useMessage() |
| 24 | + const { listGroup, updateGroupMembers } = useGroups() |
| 25 | + const [groups, setGroups] = React.useState<string[]>([]) |
| 26 | + const [groupsList, setGroupsList] = React.useState<{ label: string; value: string }[]>([]) |
| 27 | + const [loading, setLoading] = React.useState(false) |
| 28 | + const [submitting, setSubmitting] = React.useState(false) |
| 29 | + |
| 30 | + React.useEffect(() => { |
| 31 | + if (!open) return |
| 32 | + |
| 33 | + setGroups([]) |
| 34 | + setLoading(true) |
| 35 | + listGroup() |
| 36 | + .then((res) => { |
| 37 | + setGroupsList((res as string[] | undefined)?.map((group) => ({ label: group, value: group })) ?? []) |
| 38 | + }) |
| 39 | + .catch(() => { |
| 40 | + message.error(t("Failed to get data")) |
| 41 | + }) |
| 42 | + .finally(() => setLoading(false)) |
| 43 | + }, [listGroup, message, open, t]) |
| 44 | + |
| 45 | + const closeModal = () => onOpenChange(false) |
| 46 | + |
| 47 | + const submitForm = async () => { |
| 48 | + const requests = buildAddUsersToGroupsRequests(selectedUsers, groups) |
| 49 | + if (!requests.length) { |
| 50 | + message.error(t("Please select at least one item")) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + setSubmitting(true) |
| 55 | + try { |
| 56 | + await Promise.all(requests.map((request) => updateGroupMembers(request))) |
| 57 | + message.success(t("Edit Success")) |
| 58 | + onOpenChange(false) |
| 59 | + onSuccess() |
| 60 | + } catch (error) { |
| 61 | + console.error(error) |
| 62 | + message.error((error as Error)?.message || t("Edit Failed")) |
| 63 | + } finally { |
| 64 | + setSubmitting(false) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return ( |
| 69 | + <Dialog open={open} onOpenChange={closeModal}> |
| 70 | + <DialogContent className="sm:max-w-lg" onPointerDownOutside={(e) => e.preventDefault()}> |
| 71 | + <DialogHeader> |
| 72 | + <DialogTitle>{t("Add to Group")}</DialogTitle> |
| 73 | + </DialogHeader> |
| 74 | + |
| 75 | + <div className="space-y-4"> |
| 76 | + <div className="space-y-2"> |
| 77 | + <div className="text-sm font-medium">{t("Users")}</div> |
| 78 | + <div className="flex flex-wrap gap-2"> |
| 79 | + {selectedUsers.map((user) => ( |
| 80 | + <Badge key={user} variant="secondary"> |
| 81 | + {user} |
| 82 | + </Badge> |
| 83 | + ))} |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + <UserEditGroups value={groups} options={groupsList} disabled={loading || submitting} onChange={setGroups} /> |
| 87 | + </div> |
| 88 | + |
| 89 | + <DialogFooter> |
| 90 | + <Button variant="outline" onClick={closeModal} disabled={submitting}> |
| 91 | + {t("Cancel")} |
| 92 | + </Button> |
| 93 | + <Button onClick={submitForm} disabled={loading || submitting || !selectedUsers.length}> |
| 94 | + {submitting ? <Spinner className="size-4" /> : null} |
| 95 | + <span>{t("Submit")}</span> |
| 96 | + </Button> |
| 97 | + </DialogFooter> |
| 98 | + </DialogContent> |
| 99 | + </Dialog> |
| 100 | + ) |
| 101 | +} |
0 commit comments