Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: changing distribution type should show accurate percentage #4201

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import useActionFormBaseHook from '~v5/common/ActionSidebar/hooks/useActionFormB
import { useShowCreateStakedExpenditureModal } from '~v5/common/ActionSidebar/partials/CreateStakedExpenditureModal/hooks.tsx';
import { type ActionFormBaseProps } from '~v5/common/ActionSidebar/types.ts';

import { getSplitPaymentPayload } from './utils.ts';
import {
getSplitPaymentPayload,
getUnevenSplitPaymentTotalPercentage,
} from './utils.ts';

export const useValidationSchema = () => {
const { colony } = useColonyContext();
Expand Down Expand Up @@ -209,12 +212,12 @@ export const useValidationSchema = () => {
return true;
}

const sum = value.reduce(
(acc, curr) => acc + (curr?.percent || 0),
0,
const percentage = getUnevenSplitPaymentTotalPercentage(
Number(parent?.amount || 0),
value,
);

return sum === 100;
return percentage === 100;
},
)
.test(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import useWrapWithRef from '~hooks/useWrapWithRef.ts';
import { type ColonyContributor, type Token } from '~types/graphql.ts';
import { formatText } from '~utils/intl.ts';
import { getSelectedToken } from '~utils/tokens.ts';
import { getUnevenSplitPaymentTotalPercentage } from '~v5/common/ActionSidebar/partials/forms/SplitPaymentForm/utils.ts';
import UserSelect, {
type UserSearchSelectOption,
} from '~v5/common/ActionSidebar/partials/UserSelect/index.ts';
Expand Down Expand Up @@ -156,16 +157,10 @@ export const useRecipientsFieldTableColumns = ({
/>
{isTablet && (
<span className="text-md font-medium text-gray-900">
{parseFloat(
(
dataRef.current?.reduce(
(acc, _, index) =>
Number(acc) + Number(getPercentValue(index)),
0,
) || 0
).toFixed(4),
)}
%
{distributionMethodRef?.current ===
SplitPaymentDistributionType.Equal
? '100%'
: `${getUnevenSplitPaymentTotalPercentage(Number(amount || 0), dataRef.current)}%`}
</span>
)}
</div>
Expand Down Expand Up @@ -219,15 +214,7 @@ export const useRecipientsFieldTableColumns = ({
{distributionMethodRef?.current ===
SplitPaymentDistributionType.Equal
? '100%'
: `${Number(
dataRef.current
?.reduce(
(acc, _, index) =>
Number(acc) + Number(getPercentValue(index)),
0,
)
.toFixed(4) || 0,
)}%`}
: `${getUnevenSplitPaymentTotalPercentage(Number(amount || 0), dataRef.current)}%`}
</span>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { findDomainByNativeId } from '~utils/domains.ts';
import { getEnumValueFromKey } from '~utils/getEnumValueFromKey.ts';

import { type SplitPaymentFormValues } from './hooks.ts';
import { type SplitPaymentRecipientsFieldModel } from './partials/SplitPaymentRecipientsField/types.ts';

export const getSplitPaymentPayload = (
colony: Colony,
Expand Down Expand Up @@ -62,3 +63,21 @@ export const getSplitPaymentPayload = (
})) || [],
};
};

export const getUnevenSplitPaymentTotalPercentage = (
amount: number,
recipients: SplitPaymentRecipientsFieldModel[],
) => {
if (!amount) {
return 0;
}

const total =
recipients?.reduce((acc, recipient) => {
return acc + Number(recipient.amount);
}, 0) || 0;

const percentage = (100 * total) / amount;

return Number(percentage.toFixed(4));
};
Comment on lines +67 to +83
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.