Generalize WIP
This commit is contained in:
parent
a3e8b89128
commit
cb7a3a08dc
31 changed files with 657 additions and 106 deletions
|
|
@ -0,0 +1,83 @@
|
|||
"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 { addFacebookBotAction } from "../../_actions/facebook";
|
||||
|
||||
export const Create: FC = () => {
|
||||
const initialState = {
|
||||
message: null,
|
||||
errors: {},
|
||||
values: {
|
||||
name: "",
|
||||
description: "",
|
||||
one: "",
|
||||
},
|
||||
};
|
||||
const [formState, formAction] = useFormState(
|
||||
addFacebookBotAction,
|
||||
initialState,
|
||||
);
|
||||
|
||||
return (
|
||||
<InternalCreate
|
||||
title="Create Facebook Connection"
|
||||
entity="facebook"
|
||||
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 item xs={6}>
|
||||
<TextField
|
||||
name="appId"
|
||||
label="App ID"
|
||||
required
|
||||
formState={formState}
|
||||
helperText="Get it from Facebook Developer Console"
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<TextField
|
||||
name="appSecret"
|
||||
label="App Secret"
|
||||
required
|
||||
formState={formState}
|
||||
helperText="Get it from Facebook Developer Console"
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<TextField
|
||||
name="pageId"
|
||||
label="Page ID"
|
||||
required
|
||||
formState={formState}
|
||||
helperText="Get it from Facebook Developer Console"
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<TextField
|
||||
name="pageAccessToken"
|
||||
label="Page Access Token"
|
||||
required
|
||||
formState={formState}
|
||||
helperText="Get it from Facebook Developer Console"
|
||||
/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</InternalCreate>
|
||||
);
|
||||
};
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
import { Create } from "./_components/Create";
|
||||
|
||||
export default function Page() {
|
||||
return <Create />;
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
"use client";
|
||||
|
||||
import { FC } from "react";
|
||||
import { Grid } from "@mui/material";
|
||||
import { DisplayTextField } from "ui";
|
||||
import { FacebookBot } from "@/app/_lib/database";
|
||||
import { Detail as InternalDetail } from "@/app/_components/Detail";
|
||||
import { deleteFacebookBotAction } from "../../_actions/facebook";
|
||||
|
||||
type DetailProps = {
|
||||
row: FacebookBot;
|
||||
};
|
||||
|
||||
export const Detail: FC<DetailProps> = ({ row }) => (
|
||||
<InternalDetail
|
||||
title={`Facebook Connection: ${row.name}`}
|
||||
entity="facebook"
|
||||
id={row.id}
|
||||
deleteAction={deleteFacebookBotAction}
|
||||
>
|
||||
<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 item xs={6}>
|
||||
<DisplayTextField name="appId" label="App ID" value={row.appId} />
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<DisplayTextField
|
||||
name="appSecret"
|
||||
label="App Secret"
|
||||
value={row.appSecret}
|
||||
copyable
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<DisplayTextField name="pageId" label="Page ID" value={row.pageId} />
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<DisplayTextField
|
||||
name="pageAccessToken"
|
||||
label="Page Access Token"
|
||||
value={row.pageAccessToken}
|
||||
/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</InternalDetail>
|
||||
);
|
||||
|
|
@ -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("FacebookBot")
|
||||
.selectAll()
|
||||
.where("id", "=", id)
|
||||
.executeTakeFirst();
|
||||
|
||||
if (!row) return null;
|
||||
|
||||
return <Detail row={row} />;
|
||||
}
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
"use client";
|
||||
|
||||
import { FC } from "react";
|
||||
import { useFormState } from "react-dom";
|
||||
import { Grid } from "@mui/material";
|
||||
import { TextField } from "ui";
|
||||
import { FacebookBot } from "@/app/_lib/database";
|
||||
import { Edit as InternalEdit } from "@/app/_components/Edit";
|
||||
import { updateFacebookBotAction } from "../../_actions/facebook";
|
||||
|
||||
type EditProps = {
|
||||
row: FacebookBot;
|
||||
};
|
||||
|
||||
export const Edit: FC<EditProps> = ({ row }) => {
|
||||
const initialState = {
|
||||
message: null,
|
||||
errors: {},
|
||||
values: row,
|
||||
};
|
||||
const [formState, formAction] = useFormState(
|
||||
updateFacebookBotAction,
|
||||
initialState,
|
||||
);
|
||||
|
||||
return (
|
||||
<InternalEdit
|
||||
title={`Edit Facebook Connection: ${row.name}`}
|
||||
entity="facebook"
|
||||
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 item xs={6}>
|
||||
<TextField
|
||||
name="token"
|
||||
label="Token"
|
||||
disabled
|
||||
formState={formState}
|
||||
refreshable
|
||||
helperText="Token used to authenticate requests from Link"
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<TextField
|
||||
name="verifyToken"
|
||||
label="Verify Token"
|
||||
disabled
|
||||
formState={formState}
|
||||
refreshable
|
||||
helperText="Token used to authenticate requests from Facebook"
|
||||
/>
|
||||
</Grid>
|
||||
|
||||
<Grid item xs={6}>
|
||||
<TextField
|
||||
name="appId"
|
||||
label="App ID"
|
||||
required
|
||||
formState={formState}
|
||||
helperText="App ID of your approved Facebook application"
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<TextField
|
||||
name="appSecret"
|
||||
label="App Secret"
|
||||
required
|
||||
formState={formState}
|
||||
helperText="App Secret of your approved Facebook application"
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<TextField
|
||||
name="pageId"
|
||||
label="Page ID"
|
||||
required
|
||||
formState={formState}
|
||||
helperText="Page ID of the Facebook page that will receive messages"
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<TextField
|
||||
name="pageAccessToken"
|
||||
label="Page Access Token"
|
||||
required
|
||||
formState={formState}
|
||||
helperText="Access Token of the Facbook page that will receive messages"
|
||||
/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</InternalEdit>
|
||||
);
|
||||
};
|
||||
|
|
@ -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("FacebookBot")
|
||||
.selectAll()
|
||||
.where("id", "=", id)
|
||||
.executeTakeFirst();
|
||||
|
||||
if (!row) return null;
|
||||
|
||||
return <Edit row={row} />;
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
"use server";
|
||||
|
||||
import { addAction, updateAction, deleteAction } from "@/app/_lib/actions";
|
||||
|
||||
const entity = "facebook";
|
||||
const table = "FacebookBot";
|
||||
|
||||
export const addFacebookBotAction = 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 });
|
||||
};
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
"use client";
|
||||
|
||||
import { FC } from "react";
|
||||
import { GridColDef } from "@mui/x-data-grid-pro";
|
||||
import { List } from "@/app/_components/List";
|
||||
|
||||
type FacebookBotsListProps = {
|
||||
rows: any[];
|
||||
};
|
||||
|
||||
export const FacebookBotsList: FC<FacebookBotsListProps> = ({ rows }) => {
|
||||
const columns: GridColDef[] = [
|
||||
{
|
||||
field: "name",
|
||||
headerName: "Name",
|
||||
flex: 1,
|
||||
},
|
||||
{
|
||||
field: "description",
|
||||
headerName: "Description",
|
||||
flex: 2,
|
||||
},
|
||||
{
|
||||
field: "updatedAt",
|
||||
headerName: "Updated At",
|
||||
valueGetter: (value: any) => new Date(value).toLocaleString(),
|
||||
flex: 1,
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<List
|
||||
title="Facebook Connections"
|
||||
entity="facebook"
|
||||
rows={rows}
|
||||
columns={columns}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
import { ServiceLayout } from "@/app/_components/ServiceLayout";
|
||||
|
||||
export default ServiceLayout;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
import { FacebookBotsList } from "./_components/FacebookBotsList";
|
||||
import { db } from "@/app/_lib/database";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export default async function Page() {
|
||||
const rows = await db.selectFrom("FacebookBot").selectAll().execute();
|
||||
|
||||
return <FacebookBotsList rows={rows} />;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue