"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 { Sparkles } from "lucide-react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { siteConfig } from "@/config/site"; import { DomainFormData } from "@/lib/dto/domains"; import { EXPIRATION_ENUMS } from "@/lib/enums"; import { generateUrlSuffix } from "@/lib/utils"; import { createDomainSchema } from "@/lib/validations/domain"; 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 { Badge } from "../ui/badge"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "../ui/collapsible"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "../ui/select"; import { Switch } from "../ui/switch"; export type FormData = DomainFormData; export type FormType = "add" | "edit"; export interface DomainFormProps { user: Pick; isShowForm: boolean; setShowForm: Dispatch>; type: FormType; initData?: DomainFormData | null; action: string; onRefresh: () => void; } export function DomainForm({ setShowForm, type, initData, action, onRefresh, }: DomainFormProps) { const [isPending, startTransition] = useTransition(); const [isDeleting, startDeleteTransition] = useTransition(); const [currentRecordStatus, setCurrentRecordStatus] = useState( initData?.enable_dns || false, ); const { handleSubmit, register, formState: { errors }, getValues, setValue, } = useForm({ resolver: zodResolver(createDomainSchema), defaultValues: { id: initData?.id || "", domain_name: initData?.domain_name || "", enable_short_link: initData?.enable_short_link || false, enable_email: initData?.enable_email || false, enable_dns: initData?.enable_dns || false, cf_zone_id: initData?.cf_zone_id || "", cf_api_key: initData?.cf_api_key || "", cf_email: initData?.cf_email || "", cf_api_key_encrypted: initData?.cf_api_key_encrypted || false, max_short_links: initData?.max_short_links || 0, max_email_forwards: initData?.max_email_forwards || 0, max_dns_records: initData?.max_dns_records || 0, active: initData?.active || true, }, }); const onSubmit = handleSubmit((data) => { if (type === "add") { handleCreateDomain(data); } else if (type === "edit") { handleUpdateDomain(data); } }); const handleCreateDomain = async (data: DomainFormData) => { startTransition(async () => { const response = await fetch(`${action}`, { method: "POST", body: JSON.stringify({ 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 handleUpdateDomain = async (data: DomainFormData) => { startTransition(async () => { if (type === "edit") { const response = await fetch(`${action}`, { method: "PUT", body: JSON.stringify(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 handleDeleteDomain = async () => { if (type === "edit") { startDeleteTransition(async () => { const response = await fetch(`${action}`, { method: "DELETE", body: JSON.stringify({ domain_name: initData?.domain_name, }), }); 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" ? "Create" : "Edit"} Domain

Base

{errors?.domain_name ? (

{errors.domain_name.message}

) : (

Required. eg: example.com

)}
setValue("active", value)} disabled />

Services(Optional)

setValue("enable_short_link", value)} />
setValue("enable_email", value)} />
{ setValue("enable_dns", value); setCurrentRecordStatus(value); }} />

Cloudflare Configs(Optional)

{!currentRecordStatus && (
Associate with "DNS Record Service" status
)}
{errors?.cf_zone_id ? (

{errors.cf_zone_id.message}

) : (

Optional.{" "} How to get zone id?

)}
{errors?.cf_api_key ? (

{errors.cf_api_key.message}

) : (

Optional.{" "} How to get api token?

)}
{errors?.cf_email ? (

{errors.cf_email.message}

) : (

Optional.{" "} How to get cloudflare account email?

)}
{/* Action buttons */}
{type === "edit" && ( )}
); }