Skip to content

Commit

Permalink
fix: filter sell orders by project
Browse files Browse the repository at this point in the history
  • Loading branch information
r41ph committed Dec 12, 2024
1 parent dfd9164 commit 6e61e1f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 64 deletions.
127 changes: 68 additions & 59 deletions web-marketplace/src/pages/BuyCredits/BuyCredits.Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,73 +270,82 @@ export const BuyCreditsForm = ({
stripe?: Stripe | null,
elements?: StripeElements | null,
) => {
const sellOrders = await refetchSellOrders();
const creditsInAllSellOrders = getSellOrdersCredits(sellOrders);
const creditsToBuy = data?.creditsAmount;
const requestedSellOrders = findMatchingSellOrders(
data,
sellOrders?.map(normalizeToUISellOrderInfo),
);
const creditsInRequestedSellOrders =
getSellOrdersCredits(requestedSellOrders);

const sellCanProceed =
creditsToBuy && creditsToBuy <= creditsInRequestedSellOrders;

if (sellCanProceed) {
const { retirementReason, country, stateProvince, postalCode } = values;
const {
sellOrders: selectedSellOrders,
savePaymentMethod,
createAccount: createActiveAccount,
// subscribeNewsletter, TODO
// followProject,
} = data;

if (selectedSellOrders && creditsAmount)
purchase({
selectedSellOrders,
retiring,
retirementReason,
country,
stateProvince,
postalCode,
savePaymentMethod,
createActiveAccount,
paymentMethodId,
stripe,
elements,
confirmationTokenId,
});
} else {
setWarningModalState({
openModal: true,
creditsAvailable: creditsInRequestedSellOrders,
});
warningModalContent.current = getWarningModalContent(
currency,
isWeb2UserWithoutWallet,
creditsInRequestedSellOrders,
_,
allowedDenomsData,
if (project) {
const sellOrders = await refetchSellOrders();
const creditsInAllSellOrders = getSellOrdersCredits(
sellOrders,
project.id,
);
const creditsToBuy = data?.creditsAmount;
const requestedSellOrders = findMatchingSellOrders(
data,
creditsInAllSellOrders,
isVisitingUser,
sellOrders?.map(normalizeToUISellOrderInfo),
);
const creditsInRequestedSellOrders = getSellOrdersCredits(
requestedSellOrders,
project.id,
);

const sellCanProceed =
creditsToBuy && creditsToBuy <= creditsInRequestedSellOrders;

if (sellCanProceed) {
const { retirementReason, country, stateProvince, postalCode } =
values;
const {
sellOrders: selectedSellOrders,
savePaymentMethod,
createAccount: createActiveAccount,
// subscribeNewsletter, TODO
// followProject,
} = data;

if (selectedSellOrders && creditsAmount)
purchase({
selectedSellOrders,
retiring,
retirementReason,
country,
stateProvince,
postalCode,
savePaymentMethod,
createActiveAccount,
paymentMethodId,
stripe,
elements,
confirmationTokenId,
});
} else {
setWarningModalState({
openModal: true,
creditsAvailable: creditsInRequestedSellOrders,
});
warningModalContent.current = getWarningModalContent(
currency,
isWeb2UserWithoutWallet,
creditsInRequestedSellOrders,
_,
allowedDenomsData,
data,
creditsInAllSellOrders,
isVisitingUser,
);
}
}
},
[
_,
allowedDenomsData,
confirmationTokenId,
currency,
creditsAmount,
project,
refetchSellOrders,
data,
isWeb2UserWithoutWallet,
paymentMethodId,
creditsAmount,
purchase,
refetchSellOrders,
retiring,
paymentMethodId,
confirmationTokenId,
currency,
isWeb2UserWithoutWallet,
_,
allowedDenomsData,
isVisitingUser,
],
);
Expand Down
16 changes: 11 additions & 5 deletions web-marketplace/src/pages/BuyCredits/BuyCredits.utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,17 @@ export const getWarningModalContent = (

export const getSellOrdersCredits = (
sellOrders: UISellOrderInfo[] | SellOrderInfoExtented[] | undefined,
) =>
sellOrders?.reduce(
(credits, sellOrder) => credits + Number(sellOrder.quantity),
0,
) || 0;
projectId: string,
) => {
return (
sellOrders
?.filter(sellOrder => sellOrder?.batchDenom?.startsWith(projectId))
.reduce(
(credits, sellOrder) => credits + Number(sellOrder.quantity),
0,
) || 0
);
};

const getStep2Name = (card: boolean) =>
card ? i18n._(PAYMENT_INFO) : i18n._(CUSTOMER_INFO);
Expand Down

0 comments on commit 6e61e1f

Please sign in to comment.