"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, get } from "lodash"; import { useTranslations } from "next-intl"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { PlanQuotaFormData } from "@/lib/dto/plan"; import { formatFileSize } from "@/lib/utils"; 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 [currentStMaxTotalSize, setCurrentStMaxTotalSize] = useState( initData?.stMaxTotalSize || "5242880000", ); const [currentStMaxFileSize, setCurrentStMaxFileSize] = useState( initData?.stMaxFileSize || "26214400", ); 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, stMaxFileSize: initData?.stMaxFileSize || "26214400", stMaxTotalSize: initData?.stMaxTotalSize || "524288000", stMaxFileCount: initData?.stMaxFileCount ?? 1000, 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")}

{t("Base")}

{errors?.name ? (

{errors.name.message}

) : (

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

)}
setValue("isActive", value)} />

{t("Only active plans can be used")}

{t("Shorten Service")}

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

{errors.slNewLinks.message}

) : (

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

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

{errors.slAnalyticsRetention.message}

) : (

{t( "Time range for viewing short link visitor statistics data (days)", )} .

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

{errors.slTrackedClicks.message}

) : (

{t("Monthly limit of tracked clicks (times)")}.

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

{errors.slDomains.message}

) : (

{t("Limit on the number of allowed domains")}.

)}

{t("Email Service")}

{/* 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")}.

)}

{t("Subdomain Service")}

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

{errors.rcNewRecords.message}

) : (

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

)}
{/*

{t("Storage Service")}

setCurrentStMaxFileSize(e.target.value)} /> = {formatFileSize(Number(currentStMaxFileSize), { precision: 0, })}
{errors?.stMaxFileSize ? (

{errors.stMaxFileSize.message}

) : (

{t("Maximum uploaded single file size in bytes")}.

)}
setCurrentStMaxTotalSize(e.target.value)} /> = {formatFileSize(Number(currentStMaxTotalSize), { precision: 0, })}
{errors?.stMaxTotalSize ? (

{errors.stMaxTotalSize.message}

) : (

{t("Maximum uploaded total file size in bytes")}.

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