FPSMS-frontend
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

111 linhas
2.8 KiB

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