"use client"; import { Dispatch, SetStateAction, useState, useTransition } from "react"; import Link from "next/link"; import { zodResolver } from "@hookform/resolvers/zod"; import { User } from "@prisma/client"; import { create } from "lodash"; import { useTranslations } from "next-intl"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { PlanQuotaFormData } from "@/lib/dto/plan"; import { createPlanSchema } from "@/lib/validations/plan"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Icons } from "@/components/shared/icons"; import { FormSectionColumns } from "../dashboard/form-section-columns"; import { Switch } from "../ui/switch"; export type FormData = PlanQuotaFormData; export type FormType = "add" | "edit"; export interface PlanFormProps { user: Pick; isShowForm: boolean; setShowForm: Dispatch>; type: FormType; initData?: FormData | null; action: string; onRefresh: () => void; } export function PlanForm({ setShowForm, type, initData, action, onRefresh, }: PlanFormProps) { const t = useTranslations("List"); const [isPending, startTransition] = useTransition(); const [isDeleting, startDeleteTransition] = useTransition(); const { handleSubmit, register, formState: { errors }, setValue, } = useForm({ resolver: zodResolver(createPlanSchema), defaultValues: { id: initData?.id || "", name: initData?.name || "", slTrackedClicks: initData?.slTrackedClicks || 10000, slNewLinks: initData?.slNewLinks || 100, slAnalyticsRetention: initData?.slAnalyticsRetention || 180, slDomains: initData?.slDomains || 1, slAdvancedAnalytics: initData?.slAdvancedAnalytics || false, slCustomQrCodeLogo: initData?.slCustomQrCodeLogo || false, rcNewRecords: initData?.rcNewRecords || 1, emEmailAddresses: initData?.emEmailAddresses || 100, emDomains: initData?.emDomains || 1, emSendEmails: initData?.emSendEmails || 100, appSupport: initData?.appSupport || "BASIC", appApiAccess: initData?.appApiAccess || false, isActive: initData?.isActive || false, }, }); const onSubmit = handleSubmit((data) => { if (type === "add") { handleCreatePlan(data); } else if (type === "edit") { handleUpdatePlan(data); } }); const handleCreatePlan = async (data: FormData) => { startTransition(async () => { const response = await fetch(`${action}`, { method: "POST", body: JSON.stringify({ plan: data, }), }); if (!response.ok || response.status !== 200) { toast.error("Created Failed!", { description: await response.text(), }); } else { // const res = await response.json(); toast.success(`Created successfully!`); setShowForm(false); onRefresh(); } }); }; const handleUpdatePlan = async (data: FormData) => { startTransition(async () => { if (type === "edit") { const response = await fetch(`${action}`, { method: "PUT", body: JSON.stringify({ plan: data }), }); if (!response.ok || response.status !== 200) { toast.error("Update Failed", { description: await response.text(), }); } else { await response.json(); toast.success(`Update successfully!`); setShowForm(false); onRefresh(); } } }); }; const handleDeletePlan = async () => { if (type === "edit") { startDeleteTransition(async () => { const response = await fetch(`${action}`, { method: "DELETE", body: JSON.stringify({ id: initData?.id, }), }); if (!response.ok || response.status !== 200) { toast.error("Delete Failed", { description: await response.text(), }); } else { await response.json(); toast.success(`Success`); setShowForm(false); onRefresh(); } }); } }; return (
{type === "add" ? t("Create Plan") : t("Edit Plan")}
{errors?.name ? (

{errors.name.message}

) : (

{t("Required")}. Plan name must be unique

)}
{/* Short Limit - slNewLinks */}
{errors?.slNewLinks ? (

{errors.slNewLinks.message}

) : (

{t("Monthly limit of short links created")}.

)}
{/* Email Limit - emEmailAddresses */}
{errors?.emEmailAddresses ? (

{errors.emEmailAddresses.message}

) : (

{t("Monthly limit of email addresses created")}.

)}
{/* "Send Limit" - emSendEmails */}
{errors?.emSendEmails ? (

{errors.emSendEmails.message}

) : (

{t("Monthly limit of emails sent")}.

)}
{/* Record Limit - rcNewRecords */}
{errors?.rcNewRecords ? (

{errors.rcNewRecords.message}

) : (

{t("Monthly limit of subdomains created")}.

)}
setValue("isActive", value)} /> {/* Action buttons */}
{type === "edit" && initData?.name !== "free" && ( )}
); }