Skip to content

Commit

Permalink
feat: set amounts when choosing tradable credits
Browse files Browse the repository at this point in the history
  • Loading branch information
blushi committed Oct 1, 2024
1 parent 6013e4d commit ff1929c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ChangeEvent, useCallback, useEffect, useMemo, useState } from 'react';
import { useFormContext, useWatch } from 'react-hook-form';
import { msg, Trans } from '@lingui/macro';
import { msg, plural, Trans } from '@lingui/macro';
import { useLingui } from '@lingui/react';
import { useSetAtom } from 'jotai';
import { ChooseCreditsFormSchemaType } from 'web-marketplace/src/components/organisms/ChooseCreditsForm/ChooseCreditsForm.schema';

import { errorBannerTextAtom } from 'lib/atoms/error.atoms';
import { microToDenom } from 'lib/denom.utils';

import { PAYMENT_OPTIONS } from 'pages/BuyCredits/BuyCredits.constants';
Expand Down Expand Up @@ -44,7 +46,9 @@ export const CreditsAmount = ({
const { _ } = useLingui();

const [maxCreditsSelected, setMaxCreditsSelected] = useState(false);
const { setValue, trigger } = useFormContext<ChooseCreditsFormSchemaType>();
const { setValue, trigger, getValues } =
useFormContext<ChooseCreditsFormSchemaType>();
const setErrorBannerTextAtom = useSetAtom(errorBannerTextAtom);

const card = paymentOption === PAYMENT_OPTIONS.CARD;
const orderedSellOrders = useMemo(
Expand All @@ -59,24 +63,58 @@ export const CreditsAmount = ({
);

useEffect(() => {
setSpendingCap(
getSpendingCap(paymentOption, filteredCryptoSellOrders, cardSellOrders),
const _spendingCap = getSpendingCap(
paymentOption,
filteredCryptoSellOrders,
cardSellOrders,
);
setCreditsAvailable(
getCreditsAvailablePerCurrency(
paymentOption,
filteredCryptoSellOrders,
cardSellOrders,
creditTypePrecision,
),
setSpendingCap(_spendingCap);
const _creditsAvailable = getCreditsAvailablePerCurrency(
paymentOption,
filteredCryptoSellOrders,
cardSellOrders,
creditTypePrecision,
);
setCreditsAvailable(_creditsAvailable);

// This can happen when the user wants to buy only tradable credits,
// but the amount set is above the amount of tradable credits
// (all credits can be retired but are not necessarily all tradable based on disableAutoRetire value)
if (getValues(CREDITS_AMOUNT) > _creditsAvailable) {
setValue(CREDITS_AMOUNT, _creditsAvailable);
setValue(
CURRENCY_AMOUNT,
paymentOption === PAYMENT_OPTIONS.CARD
? _spendingCap
: microToDenom(_spendingCap),
);
setValue(
SELL_ORDERS,
orderedSellOrders.map(order => {
const price = getSellOrderPrice({ order, card });
return formatFullSellOrder({ order, card, price });
}),
);
setErrorBannerTextAtom(
plural(_creditsAvailable, {
one: `Only ${_creditsAvailable} credit available with those paramaters, order quantity changed`,
other: `Only ${_creditsAvailable} credits available with those paramaters, order quantity changed`,
}),
);
}
}, [
cardSellOrders,
filteredCryptoSellOrders,
paymentOption,
setCreditsAvailable,
setSpendingCap,
creditTypePrecision,
getValues,
setValue,
orderedSellOrders,
card,
setErrorBannerTextAtom,
_,
]);

// Max credits set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,7 @@ export function ChooseCreditsForm({

const handleCryptoPurchaseOptions = useCallback(() => {
setRetiring(prev => !prev);

// Reset amounts to 0 on retirement change
form.setValue(CREDITS_AMOUNT, 0);
form.setValue(CURRENCY_AMOUNT, 0);
form.trigger(); // trigger validation
}, [form, setRetiring]);
}, [setRetiring]);

const handlePaymentOptions = useCallback(
(option: string) => {
Expand Down
24 changes: 14 additions & 10 deletions web-marketplace/src/lib/i18n/locales/en.po
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
msgid ""
msgstr ""
"POT-Creation-Date: 2024-09-26 11:00+0200\n"
"POT-Creation-Date: 2024-10-01 12:25+0200\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
Expand Down Expand Up @@ -45,6 +45,10 @@ msgstr ""
msgid "(retirement is permanent and non-reversible)"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:99
msgid "{_creditsAvailable, plural, one {Only {_creditsAvailable} credit available with those paramaters, order quantity changed} other {Only {_creditsAvailable} credits available with those paramaters, order quantity changed}}"
msgstr ""

#: src/components/organisms/BasketOverview/BasketOverview.tsx:132
msgid "{0, plural, one {allowed credit class} other {allowed credit classes}}"
msgstr ""
Expand Down Expand Up @@ -705,7 +709,7 @@ msgstr ""
msgid "buy NCT"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:22
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:18
#: src/components/organisms/BuyCreditsModal/BuyCreditsModal.tsx:444
msgid "Buy tradable ecocredits"
msgstr ""
Expand Down Expand Up @@ -1167,7 +1171,7 @@ msgstr ""
msgid "credit name"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:183
#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:223
msgid "Credit prices vary. By default the lowest priced credits will be purchased first."
msgstr ""

Expand Down Expand Up @@ -2043,7 +2047,7 @@ msgstr ""
msgid "Make your purchase anonymous. Your name will be hidden from the retirement certificate and the certificate will be hidden from your public profile."
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:31
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:27
#: src/lib/constants/shared.constants.tsx:49
msgid "Max"
msgstr ""
Expand Down Expand Up @@ -2963,7 +2967,7 @@ msgstr ""
msgid "Retire all credits upon transfer"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:15
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:11
#: src/components/organisms/BuyCreditsModal/BuyCreditsModal.tsx:422
msgid "Retire credits now"
msgstr ""
Expand Down Expand Up @@ -3122,8 +3126,8 @@ msgstr ""
msgid "Select all"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:168
#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:169
#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:208
#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:209
msgid "Select option"
msgstr ""

Expand Down Expand Up @@ -3174,7 +3178,7 @@ msgstr ""
msgid "Sender"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:30
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:26
msgid "Set max credits"
msgstr ""

Expand Down Expand Up @@ -3523,15 +3527,15 @@ msgstr ""
msgid "These credits will be a tradable asset. They can be retired later via Regen Marketplace."
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:23
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:19
msgid "These credits will be a tradeable asset. They can be retired later via Regen Marketplace."
msgstr ""

#: src/components/organisms/BuyCreditsModal/BuyCreditsModal.tsx:425
msgid "These credits will be retired upon purchase and will not be tradable. Retirement is permanent and non-reversible."
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:16
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:12
msgid "These credits will be retired upon purchase and will not be tradeable. Retirement is permanent and non-reversible."
msgstr ""

Expand Down
24 changes: 14 additions & 10 deletions web-marketplace/src/lib/i18n/locales/es.po
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
msgid ""
msgstr ""
"POT-Creation-Date: 2024-09-26 11:00+0200\n"
"POT-Creation-Date: 2024-10-01 12:25+0200\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
Expand Down Expand Up @@ -45,6 +45,10 @@ msgstr ""
msgid "(retirement is permanent and non-reversible)"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:99
msgid "{_creditsAvailable, plural, one {Only {_creditsAvailable} credit available with those paramaters, order quantity changed} other {Only {_creditsAvailable} credits available with those paramaters, order quantity changed}}"
msgstr ""

#: src/components/organisms/BasketOverview/BasketOverview.tsx:132
msgid "{0, plural, one {allowed credit class} other {allowed credit classes}}"
msgstr ""
Expand Down Expand Up @@ -705,7 +709,7 @@ msgstr ""
msgid "buy NCT"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:22
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:18
#: src/components/organisms/BuyCreditsModal/BuyCreditsModal.tsx:444
msgid "Buy tradable ecocredits"
msgstr ""
Expand Down Expand Up @@ -1167,7 +1171,7 @@ msgstr ""
msgid "credit name"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:183
#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:223
msgid "Credit prices vary. By default the lowest priced credits will be purchased first."
msgstr ""

Expand Down Expand Up @@ -2043,7 +2047,7 @@ msgstr ""
msgid "Make your purchase anonymous. Your name will be hidden from the retirement certificate and the certificate will be hidden from your public profile."
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:31
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:27
#: src/lib/constants/shared.constants.tsx:49
msgid "Max"
msgstr ""
Expand Down Expand Up @@ -2963,7 +2967,7 @@ msgstr ""
msgid "Retire all credits upon transfer"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:15
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:11
#: src/components/organisms/BuyCreditsModal/BuyCreditsModal.tsx:422
msgid "Retire credits now"
msgstr ""
Expand Down Expand Up @@ -3122,8 +3126,8 @@ msgstr ""
msgid "Select all"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:168
#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:169
#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:208
#: src/components/molecules/CreditsAmount/CreditsAmount.tsx:209
msgid "Select option"
msgstr ""

Expand Down Expand Up @@ -3174,7 +3178,7 @@ msgstr ""
msgid "Sender"
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:30
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:26
msgid "Set max credits"
msgstr ""

Expand Down Expand Up @@ -3523,15 +3527,15 @@ msgstr ""
msgid "These credits will be a tradable asset. They can be retired later via Regen Marketplace."
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:23
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:19
msgid "These credits will be a tradeable asset. They can be retired later via Regen Marketplace."
msgstr ""

#: src/components/organisms/BuyCreditsModal/BuyCreditsModal.tsx:425
msgid "These credits will be retired upon purchase and will not be tradable. Retirement is permanent and non-reversible."
msgstr ""

#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:16
#: src/components/molecules/CreditsAmount/CreditsAmount.constants.ts:12
msgid "These credits will be retired upon purchase and will not be tradeable. Retirement is permanent and non-reversible."
msgstr ""

Expand Down

0 comments on commit ff1929c

Please sign in to comment.