All files / src/features/Settlements/hooks useExportSettlementMerchants.tsx

21.73% Statements 5/23
0% Branches 0/5
33.33% Functions 1/3
21.73% Lines 5/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50        2x 3x   3x                                                 3x     2x                            
import { customInstance } from "@services/api";
import { format } from "date-fns";
import { useState } from "react";
 
export const useExportSettlementMerchants = () => {
  const [isLoading, setIsLoading] = useState<boolean>(false);
  // submerchants.csv?filter=...&sort=...
  const handleExport = async ({ url }: { url: string }) => {
    try {
      setIsLoading(true);
      const data = await customInstance({
        method: "GET",
        url,
      });
 
      const _url = window.URL.createObjectURL(new Blob([data]));
      const link = document.createElement("a");
      const href = _url;
      link.href = href;
      link.setAttribute("download", generateSettlementFileName("csv"));
      document.body.appendChild(link);
      link.click();
      if (document.body.contains(link)) {
        document.body.removeChild(link);
      }
      URL.revokeObjectURL(href);
      setIsLoading(false);
    } catch {
      setIsLoading(false);
    }
  };
 
  return { handleExport, isLoading: isLoading, setIsLoading };
};
 
export const generateSettlementFileName = (
  fileExt: string,
  isDaily = false,
) => {
  if (isDaily)
    return `GIVE_SUBMERCHANT_SETTLEMENT_${format(
      new Date(),
      "yyyyMMdd_HHmmss",
    )}.${fileExt}`;
  return `settlement-${format(
    new Date(),
    "yyyyMMdd_HHmmss",
  )}-merchants.${fileExt}`;
};