Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/chore/ddw-602-improve-pin-entry-…
Browse files Browse the repository at this point in the history
…component-ux' into chore/ddw-602-improve-pin-entry-component-ux
  • Loading branch information
DeeJayElly committed May 5, 2021
2 parents 1e535a4 + 31b093c commit cc98723
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 151 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,12 @@
Changelog
=========

## vNext

### Chores

- Moved currency related code into a dedicated store ([PR 2546](https://github.com/input-output-hk/daedalus/pull/2546))

## 4.1.0-FC1

### Features
Expand Down
9 changes: 9 additions & 0 deletions source/renderer/app/actions/currency-actions.js
@@ -0,0 +1,9 @@
// @flow
import Action from './lib/Action';

// ======= CURRENCY ACTIONS =======

export default class CurrencyActions {
setCurrencySelected: Action<{ code: string }> = new Action();
toggleCurrencyIsActive: Action<any> = new Action();
}
3 changes: 3 additions & 0 deletions source/renderer/app/actions/index.js
Expand Up @@ -3,6 +3,7 @@ import AddressesActions from './addresses-actions';
import AppActions from './app-actions';
import AppUpdateActions from './app-update-actions';
import AssetsActions from './assets-actions';
import CurrencyActions from './currency-actions';
import DialogsActions from './dialogs-actions';
import HardwareWalletsActions from './hardware-wallets-actions';
import NetworkStatusActions from './network-status-actions';
Expand All @@ -26,6 +27,7 @@ export type ActionsMap = {
appUpdate: AppUpdateActions,
assets: AssetsActions,
dialogs: DialogsActions,
currency: CurrencyActions,
hardwareWallets: HardwareWalletsActions,
networkStatus: NetworkStatusActions,
notifications: NotificationsActions,
Expand All @@ -48,6 +50,7 @@ const actionsMap: ActionsMap = {
app: new AppActions(),
appUpdate: new AppUpdateActions(),
assets: new AssetsActions(),
currency: new CurrencyActions(),
dialogs: new DialogsActions(),
hardwareWallets: new HardwareWalletsActions(),
networkStatus: new NetworkStatusActions(),
Expand Down
2 changes: 0 additions & 2 deletions source/renderer/app/actions/wallets-actions.js
Expand Up @@ -87,8 +87,6 @@ export default class WalletsActions {
setCertificateTemplate: Action<{ selectedTemplate: string }> = new Action();
finishCertificate: Action<any> = new Action();
finishRewardsCsv: Action<any> = new Action();
setCurrencySelected: Action<{ currencyCode: string }> = new Action();
toggleCurrencyIsActive: Action<any> = new Action();

/* ---------- Transfer Funds ---------- */
setActiveAssetFingerprint: Action<{
Expand Down
Expand Up @@ -9,29 +9,29 @@ import type { InjectedProps } from '../../../types/injectedPropsType';
export default class WalletsSettingsPage extends Component<InjectedProps> {
static defaultProps = { actions: null, stores: null };

handleSelectCurrency = (currencyCode: string) => {
this.props.actions.wallets.setCurrencySelected.trigger({ currencyCode });
handleSelectCurrency = (code: string) => {
this.props.actions.currency.setCurrencySelected.trigger({ code });
};

handleToggleCurrencyIsActive = () =>
this.props.actions.wallets.toggleCurrencyIsActive.trigger();
this.props.actions.currency.toggleCurrencyIsActive.trigger();

render() {
const { stores } = this.props;
const {
localizedCurrency: currencySelected,
localizedCurrency: selected,
localizedCurrencyList: currencyList,
currencyRate,
currencyIsActive,
} = stores.wallets;
rate,
isActive,
} = stores.currency;
const { currentLocale } = stores.profile;
const { openExternalLink } = stores.app;
return (
<WalletsSettings
currencySelected={currencySelected}
currencyRate={currencyRate}
currencySelected={selected}
currencyRate={rate}
currencyList={currencyList}
currencyIsActive={currencyIsActive}
currencyIsActive={isActive}
currentLocale={currentLocale}
onSelectCurrency={this.handleSelectCurrency}
onToggleCurrencyIsActive={this.handleToggleCurrencyIsActive}
Expand Down
39 changes: 24 additions & 15 deletions source/renderer/app/containers/wallet/WalletSummaryPage.js
Expand Up @@ -77,7 +77,15 @@ export default class WalletSummaryPage extends Component<Props> {
render() {
const { intl } = this.context;
const { stores, actions } = this.props;
const { app, wallets, addresses, transactions, profile, assets } = stores;
const {
app,
wallets,
addresses,
transactions,
profile,
assets,
currency,
} = stores;
const { getAssetDetails, assetSettingsDialogWasOpened } = assets;
const { isInternalAddress } = addresses;
const { onAssetSettingsOpen } = actions.assets;
Expand All @@ -94,15 +102,16 @@ export default class WalletSummaryPage extends Component<Props> {
deleteTransactionRequest,
pendingTransactionsCount,
} = transactions;
const { active: wallet } = wallets;
const {
active: wallet,
currencyIsActive,
currencyIsAvailable,
currencyIsFetchingRate,
currencyLastFetched,
currencyRate,
currencySelected,
} = wallets;
isActive,
isAvailable,
isFetchingRate,
lastFetched,
rate,
selected,
} = currency;

const { currentTimeFormat, currentDateFormat, currentLocale } = profile;
const hasAssetsEnabled = WALLET_ASSETS_ENABLED;

Expand Down Expand Up @@ -190,12 +199,12 @@ export default class WalletSummaryPage extends Component<Props> {
isLoadingTransactions={recentTransactionsRequest.isExecutingFirstTime}
isLoadingAssets={isLoadingAssets}
hasAssetsEnabled={hasAssetsEnabled && hasRawAssets}
currencyIsActive={currencyIsActive}
currencyIsAvailable={currencyIsAvailable}
currencyIsFetchingRate={currencyIsFetchingRate}
currencyLastFetched={currencyLastFetched}
currencyRate={currencyRate}
currencySelected={currencySelected}
currencyIsActive={isActive}
currencyIsAvailable={isAvailable}
currencyIsFetchingRate={isFetchingRate}
currencyLastFetched={lastFetched}
currencyRate={rate}
currencySelected={selected}
onCurrencySettingClick={this.handleCurrencySettingsClick}
assets={walletAssets}
assetSettingsDialogWasOpened={assetSettingsDialogWasOpened}
Expand Down
123 changes: 123 additions & 0 deletions source/renderer/app/stores/CurrencyStore.js
@@ -0,0 +1,123 @@
// @flow
import { action, observable, computed, runInAction } from 'mobx';
import Store from './lib/Store';
import {
CURRENCY_REQUEST_RATE_INTERVAL,
getLocalizedCurrency,
getLocalizedCurrenciesList,
getCurrencyFromCode,
} from '../config/currencyConfig';
import type { Currency, LocalizedCurrency } from '../types/currencyTypes.js';

export default class CurrencyStore extends Store {
@observable isFetchingList: boolean = false;
@observable isFetchingRate: boolean = false;
@observable isAvailable: boolean = false;
@observable isActive: boolean = false;
@observable list: Array<Currency> = [];
@observable selected: ?Currency = null;
@observable rate: ?number = null;
@observable lastFetched: ?Date = null;
_getCurrencyRateInterval: ?IntervalID = null;

setup() {
const { currency: currencyActions } = this.actions;
currencyActions.setCurrencySelected.listen(this._setCurrencySelected);
currencyActions.toggleCurrencyIsActive.listen(this._toggleCurrencyIsActive);

this._setupCurrency();
}

// PUBLIC

@computed get localizedCurrencyList(): Array<LocalizedCurrency> {
const { list, stores } = this;
const { currentLocale } = stores.profile;
return getLocalizedCurrenciesList(list, currentLocale);
}

@computed get localizedCurrency(): ?LocalizedCurrency {
const { selected, stores } = this;
const { currentLocale } = stores.profile;
if (!selected) return null;
return getLocalizedCurrency(selected, currentLocale);
}

@action getCurrencyList = async () => {
this.isFetchingList = true;
const list = await this.api.ada.getCurrencyList();
runInAction(() => {
this.list = list;
this.isFetchingList = false;
});
};

@action getCurrencyRate = async () => {
const { localizedCurrency } = this;
if (localizedCurrency && localizedCurrency.code) {
try {
this.isFetchingRate = true;
const rate = await this.api.ada.getCurrencyRate(localizedCurrency);
runInAction(() => {
this.isFetchingRate = false;
this.lastFetched = new Date();
if (rate) {
this.rate = rate;
this.isAvailable = true;
} else {
throw new Error('Error fetching the Currency rate');
}
});
} catch (error) {
runInAction(() => {
this.rate = null;
this.isAvailable = false;
});
clearInterval(this._getCurrencyRateInterval);
}
}
};

// PRIVATE

@action _setupCurrency = async () => {
// Check if the user has enabled currencies
// Otherwise applies the default config
const isActive = await this.api.localStorage.getCurrencyIsActive();

// Check if the user has already selected a currency
// Otherwise applies the default currency
const localCurrencyCode = await this.api.localStorage.getCurrencySelected();
const selected = getCurrencyFromCode(localCurrencyCode);

runInAction(() => {
this.isActive = isActive;
this.selected = selected;
});

clearInterval(this._getCurrencyRateInterval);
this._getCurrencyRateInterval = setInterval(
this.getCurrencyRate,
CURRENCY_REQUEST_RATE_INTERVAL
);

// Fetch the currency list and rate
this.getCurrencyList();
this.getCurrencyRate();
};

@action _setCurrencySelected = async ({ code }: { code: string }) => {
const { list } = this;
const selected = list.find((item) => item.code === code);
if (selected) {
this.selected = selected;
this.getCurrencyRate();
await this.api.localStorage.setCurrencySelected(selected.code);
}
};

@action _toggleCurrencyIsActive = () => {
this.isActive = !this.isActive;
this.api.localStorage.setCurrencyIsActive(this.isActive);
};
}

0 comments on commit cc98723

Please sign in to comment.