Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #849 from swaponline/partialClosure
Partial closure
- Loading branch information
Showing
12 changed files
with
257 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import React, { Component, Fragment } from 'react' | ||
|
||
import Link from 'sw-valuelink' | ||
import { links } from 'helpers' | ||
|
||
import CSSModules from 'react-css-modules' | ||
import styles from './PartialClosure.scss' | ||
|
||
import { connect } from 'redaction' | ||
import actions from 'redux/actions' | ||
import { BigNumber } from 'bignumber.js' | ||
|
||
import SelectGroup from './SelectGroup/SelectGroup' | ||
import { Button, Toggle } from 'components/controls' | ||
import { Redirect } from 'react-router-dom' | ||
|
||
|
||
const filterIsPartial = (orders) => orders | ||
.filter(order => order.isPartialClosure) | ||
|
||
|
||
@connect(({ currencies, core: { orders } }) => ({ | ||
currencies: currencies.items, | ||
orders: filterIsPartial(orders), | ||
})) | ||
@CSSModules(styles) | ||
export default class PartialClosure extends Component { | ||
|
||
static defaultProps = { | ||
orders: [], | ||
} | ||
|
||
constructor() { | ||
super() | ||
|
||
this.state = { | ||
haveCurrency: 'btc', | ||
getCurrency: 'eth', | ||
haveAmount: '', | ||
getAmount: '', | ||
peer: '', | ||
filteredOrders: [], | ||
isNonOffers: false, | ||
isDeclinedOffer: false, | ||
} | ||
} | ||
|
||
static getDerivedStateFromProps({ orders }, { haveCurrency }) { | ||
if (!Array.isArray(orders)) { return } | ||
|
||
const filteredOrders = orders.filter(order => | ||
order.sellCurrency === haveCurrency.toUpperCase()) | ||
|
||
return { | ||
filteredOrders, | ||
} | ||
} | ||
|
||
sendRequest = () => { | ||
const { getAmount, haveAmount, haveCurrency, getCurrency, peer, orderId } = this.state | ||
|
||
if (!String(getAmount) || !peer || !orderId || !String(haveAmount)) { | ||
return | ||
} | ||
|
||
const order = { | ||
buyCurrency: getCurrency, | ||
sellCurrency: haveCurrency, | ||
sellAmount: haveAmount, | ||
buyAmount: getAmount, | ||
} | ||
|
||
actions.core.requestToPeer('request partial closure', peer, { order, orderId }, (orderId) => { | ||
console.log('orderId', orderId) | ||
// TODO change callback on boolean type | ||
if (orderId) { | ||
actions.core.sendRequest(orderId, (isAccept) => { | ||
if (isAccept) { | ||
this.setState(() => ({ | ||
redirect: true, | ||
orderId, | ||
})) | ||
} | ||
}) | ||
} else { | ||
this.setState(() => ({ isDeclinedOffer: true, haveAmount: '' })) | ||
} | ||
}) | ||
} | ||
|
||
setAmount = (value) => { | ||
this.setState(() => ({ haveAmount: new BigNumber(String(value)) })) | ||
|
||
const { filteredOrders } = this.state | ||
|
||
console.log('value', value) | ||
console.log('filteredOrders', filteredOrders) | ||
|
||
if (filteredOrders.length === 0) { | ||
this.setState(() => ({ isNonOffers: true })) | ||
return | ||
} | ||
|
||
// TODO add check orders and view | ||
const sortedOrder = filteredOrders.sort((a, b) => a.exchangeRate - b.exchangeRate) | ||
const exRate = new BigNumber(String(sortedOrder[0].exchangeRate)) | ||
|
||
console.log('exRate', exRate) | ||
console.log('sortedOrder', sortedOrder) | ||
|
||
this.setState(() => ({ | ||
isNonOffers: false, | ||
getAmount: exRate.multipliedBy(new BigNumber(String(value))), | ||
peer: sortedOrder[0].owner.peer, | ||
orderId: sortedOrder[0].id, | ||
})) | ||
} | ||
|
||
render() { | ||
const { currencies } = this.props | ||
const { haveCurrency, getCurrency, isNonOffers, redirect, orderId, isDeclinedOffer, type } = this.state | ||
|
||
const linked = Link.all(this, 'haveAmount', 'getAmount') | ||
|
||
if (redirect) { | ||
return <Redirect push to={`${links.swap}/${getCurrency}-${haveCurrency}/${orderId}`} /> | ||
} | ||
|
||
return ( | ||
<Fragment> | ||
<h1>Partial Closure</h1> | ||
<div style={{ width: '400px', margin: '0 auto' }}> | ||
<SelectGroup | ||
inputValueLink={linked.haveAmount.pipe(this.setAmount)} | ||
selectedValue={haveCurrency} | ||
onSelect={({ value }) => this.setState(() => ({ haveCurrency: value }))} | ||
label="You have" | ||
placeholder="Enter amount" | ||
currencies={currencies} | ||
/> | ||
<SelectGroup | ||
inputValueLink={linked.getAmount} | ||
selectedValue={getCurrency} | ||
onSelect={({ value }) => this.setState(() => ({ getCurrency: value }))} | ||
label="You get" | ||
disabled | ||
currencies={currencies} | ||
/> | ||
{ | ||
isDeclinedOffer && (<p>Offer is declined</p>) | ||
} | ||
{ | ||
isNonOffers && (<p style={{ color: 'red' }}>No offers </p>) | ||
} | ||
<Button styleName="button" brand fullWidth onClick={this.sendRequest} disabled={isNonOffers}> | ||
Start | ||
</Button> | ||
</div> | ||
</Fragment> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.button { | ||
margin-top: 20px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react' | ||
|
||
import CSSModules from 'react-css-modules' | ||
import styles from './SelectGroup.scss' | ||
|
||
import Input from 'components/forms/Input/Input' | ||
import FieldLabel from 'components/forms/FieldLabel/FieldLabel' | ||
import CurrencySelect from 'components/ui/CurrencySelect/CurrencySelect' | ||
|
||
// TODO to split data and view this component | ||
const SelectGroup = ({ selectedValue, onSelect, currencies, placeholder, label, disabled, className, inputValueLink }) => ( | ||
<div> | ||
<FieldLabel inRow>{label}</FieldLabel> | ||
<div styleName="groupField" className={className}> | ||
<Input | ||
styleName="inputRoot" | ||
inputContainerClassName="inputContainer" | ||
valueLink={inputValueLink} | ||
type="number" | ||
placeholder={placeholder} | ||
pattern="0-9." | ||
disabled={disabled} | ||
/> | ||
<CurrencySelect | ||
styleName="currencySelect" | ||
selectedValue={selectedValue} | ||
onSelect={onSelect} | ||
currencies={currencies} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
|
||
export default CSSModules(SelectGroup, styles) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
.groupField { | ||
width: 100%; | ||
display: inline-flex; | ||
height: 60px; | ||
margin-bottom: 10px; | ||
box-shadow: rgba(#000, 0.08) 0 3px 9px 0; | ||
border-radius: 6px; | ||
text-align: center; | ||
} | ||
|
||
.inputRoot { | ||
min-width: 0; | ||
flex: auto 1 1; | ||
} | ||
|
||
.inputContainer { | ||
border-radius: 6px 0 0 6px !important; | ||
box-shadow: none !important; | ||
} | ||
|
||
input:disabled { | ||
background-color:#eee; | ||
} | ||
|
||
.currencySelect { | ||
border-radius: 0 6px 6px 0 !important; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters