From 3d506cfa015287cc2514cb533ed9bb56895e8df2 Mon Sep 17 00:00:00 2001 From: fmtabbara Date: Thu, 12 Oct 2023 16:12:03 +0100 Subject: [PATCH] use switch statement for key mapping --- nym-wallet/src/hooks/useSortDelegations.tsx | 23 +++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/nym-wallet/src/hooks/useSortDelegations.tsx b/nym-wallet/src/hooks/useSortDelegations.tsx index dfa7adbe2a..b2bdcce787 100644 --- a/nym-wallet/src/hooks/useSortDelegations.tsx +++ b/nym-wallet/src/hooks/useSortDelegations.tsx @@ -2,16 +2,27 @@ import { orderBy as _orderBy } from 'lodash'; import { Order, SortingKeys } from 'src/components/Delegation/DelegationList'; import { TDelegations, isDelegation } from 'src/context/delegations'; +type MappedTypes = 'delegationValue' | 'operatorReward' | 'profitMarginValue' | 'operatorCostValue'; + export const useSortDelegations = (delegationItems: TDelegations, order: Order, orderBy: SortingKeys) => { const unbondedDelegations = delegationItems.filter((delegation) => !delegation.node_identity); const delegations = delegationItems.filter((delegation) => delegation.node_identity); - const mapOrderBy = (key: SortingKeys) => { - if (key === 'amount') return 'delegationValue'; - if (key === 'unclaimed_rewards') return 'operatorReward'; - if (key === 'profit_margin_percent') return 'profitMarginValue'; - if (key === 'operating_cost') return 'operatorCostValue'; - return key; + // example of a mapped type in typescript + + const mapOrderBy = (key: SortingKeys): MappedTypes | SortingKeys => { + switch (key) { + case 'amount': + return 'delegationValue'; + case 'unclaimed_rewards': + return 'operatorReward'; + case 'profit_margin_percent': + return 'profitMarginValue'; + case 'operating_cost': + return 'operatorCostValue'; + default: + return key; + } }; const mapAndSort = (_items: TDelegations) => {