Skip to content

Commit

Permalink
refactor: update donation amount settings to handle descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaHungDinh committed Apr 15, 2024
1 parent d883382 commit 98f4b7c
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const Inspector = ({attributes, setAttributes}) => {
const {
label = __('Donation Amount', 'give'),
levels,
descriptions,
defaultLevel,
priceOption,
setPrice,
Expand Down Expand Up @@ -120,11 +121,12 @@ const Inspector = ({attributes, setAttributes}) => {
const isRecurring = isRecurringSupported && recurringEnabled;

const [donationLevels, setDonationLevels] = useState<OptionProps[]>(
levels.map((level) => ({
levels.map((level, index) => ({
id: String(Math.floor(Math.random() * 1000000)),
label: formatCurrencyAmount(level.toString()),
value: level.toString(),
checked: defaultLevel === level,
description: descriptions[index] || '',
}))
);

Expand Down Expand Up @@ -163,9 +165,14 @@ const Inspector = ({attributes, setAttributes}) => {
const handleLevelsChange = (options: OptionProps[]) => {
const checkedLevel = options.filter((option) => option.checked);
const newLevels = options.filter((option) => option.value).map((option) => Number(option.value));

setDonationLevels(options);
setAttributes({levels: newLevels, defaultLevel: Number(checkedLevel[0].value)});
const newDescriptions = options.map((option) => option.description);

setDonationLevels(donationLevels);
setAttributes({
levels: newLevels,
defaultLevel: Number(checkedLevel[0].value),
descriptions: newDescriptions,
});
};

const getDefaultBillingPeriodOptions = useCallback(
Expand Down Expand Up @@ -220,6 +227,18 @@ const Inspector = ({attributes, setAttributes}) => {
onValueChange={(setPrice) => setAttributes({setPrice: setPrice ? parseInt(setPrice) : 0})}
/>
)}
{priceOption === 'multi' && (
<OptionsPanel
currency={currency}
multiple={false}
options={donationLevels}
setOptions={handleLevelsChange}
onAddOption={handleLevelAdded}
onRemoveOption={handleLevelRemoved}
defaultControlsTooltip={__('Default Level', 'give')}
hasDescriptions={!!descriptions}
/>
)}
</PanelBody>
<PanelBody title={__('Custom Amount', 'give')} initialOpen={false}>
<ToggleControl
Expand Down Expand Up @@ -248,20 +267,6 @@ const Inspector = ({attributes, setAttributes}) => {
)}
</PanelBody>

{priceOption === 'multi' && (
<PanelBody title={__('Donation Levels', 'give')} initialOpen={false}>
<OptionsPanel
currency={currency}
multiple={false}
options={donationLevels}
setOptions={handleLevelsChange}
onAddOption={handleLevelAdded}
onRemoveOption={handleLevelRemoved}
defaultControlsTooltip={__('Default Level', 'give')}
/>
</PanelBody>
)}

<PanelBody title={__('Recurring Donations', 'give')} initialOpen={false}>
{!isRecurringSupported &&
(recurringAddonData.isInstalled ? (
Expand Down

0 comments on commit 98f4b7c

Please sign in to comment.