1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-12 11:28:56 +00:00
trezor-wallet/src/actions/WalletActions.js

200 lines
5.6 KiB
JavaScript
Raw Normal View History

2018-02-20 09:30:36 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
import { LOCATION_CHANGE } from 'connected-react-router';
import { DEVICE } from 'trezor-connect';
import * as CONNECT from 'actions/constants/TrezorConnect';
2018-08-14 13:18:16 +00:00
import * as WALLET from 'actions/constants/wallet';
2018-09-26 12:11:37 +00:00
import * as reducerUtils from 'reducers/utils';
import * as deviceUtils from 'utils/device';
2018-02-20 09:30:36 +00:00
2018-09-03 15:43:17 +00:00
import type {
Device,
2018-07-30 10:52:13 +00:00
TrezorDevice,
RouterLocationState,
ThunkAction,
PayloadAction,
Action,
2018-07-30 10:52:13 +00:00
Dispatch,
GetState,
2018-07-30 10:52:13 +00:00
State,
2018-08-14 12:56:47 +00:00
} from 'flowtype';
2018-04-16 21:19:50 +00:00
2019-03-04 12:33:02 +00:00
export type WalletAction =
| {
type: typeof WALLET.SET_INITIAL_URL,
state?: RouterLocationState,
pathname?: string,
2019-03-04 12:33:02 +00:00
}
2019-04-17 15:13:23 +00:00
| {
type: typeof WALLET.SET_HIDDEN_COINS,
2019-04-17 17:14:06 +00:00
hiddenCoins: Array<string>,
2019-04-17 15:13:23 +00:00
}
2019-04-24 12:05:07 +00:00
| {
type: typeof WALLET.SET_HIDDEN_COINS_EXTERNAL,
hiddenCoinsExternal: Array<string>,
}
2019-03-04 12:33:02 +00:00
| {
type: typeof WALLET.TOGGLE_DEVICE_DROPDOWN,
opened: boolean,
}
| {
type: typeof WALLET.ONLINE_STATUS,
online: boolean,
}
| {
type: typeof WALLET.SET_SELECTED_DEVICE,
device: ?TrezorDevice,
}
| {
type: typeof WALLET.UPDATE_SELECTED_DEVICE,
device: TrezorDevice,
}
| {
type: typeof WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA,
devices: Array<TrezorDevice>,
}
| {
type:
| typeof WALLET.SHOW_BETA_DISCLAIMER
| typeof WALLET.HIDE_BETA_DISCLAIMER
| typeof WALLET.SET_FIRST_LOCATION_CHANGE,
}
| {
type: typeof WALLET.TOGGLE_SIDEBAR,
}
| {
type: typeof WALLET.SET_LANGUAGE,
locale: string,
messages: { [string]: string },
}
| {
type: typeof WALLET.SET_LOCAL_CURRENCY,
localCurrency: string,
}
| {
type: typeof WALLET.SET_HIDE_BALANCE,
2019-03-15 09:46:54 +00:00
toggled: boolean,
2019-03-04 12:33:02 +00:00
};
2018-05-17 14:03:11 +00:00
2018-09-06 15:04:28 +00:00
export const init = (): ThunkAction => (dispatch: Dispatch): void => {
const updateOnlineStatus = () => {
2018-07-30 10:52:13 +00:00
dispatch({
type: WALLET.ONLINE_STATUS,
online: navigator.onLine,
});
};
window.addEventListener('online', updateOnlineStatus);
window.addEventListener('offline', updateOnlineStatus);
};
export const hideBetaDisclaimer = (): WalletAction => ({
type: WALLET.HIDE_BETA_DISCLAIMER,
});
2018-07-30 10:52:13 +00:00
export const toggleDeviceDropdown = (opened: boolean): WalletAction => ({
type: WALLET.TOGGLE_DEVICE_DROPDOWN,
opened,
});
2019-01-24 17:49:33 +00:00
export const toggleSidebar = (): WalletAction => ({
type: WALLET.TOGGLE_SIDEBAR,
});
2019-02-15 19:52:56 +00:00
export const fetchLocale = (locale: string): ThunkAction => (dispatch: Dispatch): void => {
2019-03-04 13:37:27 +00:00
fetch(`./l10n/${locale}.json`)
2019-03-04 18:03:07 +00:00
.then(response => {
if (response.ok) {
return response.json();
}
throw Error(response.statusText);
})
2019-03-04 12:33:02 +00:00
.then(messages => {
2019-02-15 19:52:56 +00:00
dispatch({
type: WALLET.SET_LANGUAGE,
locale,
messages,
});
2019-03-04 18:03:07 +00:00
})
.catch(error => {
console.error(error);
2019-02-15 19:52:56 +00:00
});
};
export const setLocalCurrency = (localCurrency: string): WalletAction => ({
type: WALLET.SET_LOCAL_CURRENCY,
localCurrency: localCurrency.toLowerCase(),
});
export const setHideBalance = (toggled: boolean): WalletAction => ({
type: WALLET.SET_HIDE_BALANCE,
toggled,
});
// This method will be called after each DEVICE.CONNECT action
// if connected device has different "passphrase_protection" settings than saved instances
// all saved instances will be removed immediately inside DevicesReducer
// This method will clear leftovers associated with removed instances from reducers.
// (DiscoveryReducer, AccountReducer, TokensReducer)
2019-03-04 12:33:02 +00:00
export const clearUnavailableDevicesData = (prevState: State, device: Device): ThunkAction => (
dispatch: Dispatch
): void => {
2018-07-30 10:52:13 +00:00
if (!device.features) return;
2019-03-04 12:33:02 +00:00
const affectedDevices = prevState.devices.filter(
d =>
d.features &&
device.features &&
d.features.device_id === device.features.device_id &&
d.features.passphrase_protection !== device.features.passphrase_protection
);
2018-07-30 10:52:13 +00:00
if (affectedDevices.length > 0) {
dispatch({
type: WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA,
devices: affectedDevices,
});
}
2018-07-30 10:52:13 +00:00
};
// list of all actions which has influence on "selectedDevice" field in "wallet" reducer
// other actions will be ignored
const actions = [
LOCATION_CHANGE,
CONNECT.AUTH_DEVICE,
CONNECT.RECEIVE_WALLET_TYPE,
...Object.values(DEVICE).filter(v => typeof v === 'string'),
];
/*
2019-03-04 12:33:02 +00:00
* Called from WalletService
*/
export const observe = (prevState: State, action: Action): PayloadAction<boolean> => (
dispatch: Dispatch,
getState: GetState
): boolean => {
// ignore not listed actions
if (actions.indexOf(action.type) < 0) return false;
2018-07-30 10:52:13 +00:00
const state: State = getState();
2018-09-26 12:11:37 +00:00
const device = reducerUtils.getSelectedDevice(state);
const selectedDeviceChanged = reducerUtils.observeChanges(state.wallet.selectedDevice, device);
2018-07-30 10:52:13 +00:00
// handle devices state change (from trezor-connect events or location change)
if (selectedDeviceChanged) {
if (device && deviceUtils.isSelectedDevice(state.wallet.selectedDevice, device)) {
2018-09-26 12:11:37 +00:00
dispatch({
type: WALLET.UPDATE_SELECTED_DEVICE,
device,
});
} else {
dispatch({
type: WALLET.SET_SELECTED_DEVICE,
device,
});
}
return true;
}
return false;
2019-03-04 12:33:02 +00:00
};