From 19f61906d3075391efb32c17c99c2cba1f7a3858 Mon Sep 17 00:00:00 2001
From: fragosti <francesco.agosti93@gmail.com>
Date: Wed, 10 Oct 2018 18:27:06 -0700
Subject: feat: Move over features from zrx-buyer

---
 packages/instant/src/components/buy_button.tsx | 59 +++++++++++++++++++++-----
 1 file changed, 48 insertions(+), 11 deletions(-)

(limited to 'packages/instant/src/components/buy_button.tsx')

diff --git a/packages/instant/src/components/buy_button.tsx b/packages/instant/src/components/buy_button.tsx
index 5a32b9575..097b8e547 100644
--- a/packages/instant/src/components/buy_button.tsx
+++ b/packages/instant/src/components/buy_button.tsx
@@ -1,19 +1,56 @@
+import { BuyQuote } from '@0xproject/asset-buyer';
+import * as _ from 'lodash';
 import * as React from 'react';
 
 import { ColorOption } from '../style/theme';
+import { assetBuyer } from '../util/asset_buyer';
+import { web3Wrapper } from '../util/web3_wrapper';
 
 import { Button, Container, Text } from './ui';
 
-export interface BuyButtonProps {}
+export interface BuyButtonProps {
+    buyQuote?: BuyQuote;
+    onClick: (buyQuote: BuyQuote) => void;
+    onBuySuccess: (buyQuote: BuyQuote) => void;
+    onBuyFailure: (buyQuote: BuyQuote) => void;
+    text: string;
+}
 
-export const BuyButton: React.StatelessComponent<BuyButtonProps> = props => (
-    <Container padding="20px" width="100%">
-        <Button width="100%">
-            <Text fontColor={ColorOption.white} fontWeight={600} fontSize="20px">
-                Buy
-            </Text>
-        </Button>
-    </Container>
-);
+const boundNoop = _.noop.bind(_);
 
-BuyButton.displayName = 'BuyButton';
+export class BuyButton extends React.Component<BuyButtonProps> {
+    public static defaultProps = {
+        onClick: boundNoop,
+        onBuySuccess: boundNoop,
+        onBuyFailure: boundNoop,
+    };
+    public render(): React.ReactNode {
+        const shouldDisableButton = _.isUndefined(this.props.buyQuote);
+        return (
+            <Container padding="20px" width="100%">
+                <Button width="100%" onClick={this._handleClick} isDisabled={shouldDisableButton}>
+                    <Text fontColor={ColorOption.white} fontWeight={600} fontSize="20px">
+                        {this.props.text}
+                    </Text>
+                </Button>
+            </Container>
+        );
+    }
+    private readonly _handleClick = async () => {
+        // The button is disabled when there is no buy quote anyway.
+        if (_.isUndefined(this.props.buyQuote)) {
+            return;
+        }
+        this.props.onClick(this.props.buyQuote);
+        try {
+            const txnHash = await assetBuyer.executeBuyQuoteAsync(this.props.buyQuote, {
+                // HACK: There is a calculation issue in asset-buyer. ETH is refunded anyway so just over-estimate.
+                ethAmount: this.props.buyQuote.worstCaseQuoteInfo.totalEthAmount.mul(2),
+            });
+            await web3Wrapper.awaitTransactionSuccessAsync(txnHash);
+        } catch {
+            this.props.onBuyFailure(this.props.buyQuote);
+        }
+        this.props.onBuySuccess(this.props.buyQuote);
+    };
+}
-- 
cgit 

