feat: images upload

This commit is contained in:
Ivan Rezchikov
2026-07-04 15:45:05 +03:00
parent 2eb0de5357
commit e192717466
38 changed files with 1334 additions and 615 deletions
+34
View File
@@ -0,0 +1,34 @@
import { z } from "zod";
import { unstable_rethrow } from "next/navigation";
import { AppError } from "./action-result";
export type FormState = {
success: boolean;
error?: string;
fieldErrors?: Record<string, string[] | undefined>;
};
export const initialFormState: FormState = { success: false };
export function formAction<Schema extends z.ZodTypeAny>(schema: Schema, handler: (data: z.infer<Schema>, formData: FormData) => Promise<void>) {
return async (_prevState: FormState, formData: FormData): Promise<FormState> => {
const raw = Object.fromEntries(formData.entries());
const parsed = schema.safeParse(raw);
if (!parsed.success) {
return { success: false, fieldErrors: parsed.error.flatten().fieldErrors };
}
try {
await handler(parsed.data, formData);
return { success: true };
} catch (e) {
unstable_rethrow(e);
if (e instanceof AppError) {
return { success: false, error: e.message, fieldErrors: e.fieldErrors };
}
console.error(e);
return { success: false, error: "Something went wrong. Please try again." };
}
};
}