All files / src/features/MerchantPortal/Customers/CustomersTable/hooks useExportCustomers.ts

6.25% Statements 1/16
100% Branches 0/0
0% Functions 0/2
6.25% Lines 1/16

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 51 52 53 54                        2x                                                                                  
import { useTranslation } from "react-i18next";
import { showMessage } from "components/common/Toast";
import RESOURCE_BASE, { OPERATIONS } from "constants/permissions";
import { useCustomerReport } from "hooks/merchant-api/customers/useDownloadCustomers";
import { namespaces } from "localization/resources/i18n.constants";
import {
  composePermission,
  useAccessControl,
} from "features/Permissions/AccessControl";
import { saveAs } from "file-saver";
import { useState } from "react";
 
export const useExportCustomers = () => {
  const { t } = useTranslation(namespaces.pages.customers);
 
  const resource = composePermission(
    RESOURCE_BASE.MERCHANT,
    RESOURCE_BASE.CUSTOMER,
  );
 
  const [loading, setLoading] = useState(false);
 
  const canExport = useAccessControl({
    resource,
    operation: OPERATIONS.EXPORT,
  });
  const downloadCustomersReport = useCustomerReport();
 
  const exportCustomers = async (
    event: React.MouseEvent<HTMLButtonElement>,
  ) => {
    event.preventDefault();
    setLoading(true);
 
    try {
      const response = await downloadCustomersReport.mutateAsync();
      saveAs(new Blob([response]), "customers.csv");
    } catch (error: any) {
      console.error(error);
      showMessage(
        "Error",
        t("toast.Something_went_wrong_while_downloading_report", {
          ns: namespaces.common,
        }),
        true,
        t("toast.error", { ns: namespaces.common }),
      );
    } finally {
      setLoading(false);
    }
  };
  return { exportCustomers, canExport, loading };
};