FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

55 lines
1.7 KiB

  1. "use client";
  2. import { NEXT_PUBLIC_API_URL } from "@/config/api";
  3. import { WarehouseResult } from "./index";
  4. export const exportWarehouseQrCode = async (warehouseIds: number[]): Promise<{ blobValue: Uint8Array; filename: string }> => {
  5. const token = localStorage.getItem("accessToken");
  6. const response = await fetch(`${NEXT_PUBLIC_API_URL}/warehouse/export-qrcode`, {
  7. method: "POST",
  8. headers: {
  9. "Content-Type": "application/json",
  10. ...(token && { Authorization: `Bearer ${token}` }),
  11. },
  12. body: JSON.stringify({ warehouseIds }),
  13. });
  14. if (!response.ok) {
  15. if (response.status === 401) {
  16. throw new Error("Unauthorized: Please log in again");
  17. }
  18. throw new Error(`Failed to export QR code: ${response.status} ${response.statusText}`);
  19. }
  20. const filename = response.headers.get("Content-Disposition")?.split("filename=")[1]?.replace(/"/g, "") || "warehouse_qrcode.pdf";
  21. const blob = await response.blob();
  22. const arrayBuffer = await blob.arrayBuffer();
  23. const blobValue = new Uint8Array(arrayBuffer);
  24. return { blobValue, filename };
  25. };
  26. export const fetchWarehouseListClient = async (): Promise<WarehouseResult[]> => {
  27. const token = localStorage.getItem("accessToken");
  28. const response = await fetch(`${NEXT_PUBLIC_API_URL}/warehouse`, {
  29. method: "GET",
  30. headers: {
  31. "Content-Type": "application/json",
  32. ...(token && { Authorization: `Bearer ${token}` }),
  33. },
  34. });
  35. if (!response.ok) {
  36. if (response.status === 401) {
  37. throw new Error("Unauthorized: Please log in again");
  38. }
  39. throw new Error(`Failed to fetch warehouse list: ${response.status} ${response.statusText}`);
  40. }
  41. return response.json();
  42. };
  43. //test