import React, { useState } from "react"; import TextInput from "@/components/TextInput"; import { KeyExpiry } from "@/types/global"; import { useSession } from "next-auth/react"; import toast from "react-hot-toast"; import Modal from "../Modal"; type Props = { onClose: Function; }; export default function NewKeyModal({ onClose }: Props) { const { data } = useSession(); const initial = { name: "", expires: 0 as KeyExpiry, }; const [key, setKey] = useState(initial as any); const [submitLoader, setSubmitLoader] = useState(false); const submit = async () => { if (!submitLoader) { setSubmitLoader(true); let response; const load = toast.loading("Creating..."); response = await fetch("/api/v1/tokens", { method: "POST", body: JSON.stringify({ name: key.name, expires: key.expires, }), }); const data = await response.json(); toast.dismiss(load); if (response.ok) { toast.success(`Created!`); onClose(); } else toast.error(data.response as string); setSubmitLoader(false); return response; } }; return (

Create an Access Token

Name

setKey({ ...key, name: e.target.value })} placeholder="e.g. For the Mobile App" className="bg-base-200" />

Date of Expiry

{key.expires === KeyExpiry.sevenDays && "7 Days"} {key.expires === KeyExpiry.oneMonth && "30 Days"} {key.expires === KeyExpiry.twoMonths && "60 Days"} {key.expires === KeyExpiry.threeMonths && "90 Days"} {key.expires === KeyExpiry.never && "No Expiration"}
); }