Skip to content

Commit

Permalink
Refactor: remove extra zeros from amounts during the form migrations …
Browse files Browse the repository at this point in the history
…and display float numbers in V3 forms (#6993)

Co-authored-by: Jon Waldstein <[email protected]>
  • Loading branch information
glaubersilva and jonwaldstein authored Oct 5, 2023
1 parent 4cb4bff commit 33d4eeb
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __invoke(DonationAmountBlockModel $block, string $currency): Don

/** @var Amount $amountNode */
$amountNode = $group->getNodeByName('amount');
$defaultLevel = (float)$block->getDefaultLevel() > 0 ? (float)$block->getDefaultLevel() : 10;
$defaultLevel = $block->getDefaultLevel() > 0 ? $block->getDefaultLevel() : 10;
$amountNode
->label($block->getLabel())
->levels(...$block->getLevels())
Expand Down
14 changes: 8 additions & 6 deletions src/FormBuilder/BlockModels/DonationAmountBlockModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,22 @@ public function getLabel(): string

/**
* @since 3.0.0
*
* @return float[]
*/
public function getLevels(): array
{
return array_map('absint', $this->block->getAttribute('levels'));
return array_map(static function($level) {
return (float)filter_var($level, FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
}, $this->block->getAttribute('levels'));
}

/**
* @since 3.0.0
*
* @return string|null
*/
public function getDefaultLevel()
public function getDefaultLevel(): ?float
{
return $this->block->getAttribute('defaultLevel');
return (float)filter_var($this->block->getAttribute('defaultLevel'), FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
}

/**
Expand Down Expand Up @@ -213,4 +215,4 @@ public function getSetPrice(): int
{
return $this->block->getAttribute('setPrice');
}
}
}
14 changes: 7 additions & 7 deletions src/FormBuilder/resources/js/form-builder/src/blocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
"attributes": {
"label": "Donation Amount",
"levels": [
"10",
"25",
"50",
"100",
"250",
"500"
10,
25,
50,
100,
250,
500
],
"defaultLevel": "10",
"defaultLevel": 10,
"priceOption": "multi",
"setPrice": 25,
"customAmount": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import Notice from './notice';
import {getFormBuilderWindowData} from '@givewp/form-builder/common/getWindowData';
import {DonationAmountAttributes} from '@givewp/form-builder/blocks/fields/amount/types';

const DonationLevels = ({levels, defaultLevel}: {levels: DonationAmountAttributes['levels']; defaultLevel: string}) => (
const DonationLevels = ({levels, defaultLevel}: {levels: DonationAmountAttributes['levels']; defaultLevel: DonationAmountAttributes['defaultLevel']}) => (
<LevelGrid>
{levels.map((level: string, index: number) => {
const levelAmount = formatCurrencyAmount(level);
{levels.map((level, index) => {
const levelAmount = formatCurrencyAmount(level.toString());

return (
<LevelButton selected={level === defaultLevel} key={index}>
Expand All @@ -35,7 +35,7 @@ const FixedPriceMessage = ({amount}: {amount: string}) => (
</Notice>
);

const BillingPeriodControl = ({options, defaultSelected}: {options: string[], defaultSelected?: string}) => {
const BillingPeriodControl = ({options, defaultSelected}: {options: string[]; defaultSelected?: string}) => {
return (
<RadioControl
className={'give-billing-period-control'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ const Inspector = ({attributes, setAttributes}) => {

const [donationLevels, setDonationLevels] = useState<OptionProps[]>(
levels.map((level) => ({
label: formatCurrencyAmount(level),
value: level,
label: formatCurrencyAmount(level.toString()),
value: level.toString(),
checked: defaultLevel === level,
}))
);
Expand All @@ -138,13 +138,14 @@ const Inspector = ({attributes, setAttributes}) => {
const checkedLevel = options.filter((option) => option.checked);

if (!!checkedLevel && checkedLevel.length === 1) {
setAttributes({defaultLevel: checkedLevel[0].value});
setAttributes({defaultLevel: Number(checkedLevel[0].value)});
} else if (options.length > 0) {
options[0].checked = true;
}

setDonationLevels(options);
const newLevels = options.filter((option) => option.value).map((option) => option.value);
const newLevels = options.filter((option) => option.value).map((option) => Number(option.value));

setAttributes({levels: newLevels});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type {subscriptionPeriod} from "@givewp/forms/registrars/templates/groups

export interface DonationAmountAttributes {
label: string;
levels: string[];
defaultLevel: string;
levels: number[];
defaultLevel: number;
priceOption: string;
setPrice: number;
customAmount: boolean;
Expand Down
17 changes: 14 additions & 3 deletions src/FormMigration/Steps/DonationOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,28 @@ public function process()
// @note $formV2->levels only returns a single level
$donationLevels = $this->getMetaV2('_give_donation_levels');
// @note No corresponding setting in v3 for `_give_text` for Donation Levels.
$amountField->setAttribute('levels', wp_list_pluck($donationLevels, '_give_amount'));
$amountField->setAttribute('levels',
array_map([$this, 'roundAmount'], wp_list_pluck($donationLevels, '_give_amount')));
}

if($this->formV2->isCustomAmountOptionEnabled()) {
$amountField->setAttribute('customAmount', true);
$amountField->setAttribute('customAmountMin', $this->getMetaV2('_give_custom_amount_range_minimum'));
$amountField->setAttribute('customAmountMax', $this->getMetaV2('_give_custom_amount_range_maximum'));
$amountField->setAttribute('customAmountMin',
$this->roundAmount($this->getMetaV2('_give_custom_amount_range_minimum')));
$amountField->setAttribute('customAmountMax',
$this->roundAmount($this->getMetaV2('_give_custom_amount_range_maximum')));
} else {
$amountField->setAttribute('customAmount', false);
}

// @note No corresponding setting in v3 for "Custom Amount Text"
}

/**
* @unreleased
*/
private function roundAmount($amount): float
{
return round((float)$amount, 2);
}
}
2 changes: 1 addition & 1 deletion src/Framework/FieldsAPI/Amount.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Amount extends Field
*
* @since 3.0.0
*/
public function levels(int ...$levels): self
public function levels(float ...$levels): self
{
$this->levels = $levels;

Expand Down

0 comments on commit 33d4eeb

Please sign in to comment.