"use client"; import { FC, useEffect, useState } from "react"; import { useFormState } from "react-dom"; import { Grid } from "@mui/material"; import { useRouter } from "next/navigation"; import { TextField, Dialog, Button, Select, MultiValueField } from "ui"; import { Selectable } from "kysely"; import { type Database } from "bridge-common"; import { generateUpdateAction } from "../lib/actions"; import { serviceConfig } from "../config/config"; import { FieldDescription } from "../lib/service"; import { getBasePath } from "../lib/frontendUtils"; type EditProps = { service: string; row: Selectable; }; export const Edit: FC = ({ service, row }) => { const { [service]: { entity, table, displayName, updateFields }, } = serviceConfig; const fields = updateFields.map((field: any) => { const copy = { ...field }; Object.keys(copy).forEach((key: any) => { if (typeof copy[key] === "function") { delete copy[key]; } }); return copy; }); const updateFieldNames = fields.map((val: FieldDescription) => val.name); const updateAction = generateUpdateAction({ entity, table, fields }); const updateValues = Object.fromEntries( Object.entries(row).filter(([key]) => updateFieldNames.includes(key)), ); updateValues.id = row.id; const initialState = { message: null, errors: {}, values: updateValues, }; const [formState, formAction] = useFormState(updateAction, initialState); const router = useRouter(); const [liveFormState, setLiveFormState] = useState(formState); const updateFormState = (field: string, value: any) => { const newState = { ...liveFormState }; newState.values[field] = value; setLiveFormState(newState); }; useEffect(() => { if (formState.success) { router.push(`${getBasePath()}${entity}`); } }, [formState.success, router, entity]); return ( router.push(`${getBasePath()}${entity}`)} buttons={