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.

client.ts 1.0 KiB

1234567891011121314151617181920212223242526272829303132
  1. "use client";
  2. import { NEXT_PUBLIC_API_URL } from "@/config/api";
  3. export const exportUserQrCode = async (userIds: number[]): Promise<{ blobValue: Uint8Array; filename: string }> => {
  4. const token = localStorage.getItem("accessToken");
  5. const response = await fetch(`${NEXT_PUBLIC_API_URL}/user/export-qrcode`, {
  6. method: "POST",
  7. headers: {
  8. "Content-Type": "application/json",
  9. ...(token && { Authorization: `Bearer ${token}` }),
  10. },
  11. body: JSON.stringify({ userIds }),
  12. });
  13. if (!response.ok) {
  14. if (response.status === 401) {
  15. throw new Error("Unauthorized: Please log in again");
  16. }
  17. throw new Error(`Failed to export QR code: ${response.status} ${response.statusText}`);
  18. }
  19. const filename = response.headers.get("Content-Disposition")?.split("filename=")[1]?.replace(/"/g, "") || "user_qrcode.pdf";
  20. const blob = await response.blob();
  21. const arrayBuffer = await blob.arrayBuffer();
  22. const blobValue = new Uint8Array(arrayBuffer);
  23. return { blobValue, filename };
  24. };