"use client"; import { LoadingIcon } from "./LoadingIcon"; import { callServerPromise } from "@/components/callServerPromise"; import AutoForm, { AutoFormSubmit } from "@/components/ui/auto-form"; import type { FieldConfig } from "@/components/ui/auto-form/types"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; import { cn } from "@/lib/utils"; import * as React from "react"; import { useState } from "react"; import type { UnknownKeysParam, ZodObject, ZodRawShape, z } from "zod"; export function InsertModal< K extends ZodRawShape, Y extends UnknownKeysParam, Z extends ZodObject >(props: { tooltip?: string; disabled?: boolean; title: string; description: string; dialogClassName?: string; serverAction: (data: z.infer) => Promise; formSchema: Z; fieldConfig?: FieldConfig>; }) { const [open, setOpen] = React.useState(false); const [isLoading, setIsLoading] = React.useState(false); return ( {/* */} {props.tooltip ? (

{props.tooltip}

) : ( )} {/*
*/} {props.title} {props.description} {/* */} { setIsLoading(true); await callServerPromise(props.serverAction(data)); setIsLoading(false); setOpen(false); }} >
Save Changes {isLoading && }
{/*
*/}
); } export function UpdateModal< K extends ZodRawShape, Y extends UnknownKeysParam, Z extends ZodObject >(props: { open?: boolean; setOpen?: (open: boolean) => void; title: string; description: string; dialogClassName?: string; data: z.infer & { id: string; }; serverAction: ( data: z.infer & { id: string; } ) => Promise; formSchema: Z; fieldConfig?: FieldConfig>; trigger?: React.ReactNode; extraButtons?: React.ReactNode; }) { const [_open, _setOpen] = React.useState(false); const open = props.open ?? _open; const setOpen = props.setOpen ?? _setOpen; const [values, setValues] = useState>>({}); const [isLoading, setIsLoading] = React.useState(false); React.useEffect(() => { setValues(props.data); }, [props.data]); return ( {props.trigger ?? ( { setOpen(true); }} > {props.trigger} )} {props.title} {props.description} { setIsLoading(true); await callServerPromise( props.serverAction({ ...data, id: props.data.id, }) ); setIsLoading(false); setOpen(false); }} >
{props.extraButtons} Save Changes {isLoading && }
); }