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 { Selectable } from "kysely";
|
|
|
|
|
import { Database } from "@/app/_lib/database";
|
|
|
|
|
import { Edit as InternalEdit } from "@/app/_components/Edit";
|
|
|
|
|
import { generateUpdateAction } from "@/app/_lib/actions";
|
2024-04-29 17:27:25 +02:00
|
|
|
import { serviceConfig } from "@/app/_config/config";
|
2024-04-26 14:31:33 +02:00
|
|
|
|
|
|
|
|
type EditProps = {
|
|
|
|
|
service: string;
|
|
|
|
|
row: Selectable<keyof Database>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const Edit: FC<EditProps> = ({ service, row }) => {
|
|
|
|
|
const {
|
|
|
|
|
[service]: { entity, table, displayName, updateFields: fields },
|
|
|
|
|
} = serviceConfig;
|
|
|
|
|
const updateFieldNames = fields.map((val) => val.name);
|
|
|
|
|
const updateAction = generateUpdateAction({ entity, table, fields });
|
2024-04-26 15:49:58 +02:00
|
|
|
const updateValues = Object.fromEntries(
|
|
|
|
|
Object.entries(row).filter(([key]) => updateFieldNames.includes(key)),
|
|
|
|
|
);
|
|
|
|
|
updateValues.id = row.id;
|
2024-04-26 14:31:33 +02:00
|
|
|
const initialState = {
|
|
|
|
|
message: null,
|
|
|
|
|
errors: {},
|
2024-04-26 15:49:58 +02:00
|
|
|
values: updateValues,
|
2024-04-26 14:31:33 +02:00
|
|
|
};
|
|
|
|
|
const [formState, formAction] = useFormState(updateAction, initialState);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<InternalEdit
|
|
|
|
|
title={`Edit ${displayName}: ${row.name}`}
|
|
|
|
|
entity={entity}
|
|
|
|
|
formAction={formAction}
|
|
|
|
|
formState={formState}
|
|
|
|
|
>
|
|
|
|
|
<Grid container direction="row" rowSpacing={3} columnSpacing={2}>
|
|
|
|
|
{fields.map((field) => (
|
2024-04-26 15:49:58 +02:00
|
|
|
<Grid key={field.name} item xs={field.size ?? 6}>
|
2024-04-26 14:31:33 +02:00
|
|
|
<TextField
|
|
|
|
|
name={field.name}
|
|
|
|
|
label={field.label}
|
2024-04-26 15:49:58 +02:00
|
|
|
lines={field.lines ?? 1}
|
2024-04-26 16:29:13 +02:00
|
|
|
disabled={field.disabled ?? false}
|
|
|
|
|
refreshable={field.refreshable ?? false}
|
2024-04-26 14:31:33 +02:00
|
|
|
required={field.required ?? false}
|
|
|
|
|
formState={formState}
|
|
|
|
|
helperText={field.helperText}
|
|
|
|
|
/>
|
|
|
|
|
</Grid>
|
|
|
|
|
))}
|
|
|
|
|
</Grid>
|
|
|
|
|
</InternalEdit>
|
|
|
|
|
);
|
|
|
|
|
};
|