From 33d4eeb09395fd596a76d15f56c1796c16cfb7b1 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 5 Oct 2023 13:42:19 -0300 Subject: [PATCH] Refactor: remove extra zeros from amounts during the form migrations and display float numbers in V3 forms (#6993) Co-authored-by: Jon Waldstein --- .../ConvertDonationAmountBlockToFieldsApi.php | 2 +- .../BlockModels/DonationAmountBlockModel.php | 14 ++++++++------ .../resources/js/form-builder/src/blocks.json | 14 +++++++------- .../src/blocks/fields/amount/Edit.tsx | 8 ++++---- .../blocks/fields/amount/inspector/index.tsx | 9 +++++---- .../src/blocks/fields/amount/types.ts | 4 ++-- src/FormMigration/Steps/DonationOptions.php | 17 ++++++++++++++--- src/Framework/FieldsAPI/Amount.php | 2 +- 8 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/DonationForms/Actions/ConvertDonationAmountBlockToFieldsApi.php b/src/DonationForms/Actions/ConvertDonationAmountBlockToFieldsApi.php index 6e2737e3b2..d6af3da28e 100644 --- a/src/DonationForms/Actions/ConvertDonationAmountBlockToFieldsApi.php +++ b/src/DonationForms/Actions/ConvertDonationAmountBlockToFieldsApi.php @@ -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()) diff --git a/src/FormBuilder/BlockModels/DonationAmountBlockModel.php b/src/FormBuilder/BlockModels/DonationAmountBlockModel.php index d42afb485c..22ac74d90d 100644 --- a/src/FormBuilder/BlockModels/DonationAmountBlockModel.php +++ b/src/FormBuilder/BlockModels/DonationAmountBlockModel.php @@ -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); } /** @@ -213,4 +215,4 @@ public function getSetPrice(): int { return $this->block->getAttribute('setPrice'); } -} \ No newline at end of file +} diff --git a/src/FormBuilder/resources/js/form-builder/src/blocks.json b/src/FormBuilder/resources/js/form-builder/src/blocks.json index 77d0e13453..c9ab2d63ea 100644 --- a/src/FormBuilder/resources/js/form-builder/src/blocks.json +++ b/src/FormBuilder/resources/js/form-builder/src/blocks.json @@ -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, diff --git a/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/Edit.tsx b/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/Edit.tsx index 199d97213a..21786950f5 100644 --- a/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/Edit.tsx +++ b/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/Edit.tsx @@ -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']}) => ( - {levels.map((level: string, index: number) => { - const levelAmount = formatCurrencyAmount(level); + {levels.map((level, index) => { + const levelAmount = formatCurrencyAmount(level.toString()); return ( @@ -35,7 +35,7 @@ const FixedPriceMessage = ({amount}: {amount: string}) => ( ); -const BillingPeriodControl = ({options, defaultSelected}: {options: string[], defaultSelected?: string}) => { +const BillingPeriodControl = ({options, defaultSelected}: {options: string[]; defaultSelected?: string}) => { return ( { const [donationLevels, setDonationLevels] = useState( levels.map((level) => ({ - label: formatCurrencyAmount(level), - value: level, + label: formatCurrencyAmount(level.toString()), + value: level.toString(), checked: defaultLevel === level, })) ); @@ -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}); }; diff --git a/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/types.ts b/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/types.ts index b89918574b..2a0d945172 100644 --- a/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/types.ts +++ b/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/types.ts @@ -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; diff --git a/src/FormMigration/Steps/DonationOptions.php b/src/FormMigration/Steps/DonationOptions.php index 010dcf69ec..7047fe1727 100644 --- a/src/FormMigration/Steps/DonationOptions.php +++ b/src/FormMigration/Steps/DonationOptions.php @@ -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); + } } diff --git a/src/Framework/FieldsAPI/Amount.php b/src/Framework/FieldsAPI/Amount.php index 7aeff6fa6a..bbdc891ea4 100644 --- a/src/Framework/FieldsAPI/Amount.php +++ b/src/Framework/FieldsAPI/Amount.php @@ -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;