2024-04-26 14:31:33 +02:00
|
|
|
"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";
|
2024-04-26 15:49:58 +02:00
|
|
|
import { FieldDescription } from "@/app/_lib/service";
|
2024-04-26 14:31:33 +02:00
|
|
|
|
|
|
|
|
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: {},
|
2024-04-26 15:49:58 +02:00
|
|
|
values: fields.reduce(
|
|
|
|
|
(acc: Record<string, any>, field: FieldDescription) => {
|
|
|
|
|
acc[field.name] = field.defaultValue;
|
|
|
|
|
return acc;
|
|
|
|
|
},
|
|
|
|
|
{},
|
|
|
|
|
),
|
2024-04-26 14:31:33 +02:00
|
|
|
};
|
|
|
|
|
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}>
|
2024-04-26 16:29:13 +02:00
|
|
|
{fields.map(
|
|
|
|
|
(field) =>
|
|
|
|
|
!field.hidden && (
|
|
|
|
|
<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>
|
|
|
|
|
),
|
|
|
|
|
)}
|
2024-04-26 14:31:33 +02:00
|
|
|
</Grid>
|
|
|
|
|
</InternalCreate>
|
|
|
|
|
);
|
|
|
|
|
};
|