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

89 строки
2.5 KiB

  1. "use client";
  2. import { clientAuthFetch } from "@/app/utils/clientAuthFetch";
  3. import { NEXT_PUBLIC_API_URL } from "@/config/api";
  4. const base = NEXT_PUBLIC_API_URL;
  5. export type ItemDefaultShelfLifeRow = {
  6. id: number;
  7. itemCode: string;
  8. itemName?: string | null;
  9. defaultDays?: number | null;
  10. minus18Days?: number | null;
  11. useMinus18: boolean;
  12. openedDays?: number | null;
  13. storageC?: string | null;
  14. remarks?: string | null;
  15. effectiveDays?: number | null;
  16. };
  17. export type ItemDefaultShelfLifeInput = {
  18. itemCode: string;
  19. defaultDays?: number | null;
  20. minus18Days?: number | null;
  21. useMinus18: boolean;
  22. openedDays?: number | null;
  23. storageC?: string | null;
  24. remarks?: string | null;
  25. };
  26. async function parseJson<T>(res: Response): Promise<T> {
  27. if (!res.ok) {
  28. throw new Error(await readError(res));
  29. }
  30. return res.json() as Promise<T>;
  31. }
  32. async function readError(res: Response): Promise<string> {
  33. const text = await res.text().catch(() => "");
  34. if (!text) return `HTTP ${res.status}`;
  35. try {
  36. const json = JSON.parse(text) as { message?: string; error?: string };
  37. return json.message || json.error || text;
  38. } catch {
  39. return text;
  40. }
  41. }
  42. export async function fetchItemDefaultShelfLives(
  43. q?: string,
  44. ): Promise<ItemDefaultShelfLifeRow[]> {
  45. const url = new URL(`${base}/itemDefaultShelfLives`);
  46. if (q?.trim()) url.searchParams.set("q", q.trim());
  47. const res = await clientAuthFetch(url.toString(), { method: "GET" });
  48. return parseJson<ItemDefaultShelfLifeRow[]>(res);
  49. }
  50. export async function createItemDefaultShelfLife(
  51. data: ItemDefaultShelfLifeInput,
  52. ): Promise<ItemDefaultShelfLifeRow> {
  53. const res = await clientAuthFetch(`${base}/itemDefaultShelfLives`, {
  54. method: "POST",
  55. headers: { "Content-Type": "application/json" },
  56. body: JSON.stringify(data),
  57. });
  58. return parseJson<ItemDefaultShelfLifeRow>(res);
  59. }
  60. export async function updateItemDefaultShelfLife(
  61. id: number,
  62. data: ItemDefaultShelfLifeInput,
  63. ): Promise<ItemDefaultShelfLifeRow> {
  64. const res = await clientAuthFetch(`${base}/itemDefaultShelfLives/${id}`, {
  65. method: "PUT",
  66. headers: { "Content-Type": "application/json" },
  67. body: JSON.stringify(data),
  68. });
  69. return parseJson<ItemDefaultShelfLifeRow>(res);
  70. }
  71. export async function deleteItemDefaultShelfLife(
  72. id: number,
  73. ): Promise<ItemDefaultShelfLifeRow[]> {
  74. const res = await clientAuthFetch(`${base}/itemDefaultShelfLives/${id}`, {
  75. method: "DELETE",
  76. });
  77. return parseJson<ItemDefaultShelfLifeRow[]>(res);
  78. }