|
| 1 | +/** |
| 2 | + * Modal: Assign one or many Auth0 user ids to an existing tester, or create a new tester and assign. |
| 3 | + */ |
| 4 | +import { useEffect, useState } from "react"; |
| 5 | +import { Modal, ModalBody, ModalContent, ModalHeader } from "@heroui/modal"; |
| 6 | +import { Button } from "@heroui/button"; |
| 7 | +import { Input } from "@heroui/input"; |
| 8 | +import { useTranslation } from "react-i18next"; |
| 9 | +import { addToast } from "@heroui/toast"; |
| 10 | +import { useSecuredApi } from "@/components/auth0"; |
| 11 | + |
| 12 | +export default function AssignTesterModal({ |
| 13 | + isOpen, |
| 14 | + onClose, |
| 15 | + userIds, |
| 16 | + onSuccess, |
| 17 | +}: { |
| 18 | + isOpen: boolean; |
| 19 | + onClose: () => void; |
| 20 | + userIds: string[]; // one or more Auth0 IDs |
| 21 | + onSuccess?: () => void; |
| 22 | +}) { |
| 23 | + const { t } = useTranslation(); |
| 24 | + const { getJson, postJson } = useSecuredApi(); |
| 25 | + const [testers, setTesters] = useState<Array<{ uuid: string; name: string }>>([]); |
| 26 | + const [selectedUuid, setSelectedUuid] = useState<string | null>(null); |
| 27 | + const [createName, setCreateName] = useState(""); |
| 28 | + const [isCreating, setIsCreating] = useState(false); |
| 29 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + if (!isOpen) return; |
| 33 | + (async () => { |
| 34 | + try { |
| 35 | + const resp = await getJson(`${import.meta.env.API_BASE_URL}/testers`); |
| 36 | + const data = resp?.data || []; |
| 37 | + setTesters(data); |
| 38 | + } catch (err) { |
| 39 | + console.error(err); |
| 40 | + addToast({ title: t("error"), description: t("error-fetching-data"), variant: "solid" }); |
| 41 | + } |
| 42 | + })(); |
| 43 | + }, [isOpen]); |
| 44 | + |
| 45 | + const assignToExisting = async () => { |
| 46 | + if (!selectedUuid) return; |
| 47 | + setIsSubmitting(true); |
| 48 | + try { |
| 49 | + const resp = await postJson(`${import.meta.env.API_BASE_URL}/tester/ids`, { uuid: selectedUuid, ids: userIds }); |
| 50 | + if (resp.success) { |
| 51 | + addToast({ title: t("success"), description: t("assigned-successfully"), variant: "solid" }); |
| 52 | + onSuccess?.(); |
| 53 | + onClose(); |
| 54 | + } else { |
| 55 | + addToast({ title: t("error"), description: resp.error || t("error-assigning-testers"), variant: "solid" }); |
| 56 | + } |
| 57 | + } catch (err) { |
| 58 | + console.error(err); |
| 59 | + addToast({ title: t("error"), description: t("error-assigning-testers"), variant: "solid" }); |
| 60 | + } finally { |
| 61 | + setIsSubmitting(false); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + const createAndAssign = async () => { |
| 66 | + if (!createName.trim()) return; |
| 67 | + setIsCreating(true); |
| 68 | + try { |
| 69 | + const resp = await postJson(`${import.meta.env.API_BASE_URL}/tester`, { name: createName.trim(), ids: userIds }); |
| 70 | + if (resp.success) { |
| 71 | + addToast({ title: t("success"), description: t("tester-created-and-assigned"), variant: "solid" }); |
| 72 | + onSuccess?.(); |
| 73 | + onClose(); |
| 74 | + } else { |
| 75 | + addToast({ title: t("error"), description: resp.error || t("error-creating-tester"), variant: "solid" }); |
| 76 | + } |
| 77 | + } catch (err) { |
| 78 | + console.error(err); |
| 79 | + addToast({ title: t("error"), description: t("error-creating-tester"), variant: "solid" }); |
| 80 | + } finally { |
| 81 | + setIsCreating(false); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + return ( |
| 86 | + <Modal isOpen={isOpen} onClose={onClose} aria-labelledby="assign-tester-title"> |
| 87 | + <ModalContent> |
| 88 | + <ModalHeader id="assign-tester-title">{t("assign-tester")}</ModalHeader> |
| 89 | + <ModalBody> |
| 90 | + <div className="mb-4"> |
| 91 | + <label className="text-sm font-semibold">{t("select-tester")}</label> |
| 92 | + <select |
| 93 | + className="w-full p-2 border rounded mt-2" |
| 94 | + value={selectedUuid ?? ""} |
| 95 | + onChange={(e) => setSelectedUuid(e.target.value || null)} |
| 96 | + > |
| 97 | + <option value="">{t("choose-tester")}</option> |
| 98 | + {testers.map((tst) => ( |
| 99 | + <option key={tst.uuid} value={tst.uuid}>{tst.name}</option> |
| 100 | + ))} |
| 101 | + </select> |
| 102 | + <div className="text-sm text-muted-foreground mt-2">{t("or-create-new-tester")}</div> |
| 103 | + <Input value={createName} onChange={(e) => setCreateName(e.target.value)} placeholder={t("enter-tester-name")} className="mt-2" /> |
| 104 | + </div> |
| 105 | + |
| 106 | + <div className="flex justify-end gap-2 mt-4"> |
| 107 | + <Button onPress={onClose} color="secondary">{t("cancel")}</Button> |
| 108 | + <Button color="primary" isLoading={isSubmitting} disabled={!selectedUuid} onPress={assignToExisting}>{t("assign")}</Button> |
| 109 | + <Button color="primary" isLoading={isCreating} disabled={!createName.trim()} onPress={createAndAssign}>{t("create-and-assign")}</Button> |
| 110 | + </div> |
| 111 | + </ModalBody> |
| 112 | + </ModalContent> |
| 113 | + </Modal> |
| 114 | + ); |
| 115 | +} |
0 commit comments