aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/containers/available_erc20_token_selector.ts
blob: 4d4218d22d43b514cb3c3a6938024843ef2f2f9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import * as _ from 'lodash';
import * as React from 'react';
import { connect } from 'react-redux';
import { Dispatch } from 'redux';

import { State } from '../redux/reducer';
import { ERC20Asset } from '../types';
import { assetUtils } from '../util/asset';

import { ERC20TokenSelector } from '../components/erc20_token_selector';
import { Action, actions } from '../redux/actions';

export interface AvailableERC20TokenSelectorProps {
    onTokenSelect?: (token: ERC20Asset) => void;
}

interface ConnectedState {
    tokens: ERC20Asset[];
}

interface ConnectedDispatch {
    onTokenSelect: (token: ERC20Asset) => void;
}

const mapStateToProps = (state: State, _ownProps: AvailableERC20TokenSelectorProps): ConnectedState => ({
    tokens: assetUtils.getERC20AssetsFromAssets(state.availableAssets || []),
});

const mapDispatchToProps = (
    dispatch: Dispatch<Action>,
    ownProps: AvailableERC20TokenSelectorProps,
): ConnectedDispatch => ({
    onTokenSelect: (token: ERC20Asset) => {
        dispatch(actions.updateSelectedAsset(token));
        dispatch(actions.resetAmount());
        if (ownProps.onTokenSelect) {
            ownProps.onTokenSelect(token);
        }
    },
});

export const AvailableERC20TokenSelector: React.ComponentClass<AvailableERC20TokenSelectorProps> = connect(
    mapStateToProps,
    mapDispatchToProps,
)(ERC20TokenSelector);