"use client"; import { FC, useState, useEffect } from "react"; import { useFormState } from "react-dom"; import { useRouter } from "next/navigation"; import { Grid } from "@mui/material"; import { Dialog, Button, TextField, Autocomplete, Select, } from "@link-stack/ui"; import { createTicketAction } from "app/_actions/tickets"; import { getCustomersAction } from "app/_actions/users"; import { getGroupsAction } from "app/_actions/groups"; interface TicketCreateDialogProps { open: boolean; closeDialog: () => void; } export const TicketCreateDialog: FC = ({ open, closeDialog, }) => { const [customers, setCustomers] = useState([]); const [groups, setGroups] = useState([]); const initialState = { messages: [], errors: [], values: { customerId: "", groupId: "", ownerId: "", priorityId: "", stateId: "", tags: [], title: "", article: { body: "", type: "note", internal: true, }, }, }; const [formState, formAction] = useFormState( createTicketAction, initialState, ); const [liveFormState, setLiveFormState] = useState(formState); const updateFormState = (field: string, value: any) => { console.log({ value }); const newState = { ...liveFormState }; newState.values[field] = value; setLiveFormState(newState); }; useEffect(() => { const fetchUsers = async () => { const result = await getCustomersAction(); console.log({ result }); setCustomers(result); }; fetchUsers(); }, []); useEffect(() => { const fetchGroups = async () => { const result = await getGroupsAction(); console.log({ result }); setGroups(result); }; fetchGroups(); }, []); const router = useRouter(); useEffect(() => { if (formState.success) { closeDialog(); } }, [formState.success, router]); return ( ); };