57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
"use client";
|
|
|
|
import { FC } from "react";
|
|
import { useFormState } from "react-dom";
|
|
import { Grid } from "@mui/material";
|
|
import { TextField } from "ui";
|
|
import { Create as InternalCreate } from "@/app/_components/Create";
|
|
import { generateCreateAction } from "@/app/_lib/actions";
|
|
import { serviceConfig } from "@/app/_lib/config";
|
|
import { FieldDescription } from "@/app/_lib/service";
|
|
|
|
type CreateProps = {
|
|
service: string;
|
|
};
|
|
|
|
export const Create: FC<CreateProps> = ({ service }) => {
|
|
const {
|
|
[service]: { entity, table, displayName, createFields: fields },
|
|
} = serviceConfig;
|
|
const createAction = generateCreateAction({ entity, table, fields });
|
|
const initialState = {
|
|
message: null,
|
|
errors: {},
|
|
values: fields.reduce(
|
|
(acc: Record<string, any>, field: FieldDescription) => {
|
|
acc[field.name] = field.defaultValue;
|
|
return acc;
|
|
},
|
|
{},
|
|
),
|
|
};
|
|
const [formState, formAction] = useFormState(createAction, initialState);
|
|
|
|
return (
|
|
<InternalCreate
|
|
title={`Create ${displayName}`}
|
|
entity={entity}
|
|
formAction={formAction}
|
|
formState={formState}
|
|
>
|
|
<Grid container direction="row" rowSpacing={3} columnSpacing={2}>
|
|
{fields.map((field) => (
|
|
<Grid key={field.name} item xs={field.size ?? 6}>
|
|
<TextField
|
|
name={field.name}
|
|
label={field.label}
|
|
lines={field.lines ?? 1}
|
|
required={field.required ?? false}
|
|
formState={formState}
|
|
helperText={field.helperText}
|
|
/>
|
|
</Grid>
|
|
))}
|
|
</Grid>
|
|
</InternalCreate>
|
|
);
|
|
};
|