"use client"; import { useState } from "react"; import { add } from "date-fns"; import { Controller, useForm } from "react-hook-form"; import { Calendar } from "lucide-react"; // types import { IApiToken } from "@plane/types"; // ui import { Button, CustomSelect, Input, TextArea, ToggleSwitch, TOAST_TYPE, setToast } from "@plane/ui"; // components import { DateDropdown } from "@/components/dropdowns"; // helpers import { cn } from "@/helpers/common.helper"; import { renderFormattedDate, renderFormattedPayloadDate } from "@/helpers/date-time.helper"; 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); // 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 setToast({ type: TOAST_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); // 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(new Date(expiryDate)); } await onSubmit(payload).then(() => { reset(defaultValues); setCustomDate(null); }); }; const today = new Date(); const tomorrow = add(today, { days: 1 }); const expiredAt = watch("expired_at"); return (

Create token

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