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

112 строки
2.9 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 { EscalationCombo, UserDetail, UserResult } from ".";
  11. import { cache } from "react";
  12. export interface UserInputs {
  13. username: string;
  14. // name: string;
  15. staffNo?: string;
  16. addAuthIds?: number[];
  17. removeAuthIds?: number[];
  18. password?: string;
  19. confirmPassword?: string;
  20. }
  21. export interface PasswordInputs {
  22. password: string;
  23. newPassword: string;
  24. newPasswordCheck: string;
  25. }
  26. export interface NameList {
  27. id: number;
  28. name: string;
  29. }
  30. export interface NewNameList {
  31. id: number;
  32. name: string;
  33. title: string;
  34. department: string;
  35. }
  36. export const fetchUserDetails = cache(async (id: number) => {
  37. return serverFetchJson<UserDetail>(`${BASE_API_URL}/user/${id}`, {
  38. next: { tags: ["user"] },
  39. });
  40. });
  41. export const fetchNameList = cache(async () => {
  42. return serverFetchJson<NameList[]>(`${BASE_API_URL}/user/name-list`, {
  43. next: { tags: ["user"] },
  44. });
  45. });
  46. export const fetchNewNameList = cache(async () => {
  47. return serverFetchJson<NewNameList[]>(`${BASE_API_URL}/user/new-name-list`, {
  48. next: { tags: ["user"] },
  49. });
  50. });
  51. export const editUser = async (id: number, data: UserInputs) => {
  52. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/${id}`, {
  53. method: "PUT",
  54. body: JSON.stringify(data),
  55. headers: { "Content-Type": "application/json" },
  56. });
  57. revalidateTag("user");
  58. return newUser;
  59. };
  60. export const createUser = async (data: UserInputs) => {
  61. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/save`, {
  62. method: "POST",
  63. body: JSON.stringify(data),
  64. headers: { "Content-Type": "application/json" },
  65. });
  66. revalidateTag("user");
  67. return newUser;
  68. };
  69. export const deleteUser = async (id: number) => {
  70. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/${id}`, {
  71. method: "DELETE",
  72. headers: { "Content-Type": "application/json" },
  73. });
  74. revalidateTag("user");
  75. return newUser;
  76. };
  77. export const changePassword = async (data: any) => {
  78. return serverFetchWithNoContent(`${BASE_API_URL}/user/change-password`, {
  79. method: "PATCH",
  80. body: JSON.stringify(data),
  81. headers: { "Content-Type": "application/json" },
  82. });
  83. };
  84. export const adminChangePassword = async (data: any) => {
  85. return serverFetchWithNoContent(
  86. `${BASE_API_URL}/user/admin-change-password`,
  87. {
  88. method: "PATCH",
  89. body: JSON.stringify(data),
  90. headers: { "Content-Type": "application/json" },
  91. },
  92. );
  93. };
  94. export const fetchEscalationCombo = async () => {
  95. return serverFetchJson<EscalationCombo[]>(`${BASE_API_URL}/user/escalation-combo`, {
  96. next: { tags: ["escalationCombo"]}
  97. })
  98. };