FPSMS-frontend
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

72 righe
2.0 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. username: string;
  9. // name: 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 createUser = async (data: UserInputs) => {
  34. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/save`, {
  35. method: "POST",
  36. body: JSON.stringify(data),
  37. headers: { "Content-Type": "application/json" },
  38. });
  39. revalidateTag("user")
  40. return newUser
  41. };
  42. export const deleteUser = async (id: number) => {
  43. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/${id}`, {
  44. method: "DELETE",
  45. headers: { "Content-Type": "application/json" },
  46. });
  47. revalidateTag("user")
  48. return newUser
  49. };
  50. export const changePassword = async (data: any) => {
  51. return serverFetchWithNoContent(`${BASE_API_URL}/user/change-password`, {
  52. method: "PATCH",
  53. body: JSON.stringify(data),
  54. headers: { "Content-Type": "application/json" },
  55. });
  56. };
  57. export const adminChangePassword = async (data: any) => {
  58. return serverFetchWithNoContent(`${BASE_API_URL}/user/admin-change-password`, {
  59. method: "PATCH",
  60. body: JSON.stringify(data),
  61. headers: { "Content-Type": "application/json" },
  62. });
  63. };