|
|
|
@ -6,6 +6,10 @@ import React from 'react';
|
|
|
|
|
import ReactMixin from 'react-mixin'; |
|
|
|
|
import Catalyst from 'react-catalyst'; |
|
|
|
|
import autobind from 'autobind-decorator'; |
|
|
|
|
import cx from 'classnames'; |
|
|
|
|
import parser from '../libs/parser'; |
|
|
|
|
import Moment from 'moment'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@autobind |
|
|
|
|
class TxForm extends React.Component { |
|
|
|
@ -16,7 +20,7 @@ class TxForm extends React.Component {
|
|
|
|
|
// 2. Take the data from the form and create an object
|
|
|
|
|
var tx = { |
|
|
|
|
id : this.refs.id.value, |
|
|
|
|
amount : this.refs.amount.value, |
|
|
|
|
amount : this.parseEquation(this.refs.amount.value), |
|
|
|
|
currency : this.refs.currency.value, |
|
|
|
|
date : this.refs.date.value, |
|
|
|
|
account : this.refs.account.value, |
|
|
|
@ -28,6 +32,22 @@ class TxForm extends React.Component {
|
|
|
|
|
this.refs.txForm.reset(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
parseEquation(string) { |
|
|
|
|
try { |
|
|
|
|
var Parser = parser.Parser; |
|
|
|
|
string = Parser.evaluate(string); |
|
|
|
|
} catch (e) { |
|
|
|
|
// I guess we just don't care
|
|
|
|
|
} finally { |
|
|
|
|
return string; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
handleAmountChange(evt) { |
|
|
|
|
var result = evt.target.value; |
|
|
|
|
this.handleChange(evt) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
handleChange(evt) { |
|
|
|
|
var currTx = this.state.tx; |
|
|
|
|
currTx[evt.target.name] = evt.target.value; |
|
|
|
@ -45,6 +65,14 @@ class TxForm extends React.Component {
|
|
|
|
|
this.setState({tx: this.defaultTxState()}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
deleteItem() { |
|
|
|
|
var tx = { |
|
|
|
|
id : +this.refs.id.value |
|
|
|
|
} |
|
|
|
|
this.props.deleteTx(tx); |
|
|
|
|
this.refs.txForm.reset(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
render() { |
|
|
|
|
this.state = this.state || {}; |
|
|
|
|
if (this.state.updated) { |
|
|
|
@ -56,16 +84,44 @@ class TxForm extends React.Component {
|
|
|
|
|
if (Object.keys(this.state.tx).length == 0) |
|
|
|
|
this.state.tx = this.defaultTxState(); |
|
|
|
|
|
|
|
|
|
var hasItem = ( (this.state.tx.id) ? true : false ); |
|
|
|
|
var delDisabledClassName = cx({ |
|
|
|
|
'hollow': true, 'alert': true, 'button': true, tiny: true, |
|
|
|
|
'disabled': !hasItem, hide: !hasItem, |
|
|
|
|
}); |
|
|
|
|
var hasDiffHomeValue = false; |
|
|
|
|
var homeValue = this.parseEquation(this.state.tx.amount); |
|
|
|
|
if ( ( this.state.tx.amount ) && ( this.state.tx.amount != homeValue ) ) { |
|
|
|
|
hasDiffHomeValue = true; |
|
|
|
|
this.state.homeValue = homeValue; |
|
|
|
|
} |
|
|
|
|
var homeValueBoxClassName = cx({ |
|
|
|
|
'grid-block': true, |
|
|
|
|
'hide': !hasDiffHomeValue, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<form className="tx-edit" ref="txForm" onSubmit={this.updateTx}> |
|
|
|
|
<div className="grid-block"> |
|
|
|
|
<div className="small-6 medium-8 grid-content"> |
|
|
|
|
<input type="text" ref="amount" placeholder="Amount" required |
|
|
|
|
name="amount" value={this.state.tx.amount} onChange={this.handleChange} /> |
|
|
|
|
name="amount" value={this.state.tx.amount} |
|
|
|
|
onChange={this.handleAmountChange} /> |
|
|
|
|
</div> |
|
|
|
|
<div className="small-6 medium-4 grid-content"> |
|
|
|
|
<input type="text" ref="currency" placeholder="Currency" required |
|
|
|
|
name="currency" value={this.state.tx.currency} onChange={this.handleChange} /> |
|
|
|
|
name="currency" value={this.state.tx.currency} |
|
|
|
|
onChange={this.handleChange} /> |
|
|
|
|
</div> |
|
|
|
|
</div> |
|
|
|
|
<div className={homeValueBoxClassName}> |
|
|
|
|
<div className="small-6 medium-8 grid-content"> |
|
|
|
|
<input type="text" ref="xrate" placeholder="exchange rate" required |
|
|
|
|
name="xrate" defaultValue="1" value={this.state.tx.xrate} |
|
|
|
|
onChange={this.handleChange} /> |
|
|
|
|
</div> |
|
|
|
|
<div className="small-6 medium-4 grid-content"> |
|
|
|
|
<input type="text" value={this.state.homeValue} disabled="true" /> |
|
|
|
|
</div> |
|
|
|
|
</div> |
|
|
|
|
<div className="grid-block"> |
|
|
|
@ -78,7 +134,8 @@ class TxForm extends React.Component {
|
|
|
|
|
{this.props.accounts.map((account) => { |
|
|
|
|
var opts = { value: account.id }; |
|
|
|
|
return ( |
|
|
|
|
<option {...opts} key={account.id}>{account.title} ({account.currency})</option> |
|
|
|
|
<option {...opts} key={account.id}> |
|
|
|
|
{account.title} ({account.currency})</option> |
|
|
|
|
) |
|
|
|
|
})} |
|
|
|
|
</select> |
|
|
|
@ -95,6 +152,7 @@ class TxForm extends React.Component {
|
|
|
|
|
<input type="hidden" ref="id" value={this.state.tx.id} /> |
|
|
|
|
<button type="submit" className="success button">+ {( (this.state.tx.id) ? "Edit" : "Add" )} Tx</button> |
|
|
|
|
<button onClick={this.resetForm} className="default button">Reset</button> |
|
|
|
|
<button onClick={this.deleteItem} className={delDisabledClassName} disabled={!hasItem}>- Remove Tx</button> |
|
|
|
|
</div> |
|
|
|
|
</div> |
|
|
|
|
</form> |
|
|
|
@ -104,6 +162,7 @@ class TxForm extends React.Component {
|
|
|
|
|
|
|
|
|
|
TxForm.propTypes = { |
|
|
|
|
txFormHandler : React.PropTypes.func.isRequired, |
|
|
|
|
deleteTx : React.PropTypes.func.isRequired, |
|
|
|
|
accounts : React.PropTypes.array.isRequired, |
|
|
|
|
tx : React.PropTypes.object, |
|
|
|
|
} |
|
|
|
|