FPSMS-frontend
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

92 строки
2.4 KiB

  1. "use server";
  2. // import { serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
  3. // import { BASE_API_URL } from "@/config/api";
  4. import {
  5. serverFetchJson,
  6. serverFetchWithNoContent,
  7. } from "../../utils/fetchUtil";
  8. import { BASE_API_URL } from "../../../config/api";
  9. import { revalidateTag } from "next/cache";
  10. import { UserDetail, UserResult } from ".";
  11. import { cache } from "react";
  12. export interface UserInputs {
  13. username: string;
  14. // name: string;
  15. addAuthIds?: number[];
  16. removeAuthIds?: number[];
  17. password?: string;
  18. }
  19. export interface PasswordInputs {
  20. password: string;
  21. newPassword: string;
  22. newPasswordCheck: string;
  23. }
  24. export interface NameList {
  25. id: number;
  26. name: string;
  27. }
  28. export const fetchUserDetails = cache(async (id: number) => {
  29. return serverFetchJson<UserDetail>(`${BASE_API_URL}/user/${id}`, {
  30. next: { tags: ["user"] },
  31. });
  32. });
  33. export const fetchNameList = cache(async () => {
  34. return serverFetchJson<NameList[]>(`${BASE_API_URL}/user/name-list`, {
  35. next: { tags: ["user"] },
  36. });
  37. });
  38. export const editUser = async (id: number, data: UserInputs) => {
  39. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/${id}`, {
  40. method: "PUT",
  41. body: JSON.stringify(data),
  42. headers: { "Content-Type": "application/json" },
  43. });
  44. revalidateTag("user");
  45. return newUser;
  46. };
  47. export const createUser = async (data: UserInputs) => {
  48. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/save`, {
  49. method: "POST",
  50. body: JSON.stringify(data),
  51. headers: { "Content-Type": "application/json" },
  52. });
  53. revalidateTag("user");
  54. return newUser;
  55. };
  56. export const deleteUser = async (id: number) => {
  57. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/${id}`, {
  58. method: "DELETE",
  59. headers: { "Content-Type": "application/json" },
  60. });
  61. revalidateTag("user");
  62. return newUser;
  63. };
  64. export const changePassword = async (data: any) => {
  65. return serverFetchWithNoContent(`${BASE_API_URL}/user/change-password`, {
  66. method: "PATCH",
  67. body: JSON.stringify(data),
  68. headers: { "Content-Type": "application/json" },
  69. });
  70. };
  71. export const adminChangePassword = async (data: any) => {
  72. return serverFetchWithNoContent(
  73. `${BASE_API_URL}/user/admin-change-password`,
  74. {
  75. method: "PATCH",
  76. body: JSON.stringify(data),
  77. headers: { "Content-Type": "application/json" },
  78. },
  79. );
  80. };