aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/components/dialogs/send_dialog.tsx
blob: c1179dbd0a7aa12e7740a2c43a6c66b7ba19e905 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
import Dialog from 'material-ui/Dialog';
import FlatButton from 'material-ui/FlatButton';
import * as React from 'react';
import { Blockchain } from 'ts/blockchain';
import { AddressInput } from 'ts/components/inputs/address_input';
import { TokenAmountInput } from 'ts/components/inputs/token_amount_input';
import { EthAmountInput } from 'ts/containers/inputs/eth_amount_input';
import { Token } from 'ts/types';

interface SendDialogProps {
    blockchain: Blockchain;
    userAddress: string;
    networkId: number;
    onComplete: (recipient: string, value: BigNumber) => void;
    onCancelled: () => void;
    isOpen: boolean;
    asset: Token | 'ETH';
    lastForceTokenStateRefetch: number;
}

interface SendDialogState {
    value?: BigNumber;
    recipient: string;
    shouldShowIncompleteErrs: boolean;
    isAmountValid: boolean;
}

export class SendDialog extends React.Component<SendDialogProps, SendDialogState> {
    constructor(props: SendDialogProps) {
        super(props);
        this.state = {
            recipient: '',
            shouldShowIncompleteErrs: false,
            isAmountValid: false,
        };
    }
    public render(): React.ReactNode {
        const transferDialogActions = [
            <FlatButton key="cancelTransfer" label="Cancel" onTouchTap={this._onCancel.bind(this)} />,
            <FlatButton
                key="sendTransfer"
                disabled={this._hasErrors()}
                label="Send"
                primary={true}
                onTouchTap={this._onSendClick.bind(this)}
            />,
        ];
        return (
            <Dialog
                title="I want to send"
                titleStyle={{ fontWeight: 100 }}
                actions={transferDialogActions}
                open={this.props.isOpen}
            >
                {this._renderSendDialogBody()}
            </Dialog>
        );
    }
    private _renderSendDialogBody(): React.ReactNode {
        const input =
            this.props.asset === 'ETH' ? (
                <EthAmountInput
                    label="Amount to send"
                    shouldShowIncompleteErrs={this.state.shouldShowIncompleteErrs}
                    shouldCheckBalance={true}
                    shouldShowErrs={true}
                    onChange={this._onValueChange.bind(this)}
                    amount={this.state.value}
                />
            ) : (
                <TokenAmountInput
                    blockchain={this.props.blockchain}
                    userAddress={this.props.userAddress}
                    networkId={this.props.networkId}
                    label="Amount to send"
                    token={this.props.asset}
                    shouldShowIncompleteErrs={this.state.shouldShowIncompleteErrs}
                    shouldCheckBalance={true}
                    shouldCheckAllowance={false}
                    onChange={this._onValueChange.bind(this)}
                    amount={this.state.value}
                    lastForceTokenStateRefetch={this.props.lastForceTokenStateRefetch}
                />
            );
        return (
            <div className="mx-auto" style={{ maxWidth: 300 }}>
                <div style={{ height: 80 }}>
                    <AddressInput
                        initialAddress={this.state.recipient}
                        updateAddress={this._onRecipientChange.bind(this)}
                        isRequired={true}
                        label="Recipient address'"
                        hintText="Address"
                    />
                </div>
                {input}
            </div>
        );
    }
    private _onRecipientChange(recipient?: string): void {
        this.setState({
            shouldShowIncompleteErrs: false,
            recipient,
        });
    }
    private _onValueChange(isValid: boolean, amount?: BigNumber): void {
        this.setState({
            isAmountValid: isValid,
            value: amount,
        });
    }
    private _onSendClick(): void {
        if (this._hasErrors()) {
            this.setState({
                shouldShowIncompleteErrs: true,
            });
        } else {
            const value = this.state.value;
            this.setState({
                recipient: undefined,
                value: undefined,
            });
            this.props.onComplete(this.state.recipient, value);
        }
    }
    private _onCancel(): void {
        this.setState({
            value: undefined,
        });
        this.props.onCancelled();
    }
    private _hasErrors(): boolean {
        return _.isUndefined(this.state.recipient) || _.isUndefined(this.state.value) || !this.state.isAmountValid;
    }
}