-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TRAH-2991] shahzaib / real account currency selector config from web…
…site status (#13763) * chore: dynamic currencies based on website status * chore: revert context value * chore: update docs * chore: update docs * fix: fixed typo
- Loading branch information
1 parent
0505cce
commit e839280
Showing
9 changed files
with
121 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { CurrenciesListOrder } from '@/constants'; | ||
import { TCurrencyConfig } from '@/hooks/useCurrencies'; | ||
|
||
/** | ||
* Reorder currencies list based on the type and the defined order as `CurrenciesListOrder`. | ||
* @param {TCurrencyConfig[]} list - The list of currencies. | ||
* @param {keyof typeof CurrenciesListOrder} type - The type of the order. | ||
* @returns {TCurrencyConfig[]} The reordered list of currencies. | ||
*/ | ||
export const reorderCurrencies = (list: TCurrencyConfig[], type: keyof typeof CurrenciesListOrder) => { | ||
const newOrder = CurrenciesListOrder[type]; | ||
|
||
return list.sort((a, b) => { | ||
if (newOrder.indexOf(a.id) < newOrder.indexOf(b.id)) { | ||
return -1; | ||
} | ||
if (newOrder.indexOf(a.id) > newOrder.indexOf(b.id)) { | ||
return 1; | ||
} | ||
return 0; | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './currencyConfig'; | ||
export * from './currencies'; | ||
export * from './formikHelpers'; | ||
export * from './signupModalHelpers'; | ||
export * from './urls'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useMemo } from 'react'; | ||
import { reorderCurrencies } from '@/helpers'; | ||
import { useQuery } from '@deriv/api'; | ||
|
||
type TWebsiteStatus = NonNullable<ReturnType<typeof useQuery<'website_status'>>['data']>['website_status']; | ||
export type TCurrencyConfig = NonNullable<TWebsiteStatus>['currencies_config'][string] & { | ||
id: string; | ||
}; | ||
|
||
export type TCurrencies = { | ||
CRYPTO: TCurrencyConfig[]; | ||
FIAT: TCurrencyConfig[]; | ||
}; | ||
|
||
/** A custom hook to get the currency config information from `website_status` endpoint and in predefined order */ | ||
const useCurrencies = () => { | ||
const { data, ...rest } = useQuery('website_status'); | ||
|
||
const currencyConfig = useMemo(() => { | ||
if (!data?.website_status?.currencies_config) return; | ||
const currencies: TCurrencies = { | ||
FIAT: [], | ||
CRYPTO: [], | ||
}; | ||
|
||
// map the currencies to their respective types (FIAT or CRYPTO) with their id | ||
Object.entries(data?.website_status?.currencies_config).forEach(([key, value]) => { | ||
if (value.type === 'fiat') { | ||
currencies.FIAT.push({ ...value, id: key }); | ||
} else { | ||
currencies.CRYPTO.push({ ...value, id: key }); | ||
} | ||
}); | ||
|
||
// reorder the currencies based on the predefined order | ||
return { | ||
FIAT: reorderCurrencies(currencies.FIAT, 'FIAT'), | ||
CRYPTO: reorderCurrencies(currencies.CRYPTO, 'CRYPTO'), | ||
}; | ||
}, [data?.website_status?.currencies_config]); | ||
|
||
return { | ||
data: currencyConfig, | ||
...rest, | ||
}; | ||
}; | ||
|
||
export default useCurrencies; |
23 changes: 12 additions & 11 deletions
23
packages/tradershub/src/screens/CurrencySelector/Currencies.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters