Skip to content

Commit

Permalink
Fix/timeout on get balances (#572)
Browse files Browse the repository at this point in the history
* Fixed error parsing to take into account Error in amount field

* Changed fetchAssets to take into account assets loading
  • Loading branch information
angarita-dev authored Aug 23, 2022
1 parent 4ffdffe commit 900d8d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
6 changes: 1 addition & 5 deletions source/components/AssetItem/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ const AssetItem = ({
updateToken, logo, name, amount, value, symbol, loading, failed, assetNameTestId,
}) => {
const classes = useStyles();
const [fetchLoading, setFetchLoading] = useState(false);
const { t } = useTranslation();
const { currentNetwork, usingMainnet } = useSelector((state) => state.network);

const handleFetchAssets = async () => {
// Avoid calling multiple times
if (fetchLoading) return;
if (loading) return;

setFetchLoading(true);
await updateToken();
setFetchLoading(false);
};
const ledgerNotSpecified = !usingMainnet && !currentNetwork?.ledgerCanisterId;
return (
Expand Down Expand Up @@ -79,7 +76,6 @@ const AssetItem = ({
: (<NumberFormat value={value} displayType="text" decimalScale={2} fixedDecimalScale thousandSeparator="," prefix="$" />)}
</Typography>
)}

</div>

);
Expand Down
18 changes: 15 additions & 3 deletions source/components/Tokens/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,31 @@ const Tokens = () => {
const { navigator } = useRouter();
const { onScroll, fullScroll } = useScroll();

const fetchAssets = () => {
const fetchAssets = (cb = () => {}) => {
sendMessage({
type: HANDLER_TYPES.GET_ASSETS,
params: {},
}, (keyringAssets) => {
cb(keyringAssets);
dispatch(setAssets({ keyringAssets, icpPrice }));
dispatch(setAssetsLoading(false));
setLoading(false);
});
};

const handleFetchAssets = () => {
dispatch(setAssetsLoading(true));
setLoading(true);

return new Promise((resolve) => {
fetchAssets(resolve);
});
};

useEffect(() => {
const id = setInterval(fetchAssets, 15000);
const id = setInterval(() => {
!assetsLoading && fetchAssets();
}, 15000);
fetchAssets();
return () => clearInterval(id);
}, [icpPrice]);
Expand Down Expand Up @@ -66,7 +78,7 @@ const Tokens = () => {
<AssetItem
{...asset}
key={`${asset.symbol}-${asset.canisterId}-${currentNetwork?.id}`}
updateToken={fetchAssets}
updateToken={handleFetchAssets}
loading={loading}
failed={!!asset?.error}
assetNameTestId="asset-name"
Expand Down
4 changes: 3 additions & 1 deletion source/shared/constants/currencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,16 @@ export const parseToBigIntString = (amount, decimalPlaces) => {
export const parseAssetsAmount = (assets = []) => (
assets.map((currentAsset) => {
const { amount, token, error } = currentAsset;
const hasError = error || amount === 'Error';
const { decimals } = token;
const parsedAmount = error
const parsedAmount = hasError
? 0
: parseToFloatAmount(amount, parseInt(decimals?.toString(), 10));

return {
...currentAsset,
amount: parsedAmount,
error: hasError,
};
})
);
Expand Down

0 comments on commit 900d8d4

Please sign in to comment.