import Button from "@/components/Low/Button"; import {Type} from "@/interfaces/user"; import axios from "axios"; import clsx from "clsx"; import {capitalize} from "lodash"; import {useState} from "react"; import {toast} from "react-toastify"; import ShortUniqueId from "short-unique-id"; export default function CodeGenerator() { const [generatedCode, setGeneratedCode] = useState(); const generateCode = (type: Type) => { const uid = new ShortUniqueId(); const code = uid.randomUUID(6); axios .post("/api/code", {type, code}) .then(({data, status}) => { if (data.ok) { toast.success(`Successfully generated a ${capitalize(type)} code!`, {toastId: "success"}); setGeneratedCode(code); return; } if (status === 403) { toast.error(`You do not have permission to generate a ${capitalize(type)} code!`, {toastId: "forbidden"}); } }) .catch(({response: {status}}) => { if (status === 403) { toast.error(`You do not have permission to generate a ${capitalize(type)} code!`, {toastId: "forbidden"}); return; } toast.error(`Something went wrong, please try again later!`, {toastId: "error"}); }); }; return (
{ if (generatedCode) navigator.clipboard.writeText(generatedCode); }}> {generatedCode}
{generatedCode && Give this code to the user to complete their registration}
); }