Generalize WIP

This commit is contained in:
Darren Clarke 2024-04-26 14:31:33 +02:00
parent a3e8b89128
commit cb7a3a08dc
31 changed files with 657 additions and 106 deletions

View file

@ -0,0 +1,56 @@
"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";
type CreateProps = {
service: string;
};
export const Create: FC<CreateProps> = ({ service }) => {
const {
[service]: { entity, table, displayName, createFields: fields },
} = serviceConfig;
const createFieldNames = fields.map((val) => val.name);
const createAction = generateCreateAction({ entity, table, fields });
const initialState = {
message: null,
errors: {},
values: createFieldNames.reduce((acc, key) => {
// @ts-expect-error
acc[key] = fields[key].defaultValue;
return acc;
}, {}),
};
console.log("initialState", initialState);
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 item xs={field.size ?? 6}>
<TextField
key={field.name}
name={field.name}
label={field.label}
required={field.required ?? false}
formState={formState}
helperText={field.helperText}
/>
</Grid>
))}
</Grid>
</InternalCreate>
);
};

View file

@ -0,0 +1,9 @@
import { Create } from "./_components/Create";
type PageProps = {
params: { service: string };
};
export default function Page({ params: { service } }: PageProps) {
return <Create service={service} />;
}

View file

@ -0,0 +1,45 @@
"use client";
import { FC } from "react";
import { Grid } from "@mui/material";
import { DisplayTextField } from "ui";
import { Selectable } from "kysely";
import { Database } from "@/app/_lib/database";
import { Detail as InternalDetail } from "@/app/_components/Detail";
import { generateDeleteAction } from "@/app/_lib/actions";
import { serviceConfig } from "@/app/_lib/config";
type DetailProps = {
service: string;
row: Selectable<keyof Database>;
};
export const Detail: FC<DetailProps> = ({ service, row }) => {
const {
[service]: { entity, table, displayName, displayFields: fields },
} = serviceConfig;
const deleteAction = generateDeleteAction({ entity, table });
return (
<InternalDetail
title={`${displayName}: ${row.name}`}
entity={entity}
id={row.id as string}
deleteAction={deleteAction}
>
<Grid container direction="row" rowSpacing={3} columnSpacing={2}>
{fields.map((field) => (
<Grid item xs={6} key={field.name}>
<DisplayTextField
name={field.name}
label={field.label}
lines={field.lines ?? 1}
value={row[field.name] as string}
copyable={field.copyable ?? false}
/>
</Grid>
))}
</Grid>
</InternalDetail>
);
};

View file

@ -0,0 +1,27 @@
import { db } from "@/app/_lib/database";
import { serviceConfig } from "@/app/_lib/config";
import { Detail } from "./_components/Detail";
type Props = {
params: { service: string; segment: string[] };
};
export default async function Page({ params: { service, segment } }: Props) {
const id = segment?.[0];
if (!id) return null;
const {
[service]: { table },
} = serviceConfig;
const row = await db
.selectFrom(table)
.selectAll()
.where("id", "=", id)
.executeTakeFirst();
if (!row) return null;
return <Detail service={service} row={row} />;
}

View file

@ -0,0 +1,57 @@
"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/_lib/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 initialState = {
message: null,
errors: {},
values: Object.fromEntries(
Object.entries(row).filter(([key]) => updateFieldNames.includes(key)),
),
};
console.log("initialState", initialState);
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 item xs={field.size ?? 6}>
<TextField
key={field.name}
name={field.name}
label={field.label}
required={field.required ?? false}
formState={formState}
helperText={field.helperText}
/>
</Grid>
))}
</Grid>
</InternalEdit>
);
};

View file

@ -0,0 +1,29 @@
import { db } from "@/app/_lib/database";
import { serviceConfig } from "@/app/_lib/config";
import { Edit } from "./_components/Edit";
type PageProps = {
params: { service: string; segment: string[] };
};
export default async function Page({
params: { service, segment },
}: PageProps) {
const id = segment?.[0];
if (!id) return null;
const {
[service]: { table },
} = serviceConfig;
const row = await db
.selectFrom(table)
.selectAll()
.where("id", "=", id)
.executeTakeFirst();
if (!row) return null;
return <Edit service={service} row={row} />;
}

View file

@ -0,0 +1,66 @@
"use server";
import { addAction, updateAction, deleteAction } from "@/app/_lib/actions";
// write a function that returns the addRecordAction
export function generateAddRecordAction(
entity: string,
table: string,
fields: string[],
) {
// The returned function matches the signature of addRecordAction, but uses the parameters from the outer function.
return async (currentState: any, formData: FormData) => {
return addAction({
entity,
table,
fields,
currentState,
formData,
});
};
}
export const addRecordAction = async (
currentState: any,
formData: FormData,
) => {
return addAction({
entity,
table,
fields: [
"name",
"description",
"appId",
"appSecret",
"pageId",
"pageAccessToken",
],
currentState,
formData,
});
};
export const updateFacebookBotAction = async (
currentState: any,
formData: FormData,
) => {
return updateAction({
entity,
table,
fields: [
"name",
"description",
"appId",
"appSecret",
"pageId",
"pageAccessToken",
],
currentState,
formData,
});
};
export const deleteFacebookBotAction = async (id: string) => {
return deleteAction({ entity, table, id });
};

View file

@ -0,0 +1,25 @@
"use client";
import { FC } from "react";
import { List as InternalList } from "@/app/_components/List";
import type { Selectable } from "kysely";
import { Database } from "@/app/_lib/database";
import { serviceConfig } from "@/app/_lib/config";
type ListProps = {
service: string;
rows: Selectable<keyof Database>[];
};
export const List: FC<ListProps> = ({ service, rows }) => {
const { displayName, entity, listColumns } = serviceConfig[service];
return (
<InternalList
title={displayName}
entity={entity}
rows={rows}
columns={listColumns}
/>
);
};

View file

@ -0,0 +1,32 @@
type ServiceLayoutProps = {
children: any;
detail: any;
edit: any;
create: any;
params: {
segment: string[];
};
};
export const ServiceLayout = ({
children,
detail,
edit,
create,
params: { segment },
}: ServiceLayoutProps) => {
const length = segment?.length ?? 0;
const isCreate = length === 2 && segment[1] === "create";
const isEdit = length === 3 && segment[2] === "edit";
const id = length > 1 && !isCreate ? segment[1] : null;
const isDetail = length === 2 && !!id && !isCreate && !isEdit;
return (
<>
{children}
{isDetail && detail}
{isEdit && edit}
{isCreate && create}
</>
);
};

View file

@ -0,0 +1,19 @@
// import { List } from "./_components/List";
// import { db } from "@/app/_lib/database";
// import { serviceConfig } from "@/app/_lib/config";
type PageProps = {
params: {
service: string;
};
};
export default async function Page({ params: { service } }: PageProps) {
console.log({ service });
return <h1> Nice</h1>;
// const config = serviceConfig[service];
// const rows = await db.selectFrom(config.table).selectAll().execute();
// return <List service={service} rows={rows} />;
}