Edit and actions updates

This commit is contained in:
Darren Clarke 2024-04-25 12:31:03 +02:00
parent 0997e449bb
commit f87bcc43a5
30 changed files with 759 additions and 139 deletions

View file

@ -0,0 +1,93 @@
"use server";
import { revalidatePath } from "next/cache";
import { db, Database } from "./database";
type AddActionArgs = {
entity: string;
table: keyof Database;
fields: string[];
currentState: any;
formData: FormData;
};
export const addAction = async ({
entity,
table,
fields,
currentState,
formData,
}: AddActionArgs) => {
const newRecord = fields.reduce(
(acc: Record<string, string>, field: string) => {
// @ts-expect-error
acc[field] = formData.get(field)?.toString() ?? null;
return acc;
},
{},
);
await db.insertInto(table).values(newRecord).execute();
revalidatePath(`/${entity}`);
return {
...currentState,
values: newRecord,
success: true,
};
};
type UpdateActionArgs = {
entity: string;
table: keyof Database;
fields: string[];
currentState: any;
formData: FormData;
};
export const updateAction = async ({
entity,
table,
fields,
currentState,
formData,
}: UpdateActionArgs) => {
const id = currentState.values.id;
const updatedRecord = fields.reduce(
(acc: Record<string, string>, field: string) => {
// @ts-expect-error
acc[field] = formData.get(field)?.toString() ?? null;
return acc;
},
{},
);
await db
.updateTable(table)
.set(updatedRecord)
.where("id", "=", id)
.executeTakeFirst();
revalidatePath(`/${entity}/${id}`);
return {
...currentState,
values: updatedRecord,
success: true,
};
};
type DeleteActionArgs = {
entity: string;
table: keyof Database;
id: string;
};
export const deleteAction = async ({ entity, table, id }: DeleteActionArgs) => {
await db.deleteFrom(table).where("id", "=", id).execute();
revalidatePath(`/${entity}`);
return true;
};

View file

@ -1,5 +1,12 @@
import { PostgresDialect, CamelCasePlugin } from "kysely";
import type { GeneratedAlways, Generated, ColumnType } from "kysely";
import type {
GeneratedAlways,
Generated,
ColumnType,
Selectable,
Insertable,
Updateable,
} from "kysely";
import { Pool, types } from "pg";
import { KyselyAuth } from "@auth/kysely-adapter";
@ -22,6 +29,22 @@ export const addGraphileJob = async (jobInfo: GraphileJob) => {
// await db.insertInto("graphile_worker.jobs").values(jobInfo).execute();
};
interface FacebookBotTable {
id: GeneratedAlways<string>;
name: string | null;
description: string | null;
token: string | null;
pageAccessToken: string | null;
appSecret: string | null;
verifyToken: string | null;
pageId: string | null;
appId: string | null;
userId: string | null;
isVerified: Generated<boolean>;
createdAt: GeneratedAlways<Timestamp>;
updatedAt: GeneratedAlways<Timestamp>;
}
export interface Database {
User: {
id: string;
@ -68,21 +91,7 @@ export interface Database {
updatedAt: Date;
};
FacebookBot: {
id: GeneratedAlways<string>;
name: string | null;
description: string | null;
token: string | null;
pageAccessToken: string | null;
appSecret: string | null;
verifyToken: string | null;
pageId: string | null;
appId: string | null;
userId: string | null;
isVerified: Generated<boolean>;
createdAt: GeneratedAlways<Timestamp>;
updatedAt: GeneratedAlways<Timestamp>;
};
FacebookBot: FacebookBotTable;
VoiceLine: {
id: GeneratedAlways<string>;
@ -110,6 +119,8 @@ export interface Database {
};
}
export type FacebookBot = Selectable<FacebookBotTable>;
export const db = new KyselyAuth<Database>({
dialect: new PostgresDialect({
pool: new Pool({