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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | 4158x 4158x 4158x 4158x 4158x 4158x 4158x 4158x 4158x 4158x 4158x 4158x 32x 4158x 30x 4158x 4158x 30x 4x 4x 4x 26x 26x 17x 17x 17x 9x 4158x 4158x 4158x 4158x 2x 2x 2x 2x 2x 4158x 4158x 4158x 138x 4158x 4158x 2x 6082x 16x 6082x 2x 4x 4x 11925x 11925x 939x 11925x 11925x 30x 11925x 29x 259x 4158x 36x 29x 11925x 29x 47x 47x 47x 47x 26x 21x 47x | import { Stack } from "@mui/material";
import { PlusIcon } from "@phosphor-icons/react";
import GiveChip from "@shared/Chip/GiveChip";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import GiveText from "@shared/Text/GiveText";
import { styled, useAppTheme } from "@theme/v2/Provider";
import {
CardPropsTypes,
FilterChip,
SelectorOption,
SelectSections,
} from "@pages/ManageMoney/Modal/filter.types";
import { cloneDeep, isArray } from "lodash";
import CustomSwitcher from "@pages/ManageMoney/Modal/CustomSwitcher";
import { useFormContext } from "react-hook-form";
import { customAmountArrayKey } from "@shared/GiveFilter/constants";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
import { useState } from "react";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import SelectedMerchantCard from "@shared/GiveFilter/components/SelectedMerchantCard";
import ChipLoading from "./ChipLoading";
import { CustomFilterType } from "@pages/ManageMoney/types";
import GiveSwitch from "@shared/Switch/GiveSwitch";
import GiveDivider from "@shared/GiveDivider/GiveDivider";
import { GiveToggleGroup } from "./GiveToggleGroup";
import { ClearSectionButton } from "./ClearSectionButton";
import GiveThumbnail from "@shared/Thumbnail/GiveThumbnail";
export default function FilterModalCard({
title,
contextualMenuProps,
chips,
apiKeyName,
type,
isLoading: isChipLoading,
toggleTitle,
switches,
showDivider,
toggleData,
additionalToggles,
useToggle,
}: CardPropsTypes) {
const { palette } = useAppTheme();
const {
watch,
setValue,
formState: { errors },
} = useFormContext();
const { isMobileView } = useCustomThemeV2();
const values = watch();
const isSingleAPIKey = typeof apiKeyName === "string";
const selectedValue = isSingleAPIKey ? values[apiKeyName] : "";
const customFieldsList = values[customAmountArrayKey] ?? [];
const isCustomChipValue = customFieldsList?.includes(apiKeyName);
const errorText = isSingleAPIKey
? errors?.[apiKeyName || ""]?.message || ""
: "";
const isMultipleSelection = isArray(selectedValue);
const shouldShowToggles = isMultipleSelection
? selectedValue.length > 0
: !!selectedValue;
const handleOnChange = (val: any) => {
setValue(apiKeyName as string, val, {
shouldValidate: true,
shouldDirty: true,
shouldTouch: true,
});
};
const handleToggleCustom = (isAdd?: boolean) => {
setValue(
customAmountArrayKey as string,
addOrRemove({
array: customFieldsList || [],
value: (apiKeyName as string) || "",
isAdd,
}),
);
};
const handleFieldChange = (
toggleApiKeyName: string,
val: any,
options: { shouldDirty?: boolean } = {},
) => {
setValue(toggleApiKeyName, val, {
shouldValidate: true,
shouldTouch: true,
...options,
});
};
const handleSelect = ({
isCustomChip,
chip,
isSelected,
}: {
chip: FilterChip;
isCustomChip: boolean;
isSelected: boolean;
}) => {
if (isCustomChip) {
// if it is a custom chip we need to update the useSTATE TO OPEN THE CUSTOM COMPONENT
handleToggleCustom();
handleOnChange("");
return;
}
// resetting custom type to nun on click of chip
handleToggleCustom(false);
// IF CIP IS A list if it accepts a list value we handle it here where we add it or remove if was added before
if (isMultipleSelection) {
const arr = addOrRemove({
array: selectedValue || [],
value: chip.value as string,
});
handleOnChange(arr);
return;
} // when its a string we set it here of reset it here
else handleOnChange(isSelected ? "" : chip.value);
};
const selectedItems = Array.isArray(selectedValue) ? selectedValue : [];
//contextual menu
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const handleClose = () => {
setAnchorEl(null);
};
const handleMenuMultiSelectObject = (
fieldName: SelectSections,
value: SelectorOption | SelectorOption[],
) => {
const currentValues: SelectorOption[] =
!!values[fieldName] && Array.isArray(values[fieldName])
? (values[fieldName] as SelectorOption[])
: [];
// replace form value if the value param is an array
Iif (Array.isArray(value)) {
setValue(fieldName, value, {
shouldValidate: true,
shouldDirty: true,
shouldTouch: true,
});
return;
}
Iif (
currentValues &&
currentValues.find((item) => item.value === value.value)
) {
const newValues = currentValues?.filter(
(item) => item.value !== value.value,
);
handleFieldChange(fieldName, newValues);
// Reset additional toggles when no providers are selected
if (
newValues.length === 0 &&
additionalToggles &&
additionalToggles.length > 0
) {
additionalToggles.forEach((toggle) => {
handleFieldChange(toggle.apiKeyName, null);
});
}
} else {
const vals = currentValues ? [...currentValues, value] : [value];
setValue(fieldName, vals, {
shouldValidate: true,
shouldDirty: true,
shouldTouch: true,
});
}
};
const { options, searchBarProps, clearSearch, onEndReached, isLoading } =
contextualMenuProps ?? {};
const toggleCondition = Array.isArray(selectedValue)
? selectedValue.length > 0
: Boolean(selectedValue);
const isToggleChecked =
Array.isArray(selectedItems) && selectedItems?.length > 0
? selectedItems?.some((item) => item?.value === toggleData?.value) //if its a customToggle we check it if the toggle data is included to the array
: toggleCondition;
const handleClearFromSection = (clearedKeys: string[]) => {
clearedKeys.forEach((key) => {
setValue(key, undefined);
});
};
return (
<Card data-testid={`${title}-card`}>
{isChipLoading ? (
<ChipLoading />
) : (
<>
{toggleTitle && (
<>
<Stack direction="column" gap="8px">
<Stack
direction="row"
alignItems="center"
justifyContent="space-between"
>
<GiveText variant="bodyS">{toggleTitle}</GiveText>
<GiveSwitch
checked={isToggleChecked}
onChange={() => {
if (toggleData) {
//if we have "custom"toggle with values we add to the selcted items array
handleMenuMultiSelectObject(
apiKeyName as SelectSections,
toggleData as SelectorOption,
);
} else if (selectedValue) {
handleOnChange(""); //if the toggle was on we set to "" to not filter by false value
} else handleOnChange(true);
}}
/>
</Stack>
</Stack>
{showDivider && <GiveDivider sx={{ mt: "16px", mb: "12px" }} />}
</>
)}
{switches ? (
<GiveToggleGroup
switches={switches}
label={title as string}
useToggle={useToggle}
endElement={
<ClearSectionButton
names={["canProcessMoney", "canTransferMoney"]}
onClear={handleClearFromSection}
/>
}
/>
) : (
<Stack
direction="row"
alignItems="center"
justifyContent="space-between"
>
{title && <GiveText variant="bodyS">{title}</GiveText>}
{options && (
<>
<GiveIconButton
Icon={PlusIcon}
onClick={(e: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(e.currentTarget);
}}
data-testid="plus-icon"
size="extraSmall"
/>
<ContextualMenu
anchorEl={anchorEl}
color={isMobileView ? "primary" : "tertiary"}
texture={isMobileView ? "solid" : "blurred"}
options={options?.map((option) => {
const isSelected = selectedItems?.some(
(item: any) => item?.value === option?.value,
);
return {
description: option?.description,
text: option.label,
...(!option.hideImage && {
Image: (
<GiveThumbnail
imageUrl={option?.icon}
shouldBeRounded={
contextualMenuProps?.iconShape === "rounded"
}
size="extra_extra_small"
type={option?.iconType || "avatar"}
/>
),
}),
value: option.value,
isSelected: isSelected,
onClick: () =>
handleMenuMultiSelectObject(
apiKeyName as SelectSections,
option as SelectorOption,
),
checkedIconType:
(contextualMenuProps?.checkedIcon as
| "Plus"
| "Check") ?? "Check",
};
})}
handleClose={handleClose}
searchBarProps={searchBarProps}
menuWidth={430}
onCloseEmptyState={clearSearch}
emptySection="empty-search"
onEndReached={onEndReached}
isLoading={isLoading}
isMultiSelect
/>
</>
)}
</Stack>
)}
{selectedItems?.length > 0 && contextualMenuProps
? selectedItems
?.filter(
//filter out if we have custom toggle selected, to not show in the UI
(item: SelectorOption) => item.value !== toggleData?.value,
)
.map((item: SelectorOption, idx: number) => {
return (
<SelectedMerchantCard
key={idx}
hideAvatar={item?.hideImage}
iconShape={contextualMenuProps?.iconShape}
item={item}
isFirst={idx === 0}
onClick={() =>
handleMenuMultiSelectObject(
apiKeyName as SelectSections,
item as SelectorOption,
)
}
/>
);
})
: null}
{chips && (
<Stack direction="row" gap="8px" marginTop="16px" flexWrap="wrap">
{chips.map((chip, index) => {
const isCustomChip = chip.value === "custom";
const isSelected = isCustomChip
? isCustomChipValue
: isArray(selectedValue)
? selectedValue.find((item) => item === chip.value) !==
undefined
: selectedValue === chip.value;
const customChipLabel = `${chip.label} ${
selectedValue
? `: ${selectedValue} ${
type === CustomFilterType.amountRange ? "USD" : ""
}`
: ""
} `;
const handleChange = () =>
handleSelect({ isCustomChip, chip, isSelected });
return (
<StyledChip
key={`${chip.label} - ${index}`}
size="large"
label={
isCustomChip && isSelected ? customChipLabel : chip.label
}
variant="light"
withCheckStartIcon
isSelected={isSelected}
color={isSelected ? "blue" : "default"}
onClick={handleChange}
data-testid={`filter-panel-chip-${
isCustomChip && isSelected ? customChipLabel : chip.label
}`}
checkIconProps={{
checkedColor: palette?.primitive?.blue[100],
unCheckedColor: palette?.border?.tertiary,
}}
/>
);
})}
</Stack>
)}
{type && isCustomChipValue && (
<CustomSelectionWrapper>
<CustomSwitcher
selectedValue={selectedValue}
customType={type}
onChange={handleOnChange}
errorText={errorText as string}
/>
</CustomSelectionWrapper>
)}
{shouldShowToggles && (
<AdditionalTogglesSection
additionalToggles={additionalToggles}
values={values}
handleFieldChange={handleFieldChange}
/>
)}
</>
)}
</Card>
);
}
interface AdditionalTogglesSectionProps {
additionalToggles?: CardPropsTypes["additionalToggles"];
values: Record<string, any>;
handleFieldChange: (
toggleApiKeyName: string,
val: any,
options?: any,
) => void;
}
export const AdditionalTogglesSection = ({
additionalToggles,
values,
handleFieldChange,
}: AdditionalTogglesSectionProps) => {
Eif (!additionalToggles?.length) return null;
return (
<>
<GiveDivider sx={{ mt: "16px", mb: "24px" }} bgType="primary" />
{additionalToggles.map((toggle, index) => {
const isToggleChecked = Boolean(values[toggle.apiKeyName]);
return (
<Stack
key={toggle.apiKeyName}
direction="column"
gap="8px"
sx={{ mb: index < additionalToggles.length - 1 ? "24px" : 0 }}
>
<Stack
direction="row"
alignItems="center"
justifyContent="space-between"
>
{toggle.toggleTitle}
<GiveSwitch
checked={isToggleChecked}
onChange={() => {
handleFieldChange(
toggle.apiKeyName,
values[toggle.apiKeyName] ? null : true,
);
}}
/>
</Stack>
</Stack>
);
})}
</>
);
};
const Card = styled(Stack)(({ theme }) => ({
padding: "16px",
borderRadius: "12px",
backgroundColor: theme.palette.surface?.primary,
}));
const CustomSelectionWrapper = styled(Stack)(({ theme }) => ({
paddingTop: "16px",
marginTop: "16px",
borderTop: `1px solid ${theme.palette.border?.primary}`,
}));
interface StyledChipProps {
isSelected?: boolean;
}
const StyledChip = styled(GiveChip)<StyledChipProps>(
({ theme, isSelected }) => ({
width: "fit-content",
height: "28px",
fontSize: "12px",
padding: "6px 12px 6px 3px",
"& span": {
lineHeight: "14px",
},
background: isSelected
? theme.palette.primitive?.blue[10]
: theme.palette.primitive?.transparent["darken-5"],
color: isSelected
? theme.palette.primitive?.blue[100]
: theme.palette.text.primary,
transition: "background 0.5s ease, color 0.5s ease",
}),
);
type AddOrRemoveParams = {
array: string[];
value: string;
isAdd?: boolean;
};
const addOrRemove = ({ array, value, isAdd }: AddOrRemoveParams) => {
const arr = cloneDeep(array);
const index = arr.indexOf(value);
Iif (isAdd === true) {
if (index === -1) arr.push(value); // Add only if not already present
} else if (isAdd === false) {
Iif (index !== -1) arr.splice(index, 1); // Remove only if present
} else {
// Toggle behavior: remove if exists, add if not
index !== -1 ? arr.splice(index, 1) : arr.push(value);
}
return arr;
};
|