From 099f42e4fe2f09de6efd0d69561a40bdb913108d Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Wed, 26 Aug 2015 07:38:13 -0400 Subject: [PATCH] Add symbols for securities --- securities.go | 3 +++ static/account_register.js | 35 +++++++++++++++++++++++++++-------- static/models.js | 4 ++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/securities.go b/securities.go index 16b6672..0d9b44a 100644 --- a/securities.go +++ b/securities.go @@ -16,6 +16,7 @@ const ( type Security struct { SecurityId int64 Name string + Symbol string // Number of decimal digits (to the right of the decimal point) this // security is precise to Precision int @@ -30,11 +31,13 @@ var security_map = map[int64]*Security{ 1: &Security{ SecurityId: 1, Name: "USD", + Symbol: "$", Precision: 2, Type: Banknote}, 2: &Security{ SecurityId: 2, Name: "SPY", + Symbol: "SPY", Precision: 5, Type: Stock}, } diff --git a/static/account_register.js b/static/account_register.js index e0c8749..356fa87 100644 --- a/static/account_register.js +++ b/static/account_register.js @@ -49,13 +49,13 @@ const TransactionRow = React.createClass({ accountName = "--Split Transaction--"; } - var amount = "$" + thisAccountSplit.Amount.toFixed(security.Precision); - var balance = "$" + this.props.transaction.Balance.toFixed(security.Precision); + var amount = security.Symbol + " " + thisAccountSplit.Amount.toFixed(security.Precision); + var balance = security.Symbol + " " + this.props.transaction.Balance.toFixed(security.Precision); status = TransactionStatusMap[this.props.transaction.Status]; number = thisAccountSplit.Number; } else { - var amount = "$" + (new Big(0.0)).toFixed(security.Precision); - var balance = "$" + (new Big(0.0)).toFixed(security.Precision); + var amount = security.Symbol + " " + (new Big(0.0)).toFixed(security.Precision); + var balance = security.Symbol + " " + (new Big(0.0)).toFixed(security.Precision); } return ( @@ -74,7 +74,12 @@ const TransactionRow = React.createClass({ const AmountInput = React.createClass({ _getInitialState: function(props) { // Ensure we can edit this without screwing up other copies of it - var a = props.value.toFixed(this.props.security.Precision); + var a; + if (props.security) + a = props.value.toFixed(props.security.Precision); + else + a = props.value.toString(); + return { LastGoodAmount: a, Amount: a @@ -84,7 +89,9 @@ const AmountInput = React.createClass({ return this._getInitialState(this.props); }, componentWillReceiveProps: function(nextProps) { - if (!nextProps.value.eq(this.props.value) && !nextProps.value.eq(this.getValue())) { + if ((!nextProps.value.eq(this.props.value) && + !nextProps.value.eq(this.getValue())) || + nextProps.security !== this.props.security) { this.setState(this._getInitialState(nextProps)); } }, @@ -92,8 +99,13 @@ const AmountInput = React.createClass({ this.refs.amount.getInputDOMNode().onblur = this.onBlur; }, onBlur: function() { + var a; + if (this.props.security) + a = (new Big(this.getValue())).toFixed(this.props.security.Precision); + else + a = (new Big(this.getValue())).toString(); this.setState({ - Amount: (new Big(this.getValue())).toFixed(this.props.security.Precision) + Amount: a }); }, onChange: function() { @@ -112,10 +124,15 @@ const AmountInput = React.createClass({ } }, render: function() { + var symbol = "?"; + if (this.props.security) + symbol = this.props.security.Symbol; + return ( ); } @@ -230,7 +247,9 @@ const AddEditTransactionModal = React.createClass({ for (var i = 0; i < this.state.transaction.Splits.length; i++) { var self = this; var s = this.state.transaction.Splits[i]; - var security = this.props.security_map[this.props.account_map[s.AccountId].SecurityId]; + var security = null; + if (this.props.account_map[s.AccountId]) + security = this.props.security_map[this.props.account_map[s.AccountId].SecurityId]; // Define all closures for calling split-updating functions var deleteSplitFn = (function() { diff --git a/static/models.js b/static/models.js index 3a9b681..7a47434 100644 --- a/static/models.js +++ b/static/models.js @@ -92,6 +92,7 @@ for (var type in SecurityType) { function Security() { this.SecurityId = -1; this.Name = ""; + this.Symbol = ""; this.Precision = -1; this.Type = -1; } @@ -100,6 +101,7 @@ Security.prototype.toJSON = function() { var json_obj = {}; json_obj.SecurityId = this.SecurityId; json_obj.Name = this.Name; + json_obj.Symbol = this.Symbol; json_obj.Precision = this.Precision; json_obj.Type = this.Type; return JSON.stringify(json_obj); @@ -112,6 +114,8 @@ Security.prototype.fromJSON = function(json_input) { this.SecurityId = json_obj.SecurityId; if (json_obj.hasOwnProperty("Name")) this.Name = json_obj.Name; + if (json_obj.hasOwnProperty("Symbol")) + this.Symbol = json_obj.Symbol; if (json_obj.hasOwnProperty("Precision")) this.Precision = json_obj.Precision; if (json_obj.hasOwnProperty("Type"))