actions.ts 3.2 KB

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