|
| 1 | +declare const Thunder: any; |
| 2 | + |
| 3 | +import * as React from "react"; |
| 4 | +import * as ReactDOM from "react-dom"; |
| 5 | +import { render } from "react-dom"; |
| 6 | +import { Provider } from "react-redux"; |
| 7 | +import { connect } from "react-redux"; |
| 8 | + |
| 9 | +import configureStore from "store/configureStore"; |
| 10 | +import { login } from "actions/account"; |
| 11 | +import { requestPurchase, clearPurchase, endPurchase } from "actions/purchase"; |
| 12 | +import { addProduct, removeProduct, selectProduct, increaseProductQty, changePage } from "actions/product"; |
| 13 | + |
| 14 | +import ProductList from "components/ProductList"; |
| 15 | +import PurchaseButton from "components/PurchaseButton"; |
| 16 | +import AccountBar from "components/AccountBar"; |
| 17 | +import ButtonBar from "components/ButtonBar"; |
| 18 | +import LoadingBox from "components/LoadingBox"; |
| 19 | + |
| 20 | +interface Props { |
| 21 | + dispatch: Function; |
| 22 | + products: any; |
| 23 | + account: any; |
| 24 | + purchase: any; |
| 25 | +} |
| 26 | + |
| 27 | +class App extends React.Component<Props, {}> { |
| 28 | + componentDidMount() { |
| 29 | + const { dispatch } = this.props; |
| 30 | + // dispatch(login("154464990")); |
| 31 | + Thunder.connect("localhost:8080", "foobar", ["products", "cards"]); |
| 32 | + Thunder.listen((data: any) => { |
| 33 | + if (data.channel === "products") { |
| 34 | + dispatch(addProduct(JSON.parse(data.payload))); |
| 35 | + } else if (data.channel === "cards") { |
| 36 | + dispatch(login(data.payload)); |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + addRandomProduct() { |
| 42 | + let eans = [ |
| 43 | + "7310500088853", |
| 44 | + "7340083438684", |
| 45 | + "7611612221351", |
| 46 | + "7310500114934", |
| 47 | + "7310070765840", |
| 48 | + "7315360010754", |
| 49 | + "7622300342753" |
| 50 | + ]; |
| 51 | + |
| 52 | + let randomIndex = Math.floor(Math.random() * eans.length); |
| 53 | + this.props.dispatch(addProduct(eans[randomIndex])); |
| 54 | + } |
| 55 | + |
| 56 | + render() { |
| 57 | + const { dispatch, products, account, purchase } = this.props; |
| 58 | + |
| 59 | + let selected = products.products.filter((product: any) => { |
| 60 | + return product.selected; |
| 61 | + }).length; |
| 62 | + |
| 63 | + if ((purchase.state === "ONGOING" || purchase.state === "PENDING")) { |
| 64 | + return ( |
| 65 | + <div id="container"> |
| 66 | + <ProductList |
| 67 | + products={products} |
| 68 | + onSelect={(ean: string) => dispatch(selectProduct(ean))}/> |
| 69 | + <span id="trash" className="button" onClick={() => dispatch(clearPurchase())}> |
| 70 | + <i className="fa fa-times"></i> |
| 71 | + </span> |
| 72 | + <span style={{ position: "absolute", bottom: 0, zIndex: 99 }} className="button" onClick={() => this.addRandomProduct()}> |
| 73 | + <i className="fa fa-plus"></i> |
| 74 | + </span> |
| 75 | + <div id="sidebar"> |
| 76 | + <AccountBar {...account} /> |
| 77 | + <div id="menubox"></div> |
| 78 | + <PurchaseButton |
| 79 | + products={products} |
| 80 | + purchaseState={purchase.state} |
| 81 | + accountBalance={account.balance} |
| 82 | + onPurchase={() => dispatch(requestPurchase())} |
| 83 | + /> |
| 84 | + </div> |
| 85 | + <ButtonBar |
| 86 | + onIncrease={() => dispatch(increaseProductQty(1))} |
| 87 | + onDecrease={() => dispatch(increaseProductQty(-1))} |
| 88 | + onRemove={() => dispatch(removeProduct())} |
| 89 | + onScrollUp={() => dispatch(changePage(-1))} |
| 90 | + onScrollDown={() => dispatch(changePage(1))} |
| 91 | + active={selected > 0} |
| 92 | + scrollUpActive={products.page > 0} |
| 93 | + scrollDownActive={products.page < products.maxPage} |
| 94 | + /> |
| 95 | + <LoadingBox loading={purchase.state === "PENDING"}/> |
| 96 | + </div> |
| 97 | + ); |
| 98 | + } |
| 99 | + else if (purchase.state === "ONGOING") { |
| 100 | + return ( |
| 101 | + <div id="container" className="white"> |
| 102 | + <div className="loading"> |
| 103 | + <div className="rekt1"/> |
| 104 | + <div className="rekt2"/> |
| 105 | + <div className="rekt3"/> |
| 106 | + <div className="rekt4"/> |
| 107 | + <div className="rekt5"/> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + ); |
| 111 | + } |
| 112 | + else if (purchase.state === "FINALIZED") { |
| 113 | + return ( |
| 114 | + <div id="container"> |
| 115 | + <div id="start"> |
| 116 | + <div>Total cost of purchase was {purchase.cost} kr.</div> |
| 117 | + <span className="button" onClick={() => dispatch(endPurchase())}>Okay</span> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + ); |
| 121 | + } |
| 122 | + else /* if(purchase.state === "WAITING") */ { |
| 123 | + return ( |
| 124 | + <div id="container"> |
| 125 | + <div id="start"> |
| 126 | + <span style={{ position: "absolute", bottom: 0, zIndex: 99 }} className="button" onClick={() => this.addRandomProduct()}> |
| 127 | + <i className="fa fa-plus"></i> |
| 128 | + </span> |
| 129 | + <div>Blip a card linked with your account.</div> |
| 130 | + <div>or</div> |
| 131 | + <div>Scan a product to start a cash payment.</div> |
| 132 | + <span className="button" onClick={() => dispatch(login("154464990"))}>I like pies</span> |
| 133 | + </div> |
| 134 | + <LoadingBox loading={account.request}/> |
| 135 | + </div> |
| 136 | + ); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +function mapStateToProps(state: any) { |
| 142 | + const { products, account, purchase } = state; |
| 143 | + |
| 144 | + return { |
| 145 | + products, |
| 146 | + account, |
| 147 | + purchase |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +const store = configureStore(); |
| 152 | +const AppWithState = connect(mapStateToProps)(App); |
| 153 | + |
| 154 | +ReactDOM.render(( |
| 155 | + <Provider store={store}> |
| 156 | + <AppWithState/> |
| 157 | + </Provider> |
| 158 | +), document.getElementById("app")); |
0 commit comments