Generalize WIP
This commit is contained in:
parent
a3e8b89128
commit
cb7a3a08dc
31 changed files with 657 additions and 106 deletions
|
|
@ -1,93 +1,65 @@
|
|||
"use server";
|
||||
import { Database } from "./database";
|
||||
import { FieldDescription, Entity } from "./service";
|
||||
import {
|
||||
createAction,
|
||||
updateAction,
|
||||
deleteAction,
|
||||
} from "@/app/_actions/service";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { db, Database } from "./database";
|
||||
|
||||
type AddActionArgs = {
|
||||
entity: string;
|
||||
type GenerateCreateActionArgs = {
|
||||
entity: Entity;
|
||||
table: keyof Database;
|
||||
fields: string[];
|
||||
currentState: any;
|
||||
formData: FormData;
|
||||
fields: FieldDescription[];
|
||||
};
|
||||
|
||||
export const addAction = async ({
|
||||
export function generateCreateAction({
|
||||
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,
|
||||
}: GenerateCreateActionArgs) {
|
||||
return async (currentState: any, formData: FormData) => {
|
||||
return createAction({
|
||||
entity,
|
||||
table,
|
||||
fields,
|
||||
currentState,
|
||||
formData,
|
||||
});
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
type UpdateActionArgs = {
|
||||
entity: string;
|
||||
type GenerateUpdateActionArgs = {
|
||||
entity: Entity;
|
||||
table: keyof Database;
|
||||
fields: string[];
|
||||
currentState: any;
|
||||
formData: FormData;
|
||||
fields: FieldDescription[];
|
||||
};
|
||||
|
||||
export const updateAction = async ({
|
||||
export function generateUpdateAction({
|
||||
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,
|
||||
}: GenerateUpdateActionArgs) {
|
||||
return async (currentState: any, formData: FormData) => {
|
||||
return updateAction({
|
||||
entity,
|
||||
table,
|
||||
fields,
|
||||
currentState,
|
||||
formData,
|
||||
});
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
type DeleteActionArgs = {
|
||||
entity: string;
|
||||
type GenerateDeleteActionArgs = {
|
||||
entity: Entity;
|
||||
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;
|
||||
};
|
||||
export function generateDeleteAction({
|
||||
entity,
|
||||
table,
|
||||
}: GenerateDeleteActionArgs) {
|
||||
return async (id: string) => {
|
||||
return deleteAction({ entity, table, id });
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue