Skip to content

Commit

Permalink
fix(LLD/LLM CounterValues): Changed supported CounterValues source to…
Browse files Browse the repository at this point in the history
… API

Feedbacks

Removed overprotection on settings reducer to allow not to impact the init of the app
  • Loading branch information
male-gal committed Oct 25, 2023
1 parent ff8738d commit 92e7743
Show file tree
Hide file tree
Showing 17 changed files with 258 additions and 58 deletions.
7 changes: 7 additions & 0 deletions .changeset/neat-mirrors-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"ledger-live-desktop": patch
"live-mobile": patch
"@ledgerhq/coin-framework": patch
---

New supported countervalues source from API.
6 changes: 6 additions & 0 deletions apps/ledger-live-desktop/src/renderer/actions/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
selectedTimeRangeSelector,
SettingsState,
VaultSigner,
SupportedCountervaluesData,
} from "~/renderer/reducers/settings";
import { useRefreshAccountsOrdering } from "~/renderer/actions/general";
import { Language, Locale } from "~/config/languages";
Expand Down Expand Up @@ -344,3 +345,8 @@ export const setVaultSigner = (payload: VaultSigner) => ({
type: "SET_VAULT_SIGNER",
payload,
});

export const setSupportedCounterValues = (payload: SupportedCountervaluesData[]) => ({
type: "SET_SUPPORTED_COUNTER_VALUES",
payload,
});
1 change: 1 addition & 0 deletions apps/ledger-live-desktop/src/renderer/init.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ async function init() {
deepLinkUrl = url;
});
const initialSettings = (await getKey("app", "settings")) || {};

fetchSettings(
deepLinkUrl
? {
Expand Down
46 changes: 30 additions & 16 deletions apps/ledger-live-desktop/src/renderer/reducers/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export type SettingsState = {
};
featureFlagsButtonVisible: boolean;
vaultSigner: VaultSigner;
supportedCounterValues: SupportedCountervaluesData[];
};

export const getInitialLanguageAndLocale = (): { language: Language; locale: Locale } => {
Expand Down Expand Up @@ -189,6 +190,7 @@ const INITIAL_STATE: SettingsState = {

// Vault
vaultSigner: { enabled: false, host: "", token: "", workspace: "" },
supportedCounterValues: [],
};

/* Handlers */
Expand Down Expand Up @@ -237,6 +239,7 @@ type HandlersPayloads = {
featureFlagsButtonVisible: boolean;
};
SET_VAULT_SIGNER: VaultSigner;
SET_SUPPORTED_COUNTER_VALUES: SupportedCountervaluesData[];
};
type SettingsHandlers<PreciseKey = true> = Handlers<SettingsState, HandlersPayloads, PreciseKey>;

Expand Down Expand Up @@ -265,12 +268,6 @@ const handlers: SettingsHandlers = {
};
},
FETCH_SETTINGS: (state, { payload: settings }) => {
if (
settings.counterValue &&
!supportedCountervalues.find(({ currency }) => currency.ticker === settings.counterValue)
) {
settings.counterValue = INITIAL_STATE.counterValue;
}
return {
...state,
...settings,
Expand Down Expand Up @@ -396,6 +393,20 @@ const handlers: SettingsHandlers = {
...state,
vaultSigner: payload,
}),
SET_SUPPORTED_COUNTER_VALUES: (state: SettingsState, { payload }) => {
let activeCounterValue = state.counterValue;
if (
activeCounterValue &&
!payload.find(({ currency }) => currency.ticker === activeCounterValue)
) {
activeCounterValue = INITIAL_STATE.counterValue;
}
return {
...state,
supportedCounterValues: payload,
counterValue: activeCounterValue,
};
},
};
export default handleActions<SettingsState, HandlersPayloads[keyof HandlersPayloads]>(
handlers as unknown as SettingsHandlers<false>,
Expand Down Expand Up @@ -465,17 +476,18 @@ export type SupportedCountervaluesData = {
label: string;
currency: Currency;
};
export const supportedCountervalues: SupportedCountervaluesData[] = [
...listSupportedFiats(),
...possibleIntermediaries,
]
.map(currency => ({
value: currency.ticker,
label: `${currency.name} - ${currency.ticker}`,
currency,
}))
.sort((a, b) => (a.currency.name < b.currency.name ? -1 : 1));

export const getsupportedCountervalues = async (): Promise<SupportedCountervaluesData[]> => {
const supportedFiats = await listSupportedFiats();
const data = [...supportedFiats, ...possibleIntermediaries]
.map(currency => ({
value: currency.ticker,
label: `${currency.name} - ${currency.ticker}`,
currency,
}))
.sort((a, b) => (a.currency.name < b.currency.name ? -1 : 1));
return data;
};
// TODO refactor selectors to *Selector naming convention

export const storeSelector = (state: State): SettingsState => state.settings;
Expand Down Expand Up @@ -691,3 +703,5 @@ export const overriddenFeatureFlagsSelector = (state: State) =>
export const featureFlagsButtonVisibleSelector = (state: State) =>
state.settings.featureFlagsButtonVisible;
export const vaultSignerSelector = (state: State) => state.settings.vaultSigner;
export const supportedCounterValuesSelector = (state: State) =>
state.settings.supportedCounterValues;
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React, { useCallback, useMemo, memo } from "react";
import { supportedCountervalues, SupportedCountervaluesData } from "~/renderer/reducers/settings";
import {
supportedCounterValuesSelector,
SupportedCountervaluesData,
} from "~/renderer/reducers/settings";
import Dropdown from "./DropDown";
import Track from "~/renderer/analytics/Track";
import { useTranslation } from "react-i18next";
import { Currency } from "@ledgerhq/types-cryptoassets";
import { useSelector } from "react-redux";

type Props = {
counterCurrency?: string;
Expand All @@ -17,6 +21,7 @@ function CounterValueSelect({
supportedCounterCurrencies,
}: Props) {
const { t } = useTranslation();
const supportedCountervalues = useSelector(supportedCounterValuesSelector);

const handleChangeCounterValue = useCallback(
(item: { currency: Currency } | null) => {
Expand All @@ -32,7 +37,7 @@ function CounterValueSelect({
supportedCountervalues.filter(({ value }: SupportedCountervaluesData) =>
supportedCounterCurrencies.includes(value?.toLowerCase()),
),
[supportedCounterCurrencies],
[supportedCounterCurrencies, supportedCountervalues],
);

const cvOption = useMemo(
Expand All @@ -41,7 +46,7 @@ function CounterValueSelect({
(f: SupportedCountervaluesData) =>
f?.value?.toLowerCase() === counterCurrency?.toLowerCase(),
),
[counterCurrency],
[counterCurrency, supportedCountervalues],
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import MarketList from "./MarketList";
import SideDrawerFilter from "./SideDrawerFilter";
import { rangeDataTable } from "@ledgerhq/live-common/market/utils/rangeDataTable";
import TrackPage from "~/renderer/analytics/TrackPage";
import { useInitSupportedCounterValues } from '../settings/sections/General/index'

const Container = styled(Flex).attrs({
flex: "1",
Expand Down Expand Up @@ -71,6 +72,8 @@ export default function Market() {
const starredMarketCoins: string[] = useSelector(starredMarketCoinsSelector);
const starFilterOn = starred.length > 0;

useInitSupportedCounterValues()

const updateSearch = useCallback(
(value: string) => {
refresh({ search: value, starred: [], liveCompatible: false });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import { setCounterValue } from "~/renderer/actions/settings";
import {
SupportedCountervaluesData,
counterValueCurrencySelector,
supportedCountervalues,
supportedCounterValuesSelector,
} from "~/renderer/reducers/settings";
import Select from "~/renderer/components/Select";
import Track from "~/renderer/analytics/Track";

const CounterValueSelect = React.memo<{}>(function CounterValueSelect() {
const counterValueCurrency = useSelector(counterValueCurrencySelector);
const supportedCounterValues = useSelector(supportedCounterValuesSelector);

const dispatch = useDispatch();

const handleChangeCounterValue = useCallback(
(item?: SupportedCountervaluesData | null) => {
if (!item) return;
Expand All @@ -19,8 +23,8 @@ const CounterValueSelect = React.memo<{}>(function CounterValueSelect() {
[dispatch],
);
const cvOption = useMemo(
() => supportedCountervalues.find(f => f.value === counterValueCurrency.ticker),
[counterValueCurrency],
() => supportedCounterValues.find(f => f.value === counterValueCurrency.ticker),
[counterValueCurrency, supportedCounterValues],
);
return (
<>
Expand All @@ -31,7 +35,7 @@ const CounterValueSelect = React.memo<{}>(function CounterValueSelect() {
onChange={handleChangeCounterValue}
itemToString={(item: { name: string }) => item.name}
renderSelected={(item: { name: string }) => item && item.name}
options={supportedCountervalues}
options={supportedCounterValues}
value={cvOption}
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,22 @@ import SentryLogsButton from "./SentryLogsButton";
import ShareAnalyticsButton from "./ShareAnalyticsButton";
import CarouselVisibility from "./CarouselVisibility";
import { hasPasswordSelector } from "~/renderer/reducers/application";
import { setSupportedCounterValues } from "~/renderer/actions/settings";
import { getsupportedCountervalues } from "~/renderer/reducers/settings";
import { useDispatch } from "react-redux";

export const useInitSupportedCounterValues = async () => {
const dispatch = useDispatch();

const supportedCounterValues = await getsupportedCountervalues();
dispatch(setSupportedCounterValues(supportedCounterValues));
};

const SectionGeneral = () => {
const hasPassword = useSelector(hasPasswordSelector);
const { t } = useTranslation();
useInitSupportedCounterValues();

return (
<>
<TrackPage category="Settings" name="Display" />
Expand Down
69 changes: 69 additions & 0 deletions apps/ledger-live-desktop/tests/specs/market/market.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,75 @@ test("Market", async ({ page }) => {
await expect.soft(page).toHaveScreenshot("market-page-no-scrollbar.png");
});

await page.route("https://countervalues.live.ledger.com/v2/supported-to", async route => {
route.fulfill({
headers: { teststatus: "mocked" },
body: JSON.stringify([
"aed",
"ars",
"aud",
"bch",
"bdt",
"bhd",
"bits",
"bmd",
"bnb",
"brl",
"btc",
"cad",
"chf",
"clp",
"cny",
"czk",
"dkk",
"dot",
"eos",
"eth",
"eur",
"gbp",
"hkd",
"huf",
"idr",
"ils",
"inr",
"jpy",
"krw",
"kwd",
"link",
"lkr",
"ltc",
"mmk",
"mxn",
"myr",
"ngn",
"nok",
"nzd",
"php",
"pkr",
"pln",
"rub",
"sar",
"sats",
"sek",
"sgd",
"thb",
"try",
"twd",
"uah",
"usd",
"vef",
"vnd",
"xag",
"xau",
"xdr",
"xlm",
"xrp",
"yfi",
"zar",
]),
});
});

await test.step("change countervalue", async () => {
await marketPage.switchCountervalue("THB");
await marketPage.waitForLoading();
Expand Down
5 changes: 5 additions & 0 deletions apps/ledger-live-mobile/src/actions/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
SettingsSetClosedNetworkBannerPayload,
SettingsSetClosedWithdrawBannerPayload,
SettingsSetUserNps,
SettingsSetSupportedCounterValues,
} from "./types";
import { ImageType } from "../components/CustomImage/types";

Expand Down Expand Up @@ -274,6 +275,10 @@ export const setGeneralTermsVersionAccepted = createAction<SettingsSetGeneralTer

export const setUserNps = createAction<SettingsSetUserNps>(SettingsActionTypes.SET_USER_NPS);

export const setSupportedCounterValues = createAction<SettingsSetSupportedCounterValues>(
SettingsActionTypes.SET_SUPPORTED_COUNTER_VALUES,
);

type PortfolioRangeOption = {
key: PortfolioRange;
value: string;
Expand Down
5 changes: 4 additions & 1 deletion apps/ledger-live-mobile/src/actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export enum SettingsActionTypes {
SET_CLOSED_NETWORK_BANNER = "SET_CLOSED_NETWORK_BANNER",
SET_CLOSED_WITHDRAW_BANNER = "SET_CLOSED_WITHDRAW_BANNER",
SET_USER_NPS = "SET_USER_NPS",
SET_SUPPORTED_COUNTER_VALUES = "SET_SUPPORTED_COUNTER_VALUES",
}

export type SettingsImportPayload = Partial<SettingsState>;
Expand Down Expand Up @@ -375,6 +376,7 @@ export type SettingsSetHasBeenUpsoldProtectPayload = SettingsState["hasBeenUpsol
export type SettingsCompleteOnboardingPayload = void | SettingsState["hasCompletedOnboarding"];
export type SettingsSetGeneralTermsVersionAccepted = SettingsState["generalTermsVersionAccepted"];
export type SettingsSetUserNps = number;
export type SettingsSetSupportedCounterValues = SettingsState["supportedCounterValues"];

export type SettingsPayload =
| SettingsImportPayload
Expand Down Expand Up @@ -429,7 +431,8 @@ export type SettingsPayload =
| SettingsSetHasBeenUpsoldProtectPayload
| SettingsSetOnboardingTypePayload
| SettingsSetClosedNetworkBannerPayload
| SettingsSetUserNps;
| SettingsSetUserNps
| SettingsSetSupportedCounterValues;

// === WALLET CONNECT ACTIONS ===
export enum WalletConnectActionTypes {
Expand Down
Loading

0 comments on commit 92e7743

Please sign in to comment.