From 6e61e1fe319d9623a249e7e4f519a63a2e975946 Mon Sep 17 00:00:00 2001 From: r41ph Date: Thu, 12 Dec 2024 17:10:15 +0000 Subject: [PATCH] fix: filter sell orders by project --- .../src/pages/BuyCredits/BuyCredits.Form.tsx | 127 ++++++++++-------- .../src/pages/BuyCredits/BuyCredits.utils.tsx | 16 ++- 2 files changed, 79 insertions(+), 64 deletions(-) diff --git a/web-marketplace/src/pages/BuyCredits/BuyCredits.Form.tsx b/web-marketplace/src/pages/BuyCredits/BuyCredits.Form.tsx index 834f2515d5..0609b80a19 100644 --- a/web-marketplace/src/pages/BuyCredits/BuyCredits.Form.tsx +++ b/web-marketplace/src/pages/BuyCredits/BuyCredits.Form.tsx @@ -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, ], ); diff --git a/web-marketplace/src/pages/BuyCredits/BuyCredits.utils.tsx b/web-marketplace/src/pages/BuyCredits/BuyCredits.utils.tsx index 8a6fe3fd5d..ab7c1d44bd 100644 --- a/web-marketplace/src/pages/BuyCredits/BuyCredits.utils.tsx +++ b/web-marketplace/src/pages/BuyCredits/BuyCredits.utils.tsx @@ -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);