Skip to content

Commit

Permalink
Fix issue with not possible to buy tokens (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
ost-ptk authored Dec 9, 2024
1 parent e8cf324 commit 3015b33
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions src/apps/popup/pages/buy-cspr/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
IOnRampProvider,
IProviderSelectionData
} from 'casper-wallet-core/src/domain';
import React, { useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';

Expand Down Expand Up @@ -59,6 +59,7 @@ export const BuyCSPRPage = () => {
>([]);
const [selectedProvider, setSelectedProvider] =
useState<IOnRampProvider | null>(null);
const [providerUrl, setProviderUrl] = useState<string | null>(null);

const { t } = useTranslation();
const navigate = useTypedNavigate();
Expand All @@ -81,21 +82,28 @@ export const BuyCSPRPage = () => {
getOnRampProviderLocation
} = useGetOnRampProviders();

useEffect(() => {
if (onRampCountriesAndCurrenciesError) {
const handleError = useCallback(
(error: Error) => {
navigate(
ErrorPath,
createErrorLocationState({
errorHeaderText: t('Something went wrong'),
errorContentText:
onRampCountriesAndCurrenciesError.message ||
error.message ||
t(
'Please check browser console for error details, this will be a valuable for our team to fix the issue.'
),
errorPrimaryButtonLabel: t('Close'),
errorRedirectPath: RouterPath.Home
})
);
},
[navigate, t]
);

useEffect(() => {
if (onRampCountriesAndCurrenciesError) {
handleError(onRampCountriesAndCurrenciesError);
}
if (isLoadingOnRampCountriesAndCurrencies) return;

Expand All @@ -111,6 +119,7 @@ export const BuyCSPRPage = () => {
defaultCountry,
defaultCurrency,
defaultDepositAmount,
handleError,
isLoadingOnRampCountriesAndCurrencies,
navigate,
onRampCountriesAndCurrenciesError,
Expand Down Expand Up @@ -138,26 +147,12 @@ export const BuyCSPRPage = () => {
setBuyCSPRStep(BuyCSPRSteps.Provider);
}
},
onError: error => {
navigate(
ErrorPath,
createErrorLocationState({
errorHeaderText: t('Something went wrong'),
errorContentText:
error.message ||
t(
'Please check browser console for error details, this will be a valuable for our team to fix the issue.'
),
errorPrimaryButtonLabel: t('Close'),
errorRedirectPath: RouterPath.Home
})
);
}
onError: handleError
});
};

const handleSubmit = () => {
if (activeAccount && selectedProvider) {
useEffect(() => {
if (activeAccount?.publicKey && selectedProvider?.providerKey) {
const data: IProviderSelectionData = {
account: activeAccount.publicKey,
fiatCurrency: selectedCurrency.code,
Expand All @@ -169,27 +164,24 @@ export const BuyCSPRPage = () => {

getOnRampProviderLocation(data, {
onSuccess: providerLocation => {
window.open(providerLocation.location, '_blank');
setProviderUrl(providerLocation.location);
},
onError: error => {
console.error(error.message, 'provider selection request failed');

navigate(
ErrorPath,
createErrorLocationState({
errorHeaderText: t('Something went wrong'),
errorContentText:
error.message ||
t(
'Please check browser console for error details, this will be a valuable for our team to fix the issue.'
),
errorPrimaryButtonLabel: t('Close'),
errorRedirectPath: RouterPath.Home
})
);
}
onError: handleError
});
}
}, [
activeAccount?.publicKey,
selectedProvider?.providerKey,
selectedCurrency.code,
fiatAmount,
getOnRampProviderLocation,
handleError
]);

const handleSubmit = () => {
if (providerUrl) {
window.open(providerUrl, '_blank');
}
};

const content = {
Expand Down

0 comments on commit 3015b33

Please sign in to comment.