import { useState } from "react"; import { add } from "date-fns"; import { Controller, useForm } from "react-hook-form"; import { DateDropdown } from "components/dropdowns"; import { Calendar } from "lucide-react"; // hooks import useToast from "hooks/use-toast"; // ui import { Button, CustomSelect, Input, TextArea, ToggleSwitch } from "@plane/ui"; // helpers import { renderFormattedDate, renderFormattedPayloadDate } from "helpers/date-time.helper"; // types import { IApiToken } from "@plane/types"; type Props = { handleClose: () => void; neverExpires: boolean; toggleNeverExpires: () => void; onSubmit: (data: Partial) => Promise; }; const EXPIRY_DATE_OPTIONS = [ { key: "1_week", label: "1 week", value: { weeks: 1 }, }, { key: "1_month", label: "1 month", value: { months: 1 }, }, { key: "3_months", label: "3 months", value: { months: 3 }, }, { key: "1_year", label: "1 year", value: { years: 1 }, }, ]; const defaultValues: Partial = { label: "", description: "", expired_at: null, }; const getExpiryDate = (val: string): string | null => { const today = new Date(); const dateToAdd = EXPIRY_DATE_OPTIONS.find((option) => option.key === val)?.value; if (dateToAdd) { const expiryDate = add(today, dateToAdd); return renderFormattedDate(expiryDate); } return null; }; export const CreateApiTokenForm: React.FC = (props) => { const { handleClose, neverExpires, toggleNeverExpires, onSubmit } = props; // states const [customDate, setCustomDate] = useState(null); // toast alert const { setToastAlert } = useToast(); // form const { control, formState: { errors, isSubmitting }, handleSubmit, reset, watch, } = useForm({ defaultValues }); const handleFormSubmit = async (data: IApiToken) => { // if never expires is toggled off, and the user has not selected a custom date or a predefined date, show an error if (!neverExpires && (!data.expired_at || (data.expired_at === "custom" && !customDate))) return setToastAlert({ type: "error", title: "Error!", message: "Please select an expiration date.", }); const payload: Partial = { label: data.label, description: data.description, }; // if never expires is toggled on, set expired_at to null if (neverExpires) payload.expired_at = null; // if never expires is toggled off, and the user has selected a custom date, set expired_at to the custom date else if (data.expired_at === "custom") payload.expired_at = renderFormattedPayloadDate(customDate ?? new Date()); // if never expires is toggled off, and the user has selected a predefined date, set expired_at to the predefined date else { const expiryDate = getExpiryDate(data.expired_at ?? ""); if (expiryDate) payload.expired_at = renderFormattedPayloadDate(expiryDate); } await onSubmit(payload).then(() => { reset(defaultValues); setCustomDate(null); }); }; const today = new Date(); const tomorrow = add(today, { days: 1 }); return (

Create token

val.trim() !== "" || "Title is required", }} render={({ field: { value, onChange } }) => ( )} /> {errors.label && {errors.label.message}}
(