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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | 13x 13x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 49x 1x 48x 34x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 83x 83x 83x 162x 2x 2x 83x 83x 13x 2x 2x 2x 2x 2x | import { useMemo } from "react";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import {
selectedQueries,
setFilters,
useTableFilters,
} from "@redux/slices/tableFilters";
import { formatQueryParamsList } from "@common/TableFilters/utils";
import { CheckIcon } from "@phosphor-icons/react";
import GiveThumbnail from "@shared/Thumbnail/GiveThumbnail";
import { addSizeToImage } from "@utils/image.helpers";
import {
TOption,
UseDropdownFilterParams,
BuildQueryStringArgs,
} from "../types";
const testIdMap: Record<string, string> = {
all: "All Merchants",
pre_approval: "Pre-Approval",
auto_approval: "Auto-Approval",
};
export const useDropdownFilter = ({
queryKey,
options,
}: UseDropdownFilterParams) => {
const dispatch = useAppDispatch();
// Hooks from tableFilters slice to manage quick filter and query string
const { quickFilter, setQuickFilterValue, setQueryStringWithQuickFilter } =
useTableFilters();
// Get current filters from Redux store (selectedQueries)
const contextQueries = useAppSelector(selectedQueries) || {};
const { watchlist, ...otherQueries } = contextQueries; // Exclude watchlist
const isMerchantDropdown = queryKey === "merchantName";
const isProcessorDropdown = queryKey === "processorName";
const isMerchantOrProcessor = isMerchantDropdown || isProcessorDropdown;
const currentQueryKeyFilters = contextQueries[queryKey] ?? [];
const hasFilterForThisKey = currentQueryKeyFilters.length > 0;
// Determine the currently selected value
// For merchant/processor dropdowns, use contextQueries
// For other dropdowns, use quickFilter
let selectedValue: string;
if (isMerchantOrProcessor) {
if (hasFilterForThisKey) {
selectedValue = currentQueryKeyFilters[0]?.value;
} else {
selectedValue = "all";
}
} else {
selectedValue = quickFilter || "all";
}
// Determine the selected option
const selectedData: TOption = (() => {
// Try to find in the current options list
const optionFromList = options.find((o) => o.value === selectedValue);
// Use stored filter from contextQueries if not found in options
const optionFromContext =
!optionFromList && hasFilterForThisKey && currentQueryKeyFilters[0]
? currentQueryKeyFilters[0]
: null;
// Build the selectedData object with proper fallback
const label =
optionFromList?.label ||
optionFromContext?.label ||
optionFromContext?.value ||
`All ${isMerchantDropdown ? "Merchants" : ""}`;
const value = optionFromList?.value || optionFromContext?.value || "all";
return {
...optionFromContext,
...optionFromList,
label,
value,
};
})();
const showAvatar = isMerchantDropdown && Boolean(selectedData.id);
const selectedLabel = selectedData.label;
const selectedImg = showAvatar ? (
<GiveThumbnail
imageUrl={addSizeToImage(selectedData.imageUrl || "", "thumb")}
size="icon"
type="merchant"
/>
) : undefined;
/**
* Handle selection of an option from the dropdown
* - Updates quickFilter for normal dropdowns
* - Updates contextQueries for merchant/processor dropdowns
* - Updates query string for API / URL
*/
const handleSelect = (option?: TOption) => {
Iif (!option) return;
// Check if the selected option is already applied
const isAlreadySelected = currentQueryKeyFilters.some(
(f: any) => f.value === option.value,
);
const nextFilters = isAlreadySelected ? [] : [option]; // Toggle selection
if (isMerchantOrProcessor) {
// Update contextQueries and full query string for merchant/processor dropdowns
const updatedQueryParams = { ...contextQueries, [queryKey]: nextFilters };
const fullQueryString = formatQueryParamsList(updatedQueryParams);
const quickFilterQueryString = buildQueryString({
option,
queryKey,
currentFilters: currentQueryKeyFilters,
otherValues: otherQueries,
});
dispatch(
setFilters({
queryParams: updatedQueryParams,
queryString: fullQueryString,
}),
);
setQueryStringWithQuickFilter(quickFilterQueryString);
} else {
// For normal dropdowns, just update the quickFilter query string
setQuickFilterValue(option.value === "all" ? null : option.value);
const queryString = buildQueryString({
option,
queryKey,
currentFilters: currentQueryKeyFilters,
otherValues: otherQueries,
});
setQueryStringWithQuickFilter(queryString);
}
};
const handleClear = () => {
if (!isMerchantOrProcessor) setQuickFilterValue(null);
if (isMerchantOrProcessor) {
const updatedQueryParams = { ...contextQueries, [queryKey]: [] };
const fullQueryString = formatQueryParamsList(updatedQueryParams);
const quickFilterQueryString = formatQueryParamsList({
...otherQueries,
[queryKey]: [],
});
dispatch(
setFilters({
queryParams: updatedQueryParams,
queryString: fullQueryString,
}),
);
setQueryStringWithQuickFilter(quickFilterQueryString);
} else {
const queryString = formatQueryParamsList({
...otherQueries,
[queryKey]: [],
});
setQueryStringWithQuickFilter(queryString);
}
};
const isFilterSelected = isMerchantOrProcessor
? hasFilterForThisKey
: selectedValue !== "all";
const menuOptions = useMemo(() => {
return options.map((option) => ({
text: option.label,
isSelected: option.value === selectedValue,
IconRight: option.value === selectedValue ? CheckIcon : undefined,
onClick: () => {
option.onClick?.(option); // call option's custom handler if exists
handleSelect(option); // apply selection
},
"data-testid": testIdMap[option.value],
Image: option.Image,
}));
}, [options, selectedValue, contextQueries]);
const showResetButton = isMerchantDropdown || isProcessorDropdown;
return {
options,
selectedValue,
selectedLabel,
selectedImg,
menuOptions,
handleSelect,
handleClear,
isFilterSelected,
showResetButton,
};
};
export const buildQueryString = ({
option,
queryKey,
currentFilters = [],
otherValues,
}: BuildQueryStringArgs) => {
Iif (option.value === "all") return "";
const isAlreadySelected = currentFilters.some(
(f) => f.value === option.value,
);
const nextFilters = isAlreadySelected ? [] : [option];
const queryString = formatQueryParamsList({
...otherValues,
[queryKey]: nextFilters,
});
return queryString;
};
|