From eceae4a3a27644e8ef3e3f23670996a28e703a78 Mon Sep 17 00:00:00 2001 From: Johnny D Date: Thu, 14 Sep 2023 21:47:57 +1000 Subject: [PATCH] New edit cycle: Fix payout splits recipient not changing (#4064) --- .../PayoutsTable/modals/SwitchToUnlimitedModal.tsx | 4 ++++ .../PayoutsSection/hooks/usePayoutsTable.tsx | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditCyclePage/PayoutsSection/PayoutsTable/modals/SwitchToUnlimitedModal.tsx b/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditCyclePage/PayoutsSection/PayoutsTable/modals/SwitchToUnlimitedModal.tsx index 02c5b3aeef..72185c1ab0 100644 --- a/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditCyclePage/PayoutsSection/PayoutsTable/modals/SwitchToUnlimitedModal.tsx +++ b/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditCyclePage/PayoutsSection/PayoutsTable/modals/SwitchToUnlimitedModal.tsx @@ -2,7 +2,9 @@ import { Trans } from '@lingui/macro' import { Modal } from 'antd' import { ExternalLinkWithIcon } from 'components/ProjectDashboard/components/ui/ExternalLinkWithIcon' import { helpPagePath } from 'utils/routes' +import { ensureSplitsSumTo100Percent } from 'utils/v2v3/distributions' import { useEditCycleFormContext } from '../../../EditCycleFormContext' +import { usePayoutsTable } from '../../hooks/usePayoutsTable' export function SwitchToUnlimitedModal({ open, @@ -12,10 +14,12 @@ export function SwitchToUnlimitedModal({ onClose: VoidFunction }) { const { editCycleForm } = useEditCycleFormContext() + const { payoutSplits } = usePayoutsTable() const onOk = () => { editCycleForm?.setFieldsValue({ distributionLimit: undefined, + payoutSplits: ensureSplitsSumTo100Percent({ splits: payoutSplits }), }) onClose() } diff --git a/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditCyclePage/PayoutsSection/hooks/usePayoutsTable.tsx b/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditCyclePage/PayoutsSection/hooks/usePayoutsTable.tsx index 2e1dfecdcb..469d022b15 100644 --- a/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditCyclePage/PayoutsSection/hooks/usePayoutsTable.tsx +++ b/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditCyclePage/PayoutsSection/hooks/usePayoutsTable.tsx @@ -298,6 +298,7 @@ export const usePayoutsTable = () => { handlePayoutSplitAmountChanged({ editingPayoutSplit: newSplit, newAmount: parseFloat(newPayoutSplit.amount.value), + newSplits, }) } } @@ -308,13 +309,16 @@ export const usePayoutsTable = () => { * - Changed the % of other splits based on the new DL keep their amount the same * @param editingPayoutSplit - Split that has had its amount changed * @param newAmount - The new amount of the @editingPayoutSplit + * @param newSplits - (optional) pass new splits to adjust. If undefined, uses current @payoutSplits state */ function handlePayoutSplitAmountChanged({ editingPayoutSplit, newAmount, + newSplits, }: { editingPayoutSplit: Split newAmount: number + newSplits?: Split[] }) { const isNaN = Number.isNaN(newAmount) const _amount = isNaN @@ -335,9 +339,7 @@ export const usePayoutsTable = () => { const newSplitPercentPPB = round( (_amount / (newDistributionLimit ?? 0)) * ONE_BILLION, ) - - let adjustedSplits: Split[] = payoutSplits - + let adjustedSplits: Split[] = newSplits ?? payoutSplits // recalculate all split percents based on newly added split amount if (newDistributionLimit && !distributionLimitIsInfinite) { adjustedSplits = adjustedSplitPercents({