| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- "use server";
- // 文件顶部的 "use server" 是所有导出的异步函数自动成为 Server Action 的标志。
- import { redirect } from "next/navigation";
- import {
- createUserToLogin,
- recoverUserLogin,
- subscribeUser,
- } from '@/utils/bagisto';
- import { isObject } from "@utils/type-guards";
- import { RegisterInputs } from "@components/customer/RegistrationForm";
- import { 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 createUser(formData: RegisterInputs) {
- try {
- const { firstName, lastName, email, password, passwordConfirmation } =
- formData;
- const user = await createUserToLogin({
- firstName,
- lastName,
- email,
- password,
- passwordConfirmation,
- });
- return {
- error: {},
- success: true,
- customer: user,
- };
- } catch (err: any) {
- return {
- error: { message: err?.message || "An error occurred" },
- success: false,
- customer: null,
- };
- }
- }
- export async function recoverPassword(formData: {
- email: string;
- }): Promise<RecoverPasswordFormState> {
- const result = await recoverUserLogin({ email: formData.email }) as any;
- if (isObject(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;
- const createForgotPassword = body?.data?.createForgotPassword;
- const forgotPassword = createForgotPassword?.forgotPassword;
- return {
- success: {
- status: forgotPassword?.success ?? false,
- msg: "Recovery email sent successfully.",
- },
- };
- }
- 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.",
- },
- },
- };
- }
- }
|