Add CRUD for other entities

This commit is contained in:
Darren Clarke 2024-04-25 13:36:50 +02:00
parent f87bcc43a5
commit a3e8b89128
64 changed files with 949 additions and 62 deletions

View file

@ -0,0 +1,47 @@
"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 { addSignalBotAction } from "../../_actions/signal";
export const Create: FC = () => {
const initialState = {
message: null,
errors: {},
values: {
name: "",
description: "",
one: "",
},
};
const [formState, formAction] = useFormState(
addSignalBotAction,
initialState,
);
return (
<InternalCreate
title="Create Signal Connection"
entity="signal"
formAction={formAction}
formState={formState}
>
<Grid container direction="row" rowSpacing={3} columnSpacing={2}>
<Grid item xs={12}>
<TextField name="name" label="Name" required formState={formState} />
</Grid>
<Grid item xs={12}>
<TextField
name="description"
label="Description"
formState={formState}
lines={3}
/>
</Grid>
</Grid>
</InternalCreate>
);
};

View file

@ -0,0 +1,5 @@
import { Create } from "./_components/Create";
export default function Page() {
return <Create />;
}

View file

@ -0,0 +1,35 @@
"use client";
import { FC } from "react";
import { Grid } from "@mui/material";
import { DisplayTextField } from "ui";
import { SignalBot } from "@/app/_lib/database";
import { Detail as InternalDetail } from "@/app/_components/Detail";
import { deleteSignalBotAction } from "../../_actions/signal";
type DetailProps = {
row: SignalBot;
};
export const Detail: FC<DetailProps> = ({ row }) => (
<InternalDetail
title={`Signal Connection: ${row.name}`}
entity="signal"
id={row.id}
deleteAction={deleteSignalBotAction}
>
<Grid container direction="row" rowSpacing={3} columnSpacing={2}>
<Grid item xs={12}>
<DisplayTextField name="name" label="Name" value={row.name} />
</Grid>
<Grid item xs={12}>
<DisplayTextField
name="description"
label="Description"
lines={3}
value={row.description}
/>
</Grid>
</Grid>
</InternalDetail>
);

View file

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

View file

@ -0,0 +1,48 @@
"use client";
import { FC } from "react";
import { useFormState } from "react-dom";
import { Grid } from "@mui/material";
import { TextField } from "ui";
import { SignalBot } from "@/app/_lib/database";
import { Edit as InternalEdit } from "@/app/_components/Edit";
import { updateSignalBotAction } from "../../_actions/signal";
type EditProps = {
row: SignalBot;
};
export const Edit: FC<EditProps> = ({ row }) => {
const initialState = {
message: null,
errors: {},
values: row,
};
const [formState, formAction] = useFormState(
updateSignalBotAction,
initialState,
);
return (
<InternalEdit
title={`Edit Signal Connection: ${row.name}`}
entity="signal"
formAction={formAction}
formState={formState}
>
<Grid container direction="row" rowSpacing={3} columnSpacing={2}>
<Grid item xs={12}>
<TextField name="name" label="Name" required formState={formState} />
</Grid>
<Grid item xs={12}>
<TextField
name="description"
label="Description"
formState={formState}
lines={3}
/>
</Grid>
</Grid>
</InternalEdit>
);
};

View file

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

View file

@ -0,0 +1,36 @@
"use server";
import { addAction, updateAction, deleteAction } from "@/app/_lib/actions";
const entity = "signal";
const table = "SignalBot";
export const addSignalBotAction = async (
currentState: any,
formData: FormData,
) => {
return addAction({
entity,
table,
fields: ["name", "description"],
currentState,
formData,
});
};
export const updateSignalBotAction = async (
currentState: any,
formData: FormData,
) => {
return updateAction({
entity,
table,
fields: ["name"],
currentState,
formData,
});
};
export const deleteSignalBotAction = async (id: string) => {
return deleteAction({ entity, table, id });
};

View file

@ -0,0 +1,47 @@
"use client";
import { FC } from "react";
import { GridColDef } from "@mui/x-data-grid-pro";
import { List } from "@/app/_components/List";
type SignalBotsListProps = {
rows: any[];
};
export const SignalBotsList: FC<SignalBotsListProps> = ({ rows }) => {
const columns: GridColDef[] = [
{
field: "id",
headerName: "ID",
flex: 1,
},
{
field: "phoneNumber",
headerName: "Phone Number",
flex: 2,
},
{
field: "createdAt",
headerName: "Created At",
valueGetter: (params: any) =>
new Date(params.row?.createdAt).toLocaleString(),
flex: 1,
},
{
field: "updatedAt",
headerName: "Updated At",
valueGetter: (params: any) =>
new Date(params.row?.updatedAt).toLocaleString(),
flex: 1,
},
];
return (
<List
title="Signal Connections"
entity="signal"
rows={rows}
columns={columns}
/>
);
};

View file

@ -0,0 +1,3 @@
import { ServiceLayout } from "@/app/_components/ServiceLayout";
export default ServiceLayout;

View file

@ -0,0 +1,10 @@
import { SignalBotsList } from "./_components/SignalBotsList";
import { db } from "@/app/_lib/database";
export const dynamic = "force-dynamic";
export default async function Page() {
const rows = await db.selectFrom("SignalBot").selectAll().execute();
return <SignalBotsList rows={rows} />;
}