This repository was archived by the owner on Jan 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
224 lines (192 loc) · 7.67 KB
/
app.js
File metadata and controls
224 lines (192 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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 (
<Fund
key={fund.id}
fund={fund}
maxValue={maxValue}
total={this.state.investmentValue}
index={index}
updateFund={this.updateFund} />
)
},
render: function() {
var totalSumOfFunds = this.state.sumTotalOfFunds,
investmentValue = this.state.investmentValue,
difference = this.state.difference;
var maxAllocationMsgClassName = classNames({
'allocation-msg': true,
'max-allocation-assigned': this.state.maxAllocationAssigned
})
return (
<div className="fund-allocator-app">
<Title text="Fund allocator demo" />
<h2>Investment value = {helpers.formatPrice(investmentValue)}</h2>
<h2>Total value of funds = {helpers.formatPrice(totalSumOfFunds)}</h2>
<h3>Current allocation = {helpers.formatPercent((totalSumOfFunds/investmentValue))}</h3>
<h4>Difference = {helpers.formatPercent((difference/investmentValue))}</h4>
<div className="total-indicator">
<div className="total-indicator__inner" style={{width: helpers.formatPercent((totalSumOfFunds/investmentValue))}}></div>
</div>
<div className="fund-panels-container">
{this.state.funds.map(this.renderFund)}
</div>
<FundControls autoAllocate={this.autoAllocate} clearAll={this.clearAll} />
<div className={maxAllocationMsgClassName}>
<span className="allocation-msg__text">{this.state.maxAllocationMessage}</span>
</div>
</div>
)
}
});
var Title = React.createClass({
render: function() {
return (
<h1>{this.props.text}</h1>
)
}
})
var Fund = React.createClass({
handleChange: function(event) {
var index = this.props.index,
newValue = event.target.value == "" ? 0 : Math.max(0, event.target.value);
if(newValue >= this.props.maxValue) {
newValue = this.props.maxValue;
}
this.props.updateFund(index, newValue);
},
render: function() {
var fund = this.props.fund,
fundValueInPercent = helpers.formatPercent((fund.value/this.props.total)),
fundValueInPrice = helpers.formatPrice(fund.value),
constaintValue = helpers.formatPercent((1 - (this.props.maxValue/this.props.total))),
inputValue = helpers.formatNumber(fund.value, '0[.]00');
return (
<div className='fund-panel'>
<h3>{fund.name} </h3>
<input type="number" value={inputValue} onChange={this.handleChange} className="fund-panel__field" />
<div className="fund-indicator">
<div className="fund-indicator__inner" style={{width: fundValueInPercent}}></div>
<div className="fund-indicator__constrain-marker" style={{width: constaintValue}}></div>
</div>
<p className="fund-panel__stats">Allocation: {fundValueInPercent} of {helpers.formatPrice(this.props.total)}
<br /> Current: {fundValueInPrice}
<br /> Max: {helpers.formatPrice(this.props.maxValue)}
</p>
</div>
)
}
});
var FundControls = React.createClass({
autoAllocate: function() {
this.props.autoAllocate();
},
clearAll: function() {
this.props.clearAll();
},
render: function() {
return(
<ul className="fund-controls">
<li><button type="button" title="Assign investment equally across all funds" onClick={this.autoAllocate}>Auto allocate</button></li>
<li><button type="button" onClick={this.clearAll}>Clear all</button></li>
</ul>
)
}
})
ReactDOM.render(<App title="Fund allocator" />, document.getElementById('FundAllocatorApp'));