Skip to content

Commit

Permalink
Merge pull request #952 from Koniverse/issue-920
Browse files Browse the repository at this point in the history
[issue-920] Support sort token by value
  • Loading branch information
nguyenduythuc authored Aug 16, 2023
2 parents a45b501 + 8411721 commit fe5f5a5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/screens/Home/Crowdloans/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect } from 'react';
import React, { useCallback, useEffect, useMemo } from 'react';
import i18n from 'utils/i18n/i18n';
import { ListRenderItemInfo } from 'react-native';
import { CrowdloanItem } from 'screens/Home/Crowdloans/CrowdloanItem';
Expand Down Expand Up @@ -44,6 +44,14 @@ export const CrowdloansScreen = () => {
{ label: i18n.filterOptions.win, value: FilterValue.WINNER },
{ label: i18n.filterOptions.fail, value: FilterValue.FAIL },
];
const crowdloanData = useMemo(() => {
const result = items.sort(
// @ts-ignore
(firstItem, secondItem) => secondItem.convertedContribute - firstItem.convertedContribute,
);

return result;
}, [items]);

useEffect(() => {
if (isFocused) {
Expand Down Expand Up @@ -85,7 +93,7 @@ export const CrowdloansScreen = () => {
renderListEmptyComponent={renderListEmptyComponent}
renderItem={renderItem}
autoFocus={false}
items={items}
items={crowdloanData}
showLeftBtn={false}
searchFunction={doFilterOptions}
filterOptions={defaultFilterOpts}
Expand Down
14 changes: 11 additions & 3 deletions src/screens/Home/Crypto/TokenGroups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,16 @@ export const TokenGroups = () => {
);

const tokenGroupBalanceItems = useMemo<TokenBalanceItemType[]>(() => {
const result: TokenBalanceItemType[] = [];
const balanceItemData: TokenBalanceItemType[] = [];

sortedTokenGroups.forEach(tokenGroupSlug => {
if (tokenGroupBalanceMap[tokenGroupSlug]) {
result.push(tokenGroupBalanceMap[tokenGroupSlug]);
balanceItemData.push(tokenGroupBalanceMap[tokenGroupSlug]);
}
});
const result = balanceItemData
// @ts-ignore
.sort((firstItem, secondItem) => secondItem.total.convertedValue - firstItem.total.convertedValue);

return result;
}, [sortedTokenGroups, tokenGroupBalanceMap]);
Expand Down Expand Up @@ -185,7 +188,12 @@ export const TokenGroups = () => {
}
});

return items;
const result = items
// @ts-ignore
.sort((firstItem, secondItem) => firstItem.total.convertedValue - secondItem.total.convertedValue)
.reverse();

return result;
}, [sortedTokenSlugs, tokenBalanceMap]);

const listHeaderNode = useMemo(() => {
Expand Down
6 changes: 5 additions & 1 deletion src/screens/Home/Crypto/TokenGroupsDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ export const TokenGroupsDetail = ({
}
});

return items;
return (
items
// @ts-ignore
.sort((firstItem, secondItem) => secondItem.total.convertedValue - firstItem.total.convertedValue)
);
}

if (tokenBalanceMap[tokenGroupSlug]) {
Expand Down
23 changes: 22 additions & 1 deletion src/screens/Home/Staking/Balance/StakingBalanceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { StakingType } from '@subwallet/extension-base/background/KoniTypes';
import { RootNavigationProps } from 'routes/index';
import { EmptyList } from 'components/EmptyList';
import { setAdjustPan } from 'rn-android-keyboard-adjust';
import BigNumber from 'bignumber.js';

enum FilterValue {
NOMINATED = 'nominated',
Expand Down Expand Up @@ -84,6 +85,26 @@ const StakingBalanceList = () => {
{ label: i18n.filterOptions.nominated, value: FilterValue.NOMINATED },
{ label: i18n.filterOptions.pooled, value: FilterValue.POOLED },
];
const stakingList = useMemo(() => {
console.log(data);
if (!data.length) {
return [];
}
const BN_TEN = new BigNumber(10);
const result = data.sort((firstItem, secondItem) => {
const firstPrice = priceMap[`${firstItem.staking.chain}`] || 0;
const firstValue =
// @ts-ignore
new BigNumber(firstItem.staking.balance).dividedBy(BN_TEN.pow(firstItem.decimals)).toFixed() * firstPrice;
const secondPrice = priceMap[`${secondItem.staking.chain}`] || 0;
const secondValue =
// @ts-ignore
new BigNumber(secondItem.staking.balance).dividedBy(BN_TEN.pow(secondItem.decimals)).toFixed() * secondPrice;
console.log(firstValue, secondValue);
return secondValue - firstValue;
});
return result;
}, [data, priceMap]);

const isFocused = useIsFocused();
useEffect(() => {
Expand Down Expand Up @@ -131,7 +152,7 @@ const StakingBalanceList = () => {
style={{ flex: 1, paddingBottom: 16 }}
title={i18n.header.staking}
titleTextAlign={'left'}
items={data}
items={stakingList}
showLeftBtn={false}
placeholder={i18n.placeholder.searchToken}
autoFocus={false}
Expand Down

0 comments on commit fe5f5a5

Please sign in to comment.