Skip to content

Commit

Permalink
Merge branch 'epic/donation-amount-level-descriptions' into refactor/…
Browse files Browse the repository at this point in the history
…update-donation-type-settings
  • Loading branch information
JoshuaHungDinh committed Apr 15, 2024
2 parents 9143888 + 1750a6c commit 9623b44
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,16 @@ const numberOfDonationsControlOptions = [{label: __('Ongoing', 'give'), value: '
}))
);

/**
* @unreleased add description fields to levels.
* @since 3.0.0
*/
const Inspector = ({attributes, setAttributes}) => {
const {
label = __('Donation Amount', 'give'),
levels,
descriptions,
descriptionsEnabled = false,
defaultLevel,
priceOption,
setPrice,
Expand Down Expand Up @@ -121,9 +127,9 @@ 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()),
label: descriptions[index] || '',
value: level.toString(),
checked: defaultLevel === level,
}))
Expand Down Expand Up @@ -164,9 +170,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));
const newDescriptions = options.map((option) => option.label);

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

const getDefaultBillingPeriodOptions = useCallback(
Expand Down Expand Up @@ -219,6 +230,20 @@ const Inspector = ({attributes, setAttributes}) => {
defaultControlsTooltip={__('Default Level', 'give')}
/>
)}
{priceOption === 'multi' && (
<OptionsPanel
currency={currency}
multiple={false}
options={donationLevels}
setOptions={handleLevelsChange}
onAddOption={handleLevelAdded}
onRemoveOption={handleLevelRemoved}
defaultControlsTooltip={__('Default Level', 'give')}
toggleLabel={__('Enable amount description', 'give')}
toggleEnabled={descriptionsEnabled}
onHandleToggle={(value) => setAttributes({descriptionsEnabled: value})}
/>
)}
</PanelBody>
<PanelBody title={__('Custom Amount', 'give')} initialOpen={false}>
<ToggleControl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
setPrice,
priceOption,
levels,
descriptions,
defaultLevel,
} = getDefaultBlockAttributes('givewp/donation-amount');

Expand All @@ -39,6 +40,10 @@ const settings: FieldBlock['settings'] = {
type: 'array',
default: levels,
},
descriptions: {
type: 'array',
default: descriptions,
},
defaultLevel: {
type: 'number',
default: defaultLevel,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type {subscriptionPeriod} from "@givewp/forms/registrars/templates/groups/DonationAmount/subscriptionPeriod";
import type {subscriptionPeriod} from '@givewp/forms/registrars/templates/groups/DonationAmount/subscriptionPeriod';

export interface DonationAmountAttributes {
label: string;
levels: number[];
descriptions: string[];
descriptionsEnabled: boolean;
defaultLevel: number;
priceOption: string;
setPrice: number;
Expand All @@ -14,5 +16,5 @@ export interface DonationAmountAttributes {
recurringBillingPeriodOptions: subscriptionPeriod[];
recurringLengthOfTime: string;
recurringOptInDefaultBillingPeriod: subscriptionPeriod | 'one-time';
recurringEnableOneTimeDonations: boolean
}
recurringEnableOneTimeDonations: boolean;
}

0 comments on commit 9623b44

Please sign in to comment.