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 | 45x 12x 12x 12x 12x 12x 12x 12x 12x 12x 32x 4x 4x 4x 1x 1x 4x 4x 11x 11x 5x 5x 2x 2x 3x 3x 6x 6x 18x 6x 11x 8x 22x 8x 3x 5x 4x 4x 2x 5x 1x 2x 6x 12x 12x 12x 32x 5x 5x 5x 5x 3x 3x 3x 45x 35x | import { Stack } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import { HandCoinsIcon } from "@phosphor-icons/react";
import { VALIDATION_MESSAGES } from "@validation/messages";
import HFGiveCustomAmount from "@shared/HFInputs/HFGiveCustomAmount/HFGiveCustomAmount";
import HFGiveSwitch from "@shared/HFInputs/HFGiveSwitch/HFGiveSwitch";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import HFGiveSelect from "@shared/HFInputs/HFGiveSelect/HFGiveSelect";
import { recurringOptions } from "@sections/PayBuilder/items/AddProductItemModal";
import { Controller } from "react-hook-form";
import GiveCheckbox from "@shared/GiveCheckbox/GiveCheckbox";
import { compact, isEmpty, uniq } from "lodash";
import { useEffect } from "react";
import { convertToNumber } from "@sections/PayBuilder/utils";
const AmountsSection = () => {
const { methods } = usePayBuilderForm();
const { clearErrors, setError, watch, setValue } = methods;
const optionalAmounts = watch("Donations.optionalAmounts");
const isSuggestedAmountEnabled = watch("Donations.enableSuggestedAmounts");
const isCustomAmountAllowed = watch("Donations.allowCustomAmount");
const suggestedAmount = watch("Donations.suggestedAmount");
const isRecurringDonationEnabled = watch(
"Donations.enableRecurringDonations",
);
const selectedItems = watch("Donations.recurringIntervals");
const onHandleBlur =
(index: number) =>
({ name }: { name: string }) => {
Iif (!optionalAmounts) return;
const compactOptionalAmounts = compact(optionalAmounts);
// When user removes item that is selected, we should automatically set it to first available value
if (
compactOptionalAmounts.length &&
!compactOptionalAmounts.includes(suggestedAmount)
) {
const firstItem = compactOptionalAmounts[0];
setValue("Donations.suggestedAmount", firstItem);
}
const amountAtInputIndex = optionalAmounts?.[index];
optionalAmounts.forEach((amount, idx, array) => {
let isError = false;
if (compact(array)?.length < 3) {
const isValue = !!amountAtInputIndex;
if (isValue) {
isError = false;
clearErrors(name as any);
} else {
isError = true;
setError(name as any, {
message: VALIDATION_MESSAGES.REQUIRED,
});
}
} else Eif (compact(array)?.length >= 3) {
const isDuplicated =
array.filter(
(value) => convertToNumber(value) === convertToNumber(amount),
).length > 1;
if (!isDuplicated) clearErrors(`Donations.optionalAmounts.${idx}`);
}
if (isError) return;
const isDuplicated =
array.filter(
(value) =>
convertToNumber(value) === convertToNumber(amountAtInputIndex),
).length > 1;
if (isDuplicated && !!amountAtInputIndex) {
setError(name as any, {
message: "Value already exists",
});
} else {
clearErrors(name as any);
}
});
// Check for 2 or more values that are less than or equal to 0
const numericValues = compact(
compactOptionalAmounts.map(convertToNumber),
);
// If there are less than 3 numeric values, set errors on empty fields
if (numericValues.length < 3) {
optionalAmounts.forEach((amount, idx) => {
if (!amount) {
setError(`Donations.optionalAmounts.${idx}`, {
message: VALIDATION_MESSAGES.REQUIRED,
});
}
});
} else {
optionalAmounts.forEach((_, idx) => {
clearErrors(`Donations.optionalAmounts.${idx}`);
});
}
};
useEffect(() => {
Iif (!suggestedAmount && isSuggestedAmountEnabled) {
setValue("Donations.suggestedAmount", optionalAmounts.filter(Boolean)[0]);
}
}, [suggestedAmount, isSuggestedAmountEnabled]);
return (
<Stack gap="12px">
<Stack direction="row" spacing={2}>
<HandCoinsIcon size={24} />
<GiveText variant="bodyL">Amounts</GiveText>
</Stack>
<Stack paddingLeft="32px" gap="12px">
<SwitchWithLabel
name="Donations.allowCustomAmount"
label="Enable Preset Amounts"
/>
{isCustomAmountAllowed &&
optionalAmounts.map((_, index) => {
return (
<HFGiveCustomAmount
key={index}
name={`Donations.optionalAmounts.${index}`}
currency="usd"
onHandleBlur={onHandleBlur(index)}
/>
);
})}
{isCustomAmountAllowed && (
<SwitchWithLabel
name="Donations.enableSuggestedAmounts"
label="Enable Suggested Amount"
disabled={isEmpty(uniq(compact(optionalAmounts)))}
/>
)}
{/* AND all are mepty */}
{isSuggestedAmountEnabled && isCustomAmountAllowed && (
<HFGiveSelect
name="Donations.suggestedAmount"
disabled={isEmpty(uniq(compact(optionalAmounts)))}
options={optionalAmounts
.filter((item: any) => {
Iif (!item) return false; // Ensure item is truthy
const val = parseFloat(item.replace(/,/g, ""));
return val >= 1;
})
.map((item: string) => ({
label: item,
value: item,
}))}
/>
)}
<SwitchWithLabel
name="Donations.enableRecurringDonations"
label="Enable Recurring Donations"
/>
{isRecurringDonationEnabled &&
// remove one time option
recurringOptions
.slice(1)
.map(({ value, label }: { value: string; label: string }) => {
const checked = selectedItems.includes(value);
return (
<Stack key={value} direction="row" spacing={1}>
<Controller
control={methods.control}
name={`Donations.recurringIntervals` as any}
render={() => {
return (
<GiveCheckbox
checked={checked}
onChange={(event) => {
const newValue = event.target.checked
? [...selectedItems, value]
: selectedItems.filter((item) => item !== value);
methods.setValue(
"Donations.recurringIntervals",
newValue,
);
}}
/**
* User should have at least one option selected and since the one_time option is included
* in the recurrence options's array but can't be removed, the minimal length is 2
*/
disabled={checked && selectedItems.length === 2}
/>
);
}}
/>
<GiveText
color="secondary"
variant="bodyS"
alignItems="center"
>
{label}
</GiveText>
</Stack>
);
})}
</Stack>
</Stack>
);
};
const SwitchWithLabel = ({
name,
label,
disabled = false,
}: {
name: string;
label: string;
disabled?: boolean;
}) => {
return (
<Stack direction="row" spacing={1}>
<HFGiveSwitch disabled={disabled} name={name} />
<GiveText color="primary" variant="bodyS">
{label}
</GiveText>
</Stack>
);
};
export default AmountsSection;
|