var React = require('react'),
ReactDOM = require('react-dom'),
classNames = require('classnames'),
helpers = require('./helpers'),
data = require('./data');
var App = React.createClass({
// set state
getInitialState: function() {
return {
investmentValue: data.value,
funds: data.funds,
sumTotalOfFunds: 0,
difference: 0,
maxAllocationAssigned: false,
maxAllocationMessage: "You have reached your max allocation"
}
},
// update state with total sum of funds
componentWillMount: function() {
// get the total value of all the funds and set to state
var sumTotal = this.getSumTotal(this.state.funds);
this.state.sumTotalOfFunds = sumTotal
// get the difference between total sum of funds and overal investment value
this.state.difference = this.getDifference(sumTotal, this.state.investmentValue);
// set state
this.setState({
sumTotalOfFunds: this.state.sumTotalOfFunds,
difference: this.state.difference
});
},
// spread the investment value equally amongst each of the funds
autoAllocate: function() {
var funds = this.state.funds,
fundsLength = funds.length,
investmentValue = this.state.investmentValue,
allocationValue = investmentValue/fundsLength;
funds.forEach((elem, i) => {
this.state.funds[i].value = allocationValue;
})
this.state.sumTotalOfFunds = investmentValue;
this.state.maxAllocationAssigned = true;
this.state.difference = 0;
this.setState({
funds: this.state.funds,
sumTotalOfFunds: this.state.sumTotalOfFunds,
maxAllocationAssigned: this.state.maxAllocationAssigned,
difference: this.state.difference
});
},
clearAll: function() {
var funds = this.state.funds;
funds.forEach((elem, i) => {
this.state.funds[i].value = 0;
});
this.state.sumTotalOfFunds = 0;
this.state.maxAllocationAssigned = false;
this.state.difference = 0;
this.setState({
funds: this.state.funds,
sumTotalOfFunds: this.state.sumTotalOfFunds,
maxAllocationAssigned: this.state.maxAllocationAssigned,
difference: this.state.difference
});
},
// returns total value of all funds
getSumTotal: (array) => {
var total = 0;
array.forEach(function(item) {
total += item.value;
});
return total;
},
getDifference: function(allocation, total) {
var difference = total - allocation;
return difference;
},
// updates fund value to new value
updateFund: function(index, newValue) {
var sumTotalOfFunds,
difference;
this.state.funds[index].value = parseInt(newValue);
sumTotalOfFunds = this.getSumTotal(this.state.funds);
this.state.sumTotalOfFunds = sumTotalOfFunds;
difference = this.getDifference(sumTotalOfFunds, this.state.investmentValue);
this.state.difference = difference;
// check if all the funds have been allocated
if(sumTotalOfFunds == this.state.investmentValue) {
this.state.maxAllocationAssigned = true;
}
this.setState({
funds: this.state.funds,
sumTotalOfFunds: this.state.sumTotalOfFunds,
maxAllocationAssigned: this.state.maxAllocationAssigned
});
},
// render a Fund panel
renderFund: function(fund, index) {
var maxValue = this.state.difference + fund.value;
return (
Allocation: {fundValueInPercent} of {helpers.formatPrice(this.props.total)}
Current: {fundValueInPrice}
Max: {helpers.formatPrice(this.props.maxValue)}