All files / src/shared/GiveExportModal/hooks useExportSideEffects.ts

71.23% Statements 52/73
60% Branches 42/70
100% Functions 10/10
70.42% Lines 50/71

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                                                                                            86x 22x       6x     16x 16x         16x             16x     16x     65x 40x 65x                   16x 16x 16x     16x 16x     16x 2x 1x     1x 1x     2x       14x 10x             10x 10x       10x 10x         4x                 4x                 4x 4x           4x                         86x 23x   3x         3x 1x 1x 13x 8x 13x   1x 1x     2x 2x 2x         86x 16x 8x                        
import { useEffect } from "react";
import type { UseFormReturn } from "react-hook-form";
import { isEqual } from "lodash";
import {
  customerOptions,
  exportType,
  riskOptions,
  TableType,
  transactionInfoOptions,
} from "../const";
import type { FormValues, MerchantReportColumns } from "../giveExportTypes";
import {
  isReconciliationLikeReport,
  matchMerchantPresetFromColumns,
} from "./merchantTable/merchantTableConfig";
import { applyMerchantColumnsIncludedSelection } from "./merchantTable/merchantTableEffects";
 
type Props = {
  fieldTypes: TableType;
  columns: string[] | undefined;
  reportType: string | undefined;
  columnsIncluded: string | undefined;
  methods: UseFormReturn<FormValues>;
  defaultValues: Partial<FormValues>;
  merchantAllColumnValues: string[];
  merchantVisibleColumns: string[];
  merchantReportColumns: MerchantReportColumns;
  timestamp: string;
  currentTimezone: string;
  setIsOpenAdvancedSettings: React.Dispatch<React.SetStateAction<boolean>>;
};
export default function useExportSideEffects({
  fieldTypes,
  columns,
  reportType,
  columnsIncluded,
  methods,
  defaultValues,
  merchantAllColumnValues,
  merchantVisibleColumns,
  merchantReportColumns,
  timestamp,
  currentTimezone,
  setIsOpenAdvancedSettings,
}: Props) {
  // Sync preset selectors (reportType / columnsIncluded) based on column checkbox changes.
  useEffect(() => {
    if (
      fieldTypes !== exportType.RISK_TRANSACTIONS &&
      fieldTypes !== exportType.MERCHANT_TABLE
    ) {
      return;
    }
 
    const currentReportType = methods.getValues("reportType");
    const currentColumnsIncluded = methods.getValues("columnsIncluded");
 
    // Reconciliation normally uses a GET endpoint with no columns.
    // However, if the user manually selects columns in Advanced Settings,
    // switch the dropdown to "custom" so the selection is reflected.
    Iif (isReconciliationLikeReport(currentColumnsIncluded)) {
      if (columns && columns.length > 0) {
        methods.setValue("columnsIncluded", "custom");
      }
      return;
    }
 
    const currentColumns = columns || [];
 
    const { allColumnValues, defaultColumns } =
      fieldTypes === exportType.RISK_TRANSACTIONS
        ? {
            allColumnValues: [
              ...transactionInfoOptions.map((opt) => opt.value),
              ...riskOptions.map((opt) => opt.value),
              ...customerOptions.map((opt) => opt.value),
            ],
            defaultColumns: (defaultValues.columns || []) as string[],
          }
        : {
            allColumnValues: merchantAllColumnValues,
            defaultColumns: merchantVisibleColumns,
          };
 
    // Check if all columns are selected
    const sortedCurrent = [...currentColumns].sort();
    const sortedAll = [...allColumnValues].sort();
    const isAllColumnsSelected = isEqual(sortedCurrent, sortedAll);
 
    // Check if columns match "only in view" preset
    const sortedDefault = [...defaultColumns].sort();
    const matchesDefault = isEqual(sortedCurrent, sortedDefault);
 
    // If all columns are selected, switch to "all"
    if (isAllColumnsSelected) {
      if (fieldTypes === exportType.MERCHANT_TABLE) {
        Iif (methods.getValues("columnsIncluded") !== "all") {
          methods.setValue("columnsIncluded", "all");
        }
        methods.setValue("reportType", "all");
      } else Iif (currentReportType !== "all") {
        methods.setValue("reportType", "all");
      }
      return;
    }
 
    // Merchant table: detect preset reports (monthly/ofac/etc) and set columnsIncluded
    if (fieldTypes === exportType.MERCHANT_TABLE) {
      const preset = matchMerchantPresetFromColumns({
        columns: currentColumns,
        visibleColumns: merchantVisibleColumns,
        reportColumns: merchantReportColumns,
        allColumns: merchantAllColumnValues,
      });
 
      Eif (preset) {
        Iif (methods.getValues("columnsIncluded") !== preset) {
          methods.setValue("columnsIncluded", preset);
        }
        // Keep reportType aligned (used later in payload)
        methods.setValue("reportType", preset);
        return;
      }
    }
 
    // If reportType is "all" but not all columns are selected, switch to custom
    Iif (currentReportType === "all" && !isAllColumnsSelected) {
      if (fieldTypes === exportType.MERCHANT_TABLE) {
        methods.setValue("columnsIncluded", "custom");
      }
      methods.setValue("reportType", "merchant_report");
      return;
    }
 
    // If reportType is "only_in_view" but columns don't match default, switch to custom
    Iif (currentReportType === "only_in_view" && !matchesDefault) {
      if (fieldTypes === exportType.MERCHANT_TABLE) {
        methods.setValue("columnsIncluded", "custom");
      }
      methods.setValue("reportType", "merchant_report");
      return;
    }
 
    // Don't override reportType if it's "only_in_view" and columns match default - this is a preset mode
    Eif (currentReportType === "only_in_view" && matchesDefault) {
      Iif (
        fieldTypes === exportType.MERCHANT_TABLE &&
        methods.getValues("columnsIncluded") !== "visible"
      ) {
        methods.setValue("columnsIncluded", "visible");
      }
      return;
    }
 
    // Otherwise treat as custom selection
    if (!matchesDefault) {
      if (fieldTypes === exportType.MERCHANT_TABLE) {
        methods.setValue("columnsIncluded", "custom");
      }
      methods.setValue("reportType", "merchant_report");
    }
  }, [columns]);
 
  // Handle reportType changes: open advanced settings for Custom, select all columns for All columns
  useEffect(() => {
    if (fieldTypes !== exportType.RISK_TRANSACTIONS || !reportType) return;
 
    Iif (reportType === "merchant_report") {
      setIsOpenAdvancedSettings(true);
      return;
    }
 
    if (reportType === "all") {
      setIsOpenAdvancedSettings(true);
      const allColumnValues = [
        ...transactionInfoOptions.map((opt) => opt.value),
        ...riskOptions.map((opt) => opt.value),
        ...customerOptions.map((opt) => opt.value),
      ];
      methods.setValue("columns", allColumnValues);
      return;
    }
 
    Eif (reportType === "only_in_view") {
      const defaultColumns = defaultValues.columns || [];
      methods.setValue("columns", defaultColumns);
    }
  }, [reportType, fieldTypes]);
 
  // Merchant table: columnsIncluded drives the same behavior as old ExportView "columnsIncluded" select.
  useEffect(() => {
    if (fieldTypes !== exportType.MERCHANT_TABLE || !columnsIncluded) return;
    applyMerchantColumnsIncludedSelection({
      columnsIncluded,
      methods,
      merchantAllColumnValues,
      merchantVisibleColumns,
      merchantReportColumns,
      timestamp,
      currentTimezone,
      setIsOpenAdvancedSettings,
    });
  }, [columnsIncluded, fieldTypes]);
}