"use server"; // 文件顶部的 "use server" 是所有导出的异步函数自动成为 Server Action 的标志。 import { redirect } from "next/navigation"; import { subscribeUser, } from '@/utils/bagisto'; import type { RecoverPasswordFormState } from "@components/customer/types"; export type RegisterFormState = { errors?: { firstName?: string[]; lastName?: string[]; email?: string[]; password?: string[]; passwordConfirmation?: string[]; apiError?: string; agreement?: string[]; }; }; export async function redirectToCheckout(formData: FormData) { const url = formData.get("url") as string; redirect(url); } export async function userSubscribe( _prevState: RecoverPasswordFormState, formData: FormData ): Promise { const email = formData.get("email"); const data = { email: typeof email === "string" ? email.trim() : "", }; try { const result = await subscribeUser(data) as Record; if (result?.error) { const error = result.error as Record; return { errors: { apiRes: { status: false, msg: (error.message as string) || "Something went wrong", }, }, }; } const body = result?.body as Record; const bodyData = body?.data as Record; return { errors: { apiRes: { status: true, msg: (bodyData?.message as string) || "Subscription successful!", }, }, }; } catch { return { errors: { apiRes: { status: false, msg: "Unexpected error occurred. Please try again.", }, }, }; } }