actions.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use server";
  2. // 文件顶部的 "use server" 是所有导出的异步函数自动成为 Server Action 的标志。
  3. import { redirect } from "next/navigation";
  4. import {
  5. createUserToLogin,
  6. recoverUserLogin,
  7. subscribeUser,
  8. } from '@/utils/bagisto';
  9. import { isObject } from "@utils/type-guards";
  10. import { RegisterInputs } from "@components/customer/RegistrationForm";
  11. import { RecoverPasswordFormState } from "@components/customer/types";
  12. export type RegisterFormState = {
  13. errors?: {
  14. firstName?: string[];
  15. lastName?: string[];
  16. email?: string[];
  17. password?: string[];
  18. passwordConfirmation?: string[];
  19. apiError?: string;
  20. agreement?: string[];
  21. };
  22. };
  23. export async function redirectToCheckout(formData: FormData) {
  24. const url = formData.get("url") as string;
  25. redirect(url);
  26. }
  27. export async function createUser(formData: RegisterInputs) {
  28. try {
  29. const { firstName, lastName, email, password, passwordConfirmation } =
  30. formData;
  31. const user = await createUserToLogin({
  32. firstName,
  33. lastName,
  34. email,
  35. password,
  36. passwordConfirmation,
  37. });
  38. return {
  39. error: {},
  40. success: true,
  41. customer: user,
  42. };
  43. } catch (err: any) {
  44. return {
  45. error: { message: err?.message || "An error occurred" },
  46. success: false,
  47. customer: null,
  48. };
  49. }
  50. }
  51. export async function recoverPassword(formData: {
  52. email: string;
  53. }): Promise<RecoverPasswordFormState> {
  54. const result = await recoverUserLogin({ email: formData.email }) as any;
  55. if (isObject(result?.error)) {
  56. const error = result.error as Record<string, unknown>;
  57. return {
  58. errors: {
  59. apiRes: {
  60. status: false,
  61. msg: (error?.message as string) ?? "Something went wrong",
  62. },
  63. },
  64. };
  65. }
  66. const body = result?.body;
  67. const createForgotPassword = body?.data?.createForgotPassword;
  68. const forgotPassword = createForgotPassword?.forgotPassword;
  69. return {
  70. success: {
  71. status: forgotPassword?.success ?? false,
  72. msg: "Recovery email sent successfully.",
  73. },
  74. };
  75. }
  76. export async function userSubscribe(
  77. _prevState: RecoverPasswordFormState,
  78. formData: FormData
  79. ): Promise<RecoverPasswordFormState> {
  80. const email = formData.get("email");
  81. const data = {
  82. email: typeof email === "string" ? email.trim() : "",
  83. };
  84. try {
  85. const result = await subscribeUser(data) as Record<string, unknown>;
  86. if (result?.error) {
  87. const error = result.error as Record<string, unknown>;
  88. return {
  89. errors: {
  90. apiRes: {
  91. status: false,
  92. msg: (error.message as string) || "Something went wrong",
  93. },
  94. },
  95. };
  96. }
  97. const body = result?.body as Record<string, unknown>;
  98. const bodyData = body?.data as Record<string, unknown>;
  99. return {
  100. errors: {
  101. apiRes: {
  102. status: true,
  103. msg: (bodyData?.message as string) || "Subscription successful!",
  104. },
  105. },
  106. };
  107. } catch {
  108. return {
  109. errors: {
  110. apiRes: {
  111. status: false,
  112. msg: "Unexpected error occurred. Please try again.",
  113. },
  114. },
  115. };
  116. }
  117. }