| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- "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<RecoverPasswordFormState> {
- const email = formData.get("email");
- const data = {
- email: typeof email === "string" ? email.trim() : "",
- };
- try {
- const result = await subscribeUser(data) as Record<string, unknown>;
- if (result?.error) {
- const error = result.error as Record<string, unknown>;
- return {
- errors: {
- apiRes: {
- status: false,
- msg: (error.message as string) || "Something went wrong",
- },
- },
- };
- }
- const body = result?.body as Record<string, unknown>;
- const bodyData = body?.data as Record<string, unknown>;
- 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.",
- },
- },
- };
- }
- }
|