Skip to content

Commit

Permalink
Implement store setting
Browse files Browse the repository at this point in the history
Implement sell page
  • Loading branch information
ianpark committed Jun 19, 2018
1 parent 134cabf commit 414858f
Show file tree
Hide file tree
Showing 17 changed files with 628 additions and 43 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"redux": "^3.7.2",
"semantic-ui-css": "^2.3.1",
"semantic-ui-react": "^0.79.1",
"steem": "^0.7.1"
"steem": "^0.7.1",
"uuid": "^3.2.1"
},
"scripts": {
"start": "cross-env NODE_PATH=src react-scripts start",
Expand Down
1 change: 1 addition & 0 deletions src/components/AppFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const NavBarChildren = ({ children }) => (

const slideMenu = [
{ name: 'Home', link: '/', icon: 'home' },
{ name: 'Sell', link: '/sell', icon: 'tag' },
{ name: 'Payment', link: '/pay', icon: 'payment' },
{ name: 'Invoice', link: '/invoice', icon: 'tag' },
{ name: 'Setting', link: '/setting', icon: 'setting' },
Expand Down
254 changes: 254 additions & 0 deletions src/components/SellForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
import React, { Component } from "react";
import { Button, Form, Dimmer, Loader, Input, Dropdown, TextArea, Label, Message, Table, Divider } from 'semantic-ui-react'
import { Redirect, withRouter, Link } from 'react-router-dom';
import Api from 'shared/API';
import Utils from 'shared/Utils';

class SellForm extends Component {
constructor(props) {
super(props);
let store = props.config.stores.length > 0 ? props.config.stores[0] : false

this.state = {
store: store,
itemList : [],
receiverInfo: false,
invoiceId: null,
fetching: false,
receivers: this.props.users.map((user) => {
var key = JSON.stringify({account: user.account});
return {text: user.account, key: user.account, value: key}
}),
exchanges: this.props.exchanges.map((exchange) => {
var key = JSON.stringify({exchange: exchange.name, account: exchange.account, wallet: exchange.wallet, nickname: exchange.nickname});
return {text: exchange.nickname + ' (' + exchange.account + ')', value: key, key: key };
}),
currencies: Utils.getCurrencies().map((currency) => {
return {key: currency.code, text: currency.symbol, value: currency.code, content: currency.code + ", " + currency.name };
})
};
this.handleChange = this.handleChange.bind(this);
}

onStoreChange = (event, data) => {
let store = this.props.config.stores.find((store) => store.name === data.value)
this.setState({store, itemList: []});
}

handleChange = (event, data) => {
let SellData = this.state.SellData;
SellData[data.name] = data.value;
this.setState(SellData);
}

onPaymentCreated = (result) => {
console.log(result)
this.setState({invoiceId: result.invoiceId});
this.setState({fetching: false});
}

onPaymentCreationFailed = (err) => {
this.setState({fetching: false});
}

getTotalAmount = (items) => {
let totalAmount = 0;
items.forEach(item => {
totalAmount += item.price * item.count;
})
return totalAmount
}

createPayment = (onSuccess) => {
localStorage.setItem('preferred_store', this.state.store.name);

let items = this.getUniqueItemList()
let totalAmount = this.getTotalAmount(items);
let memo = items.map(item => item.name + '✕' + item.count).join(', ')

this.setState({fetching: true});
var receiver = this.getReceiverDetail();
console.log(receiver)

var payload = {
receiver: receiver.account,
type: receiver.type,
receiverDetail: receiver,
amount: totalAmount,
currency: this.state.store.currency,
memo: memo
};

Api.createInvoice(payload,
(data) => { this.onPaymentCreated(data); },
(error) => { this.onPaymentCreationFailed(error); }
);
}

isReady = () => {
return this.state.itemList.length > 0
}

getParameters = () => {
let config = this.props.config;

return config && {
receivers: config.users.map((user) => {
var key = JSON.stringify({account: user.account});
return {text: user.account, key: user.account, value: key}
}),
exchanges: config.exchanges.map((exchange) => {
var key = JSON.stringify({exchange: exchange.name, account: exchange.account, wallet: exchange.wallet, nickname: exchange.nickname});
return {text: exchange.nickname + ' (' + exchange.account + ')', value: key, key: key };
}),
currencies: Utils.getCurrencies().map((currency) => {
return {key: currency.code, text: currency.symbol, value: currency.code, content: currency.code + ", " + currency.name };
})
}
}

getUniqueItemList = () => {
let itemList = this.state.itemList;
let uniqItems = []
itemList.forEach((item) => {
let uniqItem = uniqItems.find(uniqItem => uniqItem.name === item.name);
if (!uniqItem) {
uniqItems.push({...item, count: 1});
} else {
uniqItem.count += 1;
}
});
return uniqItems;
}

addItemList = (item) => {
let itemList = this.state.itemList;
itemList.push(item)
this.setState({itemList});
}

removeItemList = (name) => {
let itemList = this.state.itemList;
let index = itemList.findIndex((item) => item.name === name)
if (index > -1) {
itemList.splice(index, 1);
}
this.setState({itemList});
}

renderStorePanel = () => {
return (
<div>
<Label.Group color='teal' style={{paddingTop: '10px'}}>
{this.state.store.items.map((item, index) => (
<Button key={index} color='teal' size='small' onClick={() => this.addItemList(item)}>
{item.name}
</Button>
))}
</Label.Group>
</div>
)
}

renderSelectedItems = () => {
let currency = Utils.getCurrencySymbol(this.state.store.currency);
let selectedItems = this.getUniqueItemList();
let totalAmount = this.getTotalAmount(selectedItems);

return (
<div>
<Table compact='very' unstackable>
<Table.Header>
<Table.Row>
<Table.HeaderCell>
Item Name
</Table.HeaderCell>
<Table.HeaderCell textAlign='center'>
Qty
</Table.HeaderCell>
<Table.HeaderCell textAlign='right'>
Price
</Table.HeaderCell>
<Table.HeaderCell textAlign='right'>
Net Price
</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
{selectedItems.map((item, index) => (
<Table.Row key={index}>
<Table.Cell>
<Button icon='minus' color='orange' size='mini' compact onClick={() => this.removeItemList(item.name)} />
<b>{item.name}</b>
</Table.Cell>
<Table.Cell textAlign='center'>
{item.count}
</Table.Cell>
<Table.Cell textAlign='right'>
{currency}{item.price}
</Table.Cell>
<Table.Cell textAlign='right'>
{currency}{item.count * item.price}
</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table>
<div style={{textAlign: 'right'}}>
<Label floated='right' color='teal' size='big' tag as='a'>{currency}{totalAmount}</Label>
</div>
</div>
)
}

getReceiverDetail = () => {
let config = this.props.config;
let accountId = this.state.store.linkedAccount;
let receiverInfo = [...config.users, ...config.exchanges].find(user => user.id === accountId);
return receiverInfo;
}

render() {
if (this.state.invoiceId) {
return <Redirect push to={"/invoice/" + this.state.invoiceId} />
}
if (!this.props.config.stores) {
return <Message>You have no store. <Link to='/setting'>setting</Link></Message>
}
let stores = this.props.config.stores.map((store, index) => { return {text: store.name, value: store.name, key: index} });

return (
<div style={{textAlign:'center'}}>
<div style={{display:'inline-block', textAlign: 'left', width: '100%', maxWidth: '600px'}}>
<h2>Sell Items</h2>
<Divider />
<Dropdown placeholder='Choose Receiver' name='receiver' fluid selection options={[...stores]}
onChange={this.onStoreChange} style={{fontSize: '14pt'}} defaultValue={this.state.store.name}
/>
{this.renderStorePanel()}
<h3>Items to Sell</h3>
{this.renderSelectedItems()}
<Divider hidden />
<Button size='big' circular disabled={!this.isReady()} fluid onClick={() => this.createPayment(this.onPaymentCreated)}>
Sell!
</Button>
{this.state.fetching && (
<Dimmer active>
<Loader>Creating...</Loader>
</Dimmer>
)}
</div>
</div>
)
}
}

SellForm.defaultProps = {
users: [],
exchanges: [],
feed: {}
}



export default withRouter(SellForm);
54 changes: 43 additions & 11 deletions src/components/login/LoginPanel.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
import React, { Component } from 'react'
import { Button, Dropdown, Image } from 'semantic-ui-react'
import { Button, Dropdown, Image, Icon, Confirm } from 'semantic-ui-react'
import { Link } from 'react-router-dom';
import userIcon from './user.jpg'

class LoginPanel extends Component {
constructor(props) {
super(props);
this.state = {
logoutConfirmOpen: false
}
}

showLogoutDialog = () => {
this.setState({logoutConfirmOpen: true})
}

handleLogoutConfirm = () => {
this.props.onLogoutRequest()
this.setState({logoutConfirmOpen: false})
}

handleLogoutCancel = () => {
this.setState({logoutConfirmOpen: false})
}

renderAuthenticated = () => {
let nickname = this.props.auth.attributes.nickname
const trigger = (
<span>
Hello, aaaaadfs <Image avatar src={"http://localhost:3000/img/home.jpeg"} />
</span>
<Image avatar src={userIcon} />
)
const options = [
{ key: 'user', text: 'Account', icon: 'user' },
{ key: 'settings', text: 'Settings', icon: 'settings' },
{ key: 'sign-out', text: 'Sign Out', icon: 'sign out', onClick: this.props.onLogoutRequest },
]
console.log(this.props.auth)

return (
<div>
<Dropdown trigger={trigger} options={options} icon={null} />
<Dropdown trigger={trigger} size="big">
<Dropdown.Menu>
<Dropdown.Header content={`hello, ${nickname}`} />
<Dropdown.Item as={Link} to="/setting">
<Icon name="setting"/> Settings
</Dropdown.Item>
<Dropdown.Item onClick={this.showLogoutDialog}>
<Icon name="sign out"/> Sign Out
</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
<Confirm open={this.state.logoutConfirmOpen}
onCancel={this.handleLogoutCancel}
onConfirm={this.handleLogoutConfirm} style={{
marginTop: '40px !important',
marginLeft: 'auto',
marginRight: 'auto',
}}/>
</div>
)
}
Expand Down
Binary file added src/components/login/user.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
4 changes: 3 additions & 1 deletion src/components/setting/AddExchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class AddExchange extends Component {
handleSave = () => {
let exchange = this.state.exchangeData;
exchange.account = Utils.getExchangeAccount(this.state.exchangeData.name);
exchange.id = exchange.nickname
exchange.type = 'exchange'
this.props.onSave(exchange);
this.setState({exchangeData: {}});
this.handleClose();
Expand All @@ -56,7 +58,7 @@ class AddExchange extends Component {
<Button circular onClick={this.handleClose}>
Cancel
</Button>
<Button circular positive onClick={this.handleSave}
<Button circular color='teal' onClick={this.handleSave}
icon='checkmark' labelPosition='right' content='Add' />
</Modal.Actions>
</Modal>
Expand Down
9 changes: 7 additions & 2 deletions src/components/setting/AddUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ class AddUser extends Component {
}

handleSave() {
this.props.onSave({account: this.state.account});
this.props.onSave(
{
type: 'user',
account: this.state.account,
id: this.state.account
});
this.handleClose();
}

Expand All @@ -51,7 +56,7 @@ class AddUser extends Component {
<Button circular onClick={this.handleClose}>
Cancel
</Button>
<Button circular positive onClick={this.handleSave}
<Button circular color='teal' onClick={this.handleSave}
icon='checkmark' labelPosition='right' content='Add' />
</Modal.Actions>
</Modal>
Expand Down
Loading

0 comments on commit 414858f

Please sign in to comment.