You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

62 regels
1.7 KiB

  1. "use server";
  2. import { serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
  3. import { BASE_API_URL } from "@/config/api";
  4. import { revalidateTag } from "next/cache";
  5. import { UserDetail, UserResult } from ".";
  6. import { cache } from "react";
  7. export interface UserInputs {
  8. name: string;
  9. email?: string;
  10. addAuthIds?: number[];
  11. removeAuthIds?: number[];
  12. password?: string;
  13. }
  14. export interface PasswordInputs {
  15. password: string;
  16. newPassword: string;
  17. newPasswordCheck: string;
  18. }
  19. export const fetchUserDetails = cache(async (id: number) => {
  20. return serverFetchJson<UserDetail>(`${BASE_API_URL}/user/${id}`, {
  21. next: { tags: ["user"] },
  22. });
  23. });
  24. export const editUser = async (id: number, data: UserInputs) => {
  25. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/${id}`, {
  26. method: "PUT",
  27. body: JSON.stringify(data),
  28. headers: { "Content-Type": "application/json" },
  29. });
  30. revalidateTag("user")
  31. return newUser
  32. };
  33. export const deleteUser = async (id: number) => {
  34. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/${id}`, {
  35. method: "DELETE",
  36. headers: { "Content-Type": "application/json" },
  37. });
  38. revalidateTag("user")
  39. return newUser
  40. };
  41. export const changePassword = async (data: any) => {
  42. return serverFetchWithNoContent(`${BASE_API_URL}/user/change-password`, {
  43. method: "PATCH",
  44. body: JSON.stringify(data),
  45. headers: { "Content-Type": "application/json" },
  46. });
  47. };
  48. export const adminChangePassword = async (data: any) => {
  49. return serverFetchWithNoContent(`${BASE_API_URL}/user/admin-change-password`, {
  50. method: "PATCH",
  51. body: JSON.stringify(data),
  52. headers: { "Content-Type": "application/json" },
  53. });
  54. };