link-stack/packages/bridge-ui/components/Create.tsx

126 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-04-26 14:31:33 +02:00
"use client";
2024-04-30 11:39:16 +02:00
import { FC, useEffect, useState } from "react";
2024-04-26 14:31:33 +02:00
import { useFormState } from "react-dom";
import { Grid } from "@mui/material";
2024-04-30 11:39:16 +02:00
import { useRouter } from "next/navigation";
2024-06-05 08:52:41 +02:00
import {
Button,
Dialog,
TextField,
Select,
MultiValueField,
} from "@link-stack/ui";
2024-04-30 11:39:16 +02:00
import { generateCreateAction } from "../lib/actions";
import { FieldDescription } from "../lib/service";
import { serviceConfig } from "../config/config";
2024-05-09 07:42:44 +02:00
import { getBasePath } from "../lib/frontendUtils";
2024-04-26 14:31:33 +02:00
type CreateProps = {
service: string;
};
export const Create: FC<CreateProps> = ({ service }) => {
const {
2024-04-29 17:27:25 +02:00
[service]: { entity, table, displayName, createFields },
2024-04-26 14:31:33 +02:00
} = serviceConfig;
2024-04-29 17:27:25 +02:00
const fields = createFields.map((field: any) => {
const copy = { ...field };
Object.keys(copy).forEach((key: any) => {
if (typeof copy[key] === "function") {
delete copy[key];
}
});
return copy;
});
2024-04-26 14:31:33 +02:00
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);
2024-04-30 11:39:16 +02:00
const [liveFormState, setLiveFormState] = useState(formState);
const updateFormState = (field: string, value: any) => {
const newState = { ...liveFormState };
newState.values[field] = value;
setLiveFormState(newState);
};
const router = useRouter();
useEffect(() => {
if (formState.success) {
2024-05-09 07:42:44 +02:00
router.push(`${getBasePath()}${entity}/${formState.values.id}`);
2024-04-30 11:39:16 +02:00
}
}, [formState.success, router, entity, formState.values.id]);
2024-04-26 14:31:33 +02:00
return (
2024-04-30 11:39:16 +02:00
<Dialog
open
2024-04-26 14:31:33 +02:00
title={`Create ${displayName}`}
formAction={formAction}
2024-05-09 07:42:44 +02:00
onClose={() => router.push(`${getBasePath()}${entity}`)}
2024-04-30 11:39:16 +02:00
buttons={
<Grid container justifyContent="space-between">
<Grid item>
<Button
text="Cancel"
kind="secondary"
2024-05-09 07:42:44 +02:00
onClick={() => router.push(`${getBasePath()}${entity}`)}
2024-04-30 11:39:16 +02:00
/>
</Grid>
<Grid item>
<Button text="Save" kind="primary" type="submit" />
</Grid>
</Grid>
}
2024-04-26 14:31:33 +02:00
>
<Grid container direction="row" rowSpacing={3} columnSpacing={2}>
2024-04-29 17:27:25 +02:00
{createFields.map(
2024-04-30 11:39:16 +02:00
(field: FieldDescription) =>
2024-04-26 16:29:13 +02:00
!field.hidden && (
<Grid key={field.name} item xs={field.size ?? 6}>
2024-04-29 17:27:25 +02:00
{field.kind === "select" && (
<Select
name={field.name}
label={field.label}
required={field.required ?? false}
2024-04-30 11:39:16 +02:00
formState={liveFormState}
2024-04-29 17:27:25 +02:00
getOptions={field.getOptions}
2024-04-30 11:39:16 +02:00
updateFormState={updateFormState}
2024-04-29 17:27:25 +02:00
/>
)}
{field.kind === "multi" && (
<MultiValueField
name={field.name}
label={field.label}
formState={formState}
helperText={field.helperText}
/>
)}
{(!field.kind || field.kind === "text") && (
<TextField
name={field.name}
label={field.label}
lines={field.lines ?? 1}
required={field.required ?? false}
formState={formState}
helperText={field.helperText}
/>
)}
2024-04-26 16:29:13 +02:00
</Grid>
),
)}
2024-04-26 14:31:33 +02:00
</Grid>
2024-04-30 11:39:16 +02:00
</Dialog>
2024-04-26 14:31:33 +02:00
);
};