Refactoring 2
This commit is contained in:
parent
dd14dfe72e
commit
e4b78ceec2
76 changed files with 870 additions and 734 deletions
|
|
@ -1,60 +0,0 @@
|
|||
"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";
|
||||
import { serviceConfig } from "@/app/_config/config";
|
||||
|
||||
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 });
|
||||
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);
|
||||
|
||||
return (
|
||||
<InternalEdit
|
||||
title={`Edit ${displayName}: ${row.name}`}
|
||||
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}
|
||||
disabled={field.disabled ?? false}
|
||||
refreshable={field.refreshable ?? false}
|
||||
required={field.required ?? false}
|
||||
formState={formState}
|
||||
helperText={field.helperText}
|
||||
/>
|
||||
</Grid>
|
||||
))}
|
||||
</Grid>
|
||||
</InternalEdit>
|
||||
);
|
||||
};
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
import { db } from "@/app/_lib/database";
|
||||
import { serviceConfig } from "@/app/_config/config";
|
||||
import { Edit } from "./_components/Edit";
|
||||
import { db } from "bridge-common";
|
||||
import { serviceConfig, Edit } from "bridge-ui";
|
||||
|
||||
type PageProps = {
|
||||
params: { segment: string[] };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue