-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
318 lines (293 loc) · 10.7 KB
/
index.html
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
<script type="text/jsx">
// TODO: Capture left over payments on months where there is a payoff
// TODO: Edit Loans
function clone(obj) {
var copy;
if (null == obj || "object" != typeof obj) return obj;
if (obj instanceof Array) {
copy = [];
for (var i = 0, len = obj.length; i < len; i++) {
copy[i] = clone(obj[i]);
}
return copy;
}
if (obj instanceof Object) {
copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}
var Loan = React.createClass({
render: function() {
return (
<tr className="loan">
<td>{this.props.name}</td>
<td>{this.props.balance}</td>
<td>{this.props.rate}</td>
<td>{this.props.minPayment}</td>
</tr>
);
}
});
var LoanList = React.createClass({
render: function() {
var loanNodes = this.props.loans.map(function(loan) {
return (
<Loan
balance={loan.balance}
rate={loan.rate}
name={loan.name}
minPayment={loan.minPayment}
/>
);
});
return (
<div className="col-md-4 loan-list">
<h3>Loans</h3>
<table className="table table-striped">
<thead>
<th>Name</th>
<th>Balance</th>
<th>Rate</th>
<th>Min. Payment</th>
</thead>
<tbody>{loanNodes}</tbody>
</table>
</div>
);
}
});
var LoanForm = React.createClass({
handleSubmit: function(event) {
event.preventDefault();
var name = React.findDOMNode(this.refs.name).value.trim();
var balance = parseFloat(React.findDOMNode(this.refs.balance).value.trim());
var rate = parseFloat(React.findDOMNode(this.refs.rate).value.trim());
var minPayment = parseFloat(React.findDOMNode(this.refs.minPayment).value.trim());
// TODO: Input Validation
this.props.onLoanSubmit({name: name, balance: balance, rate: rate, minPayment: minPayment});
React.findDOMNode(this.refs.name).value = "";
React.findDOMNode(this.refs.balance).value = "";
React.findDOMNode(this.refs.rate).value = "";
React.findDOMNode(this.refs.minPayment).value = "";
},
render: function() {
return (
<form className="form-inline loanForm" onSubmit={this.handleSubmit}>
<div className="form-group">
<input className="form-control" type="text" placeholder="Loan Name" ref="name" />
</div>
<div className="form-group">
<input className="form-control" type="number" step="0.01" placeholder="Balance" ref="balance" />
</div>
<div className="form-group">
<input className="form-control" type="number" step="0.01" placeholder="Interest Rate" ref="rate" />
</div>
<div className="form-group">
<input className="form-control" type="number" step="0.01" placeholder="Min. Monthly Payment" ref="minPayment" />
</div>
<input className="btn btn-primary" type="Submit" value="Add" />
</form>
);
}
});
var PayoffOptions = React.createClass({
handleSortChange: function(event) {
this.props.onSortChange(event.currentTarget.value);
},
handlePaymentChange: function(event) {
this.props.onPaymentChange(parseFloat(event.currentTarget.value));
},
render: function() {
return (
<div className="options-form col-md-4">
<h3>Payment Options</h3>
<form>
<div class="form-group">
<label for="extra">Extra Payment</label>
<input className="form-control" type="number" name="extra" placeholder="Extra Payment" onChange={this.handlePaymentChange} value={this.props.extraPayment} /><br/>
</div>
Payoff Order:
<div class="radio">
<label>
<input type="radio" name="sort" value="balance" onChange={this.handleSortChange} />
By Lowest Balance
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="sort" value="rate" onChange={this.handleSortChange} />
By Highest Rate
</label>
</div>
</form>
</div>
);
}
});
var BalanceAfterInterest = function(balance, rate) {
var periodicRate = (rate) / 365 / 100;
var interest = balance * periodicRate * 30;
return balance + interest;
};
var BalanceAfterPayment = function(loan, extraPayment) {
loan.balance = BalanceAfterInterest(loan.balance, loan.rate);
loan.balance -= (loan.minPayment + extraPayment);
if (loan.balance < 0) { loan.balance = 0; }
return Math.round(loan.balance * 100) / 100;
}
var AvailableMonthlyMin = function(loans) {
var avail = 0;
loans.forEach(function(loan) {
if (loan.balance == 0) {
avail += loan.minPayment
};
});
return avail;
}
var LoansToPayments = function(loans, extraPayment) {
loans = clone(loans);
var totalBalance = 100;
var months = 0;
var payments = [];
while(totalBalance > 0 && months < 100) {
var alreadyPaid = true;
var interBalance = 0;
loans.forEach(function(loan, index) {
var newBal = 0;
var availableMin = AvailableMonthlyMin(loans);
if (loan.balance > 0 && alreadyPaid) {
payments.push({
month: months,
name: loan.name,
payment: loan.minPayment + extraPayment + availableMin,
balance: BalanceAfterPayment(loan, extraPayment + availableMin)
});
alreadyPaid = false;
} else if (loan.balance > 0) {
payments.push({
month: months,
name: loan.name,
payment: loan.minPayment,
balance: BalanceAfterPayment(loan, 0)
});
}
interBalance += loan.balance;
});
months += 1;
totalBalance = interBalance;
}
return payments;
};
var PayoffSchedule = React.createClass({
render: function() {
var payments = LoansToPayments(this.props.loans, this.props.extraPayment);
var paymentNodes = payments.map(function(payment) {
return (
<tr>
<td>{payment.month}</td>
<td>{payment.name}</td>
<td>{payment.payment}</td>
<td>{payment.balance}</td>
</tr>
);
});
return (
<div className="payoff-table col-md-4">
<h3>Payment Schedule</h3>
<table className="table table-striped">
<thead>
<th>Month</th>
<th>Name</th>
<th>Payment</th>
<th>Balance</th>
</thead>
<tbody>
{paymentNodes}
</tbody>
</table>
</div>
);
}
});
var SortByRate = function(a, b) {
if (a.rate > b.rate) { return -1; }
else if (a.rate < b.rate) { return 1; }
else {
if (a.balance === b.balance) { return 0; }
return SortByBalance(a, b);
}
}
var SortByBalance = function(a, b) {
if (a.balance < b.balance) {
return -1;
} else if (a.balance > b.balance) {
return 1;
} else {
if (a.rate === b.rate) {
return 0;
} else {
return SortByRate(a, b);
}
}
}
var CalculatorBox = React.createClass({
handleLoanSubmit: function(loan) {
var data = this.state.loans;
data.push(loan);
if (this.state.sort == 'rate') {
data.sort(SortByRate);
} else {
data.sort(SortByBalance);
}
this.setState(data);
},
handleSortSubmit: function(direction) {
if (direction == "rate") {
this.setState(this.state.loans.sort(SortByRate));
} else {
this.setState(this.state.loans.sort(SortByBalance));
}
this.setState({sort: direction});
},
handlePaymentChange: function(paymentAmount) {
this.setState({extraPayment: paymentAmount});
},
getInitialState: function() {
return { loans: [], extraPayment: 0, sort: 'rate' };
},
render: function() {
return (
<div className="top-level">
<h1>Loan Calculator</h1>
<h3>Loans</h3>
<LoanForm onLoanSubmit={this.handleLoanSubmit} /><br/>
<div className="calculator-box row">
<PayoffOptions onSortChange={this.handleSortSubmit} onPaymentChange={this.handlePaymentChange} extraPayment={this.state.extraPayment} />
<LoanList loans={this.state.loans} />
<PayoffSchedule loans={this.state.loans} extraPayment={this.state.extraPayment} />
</div>
</div>
);
}
});
React.render(<CalculatorBox />, document.getElementById("main"));
</script>
<meta charset="utf-8">
<title>Loan Payment Calculator - React</title>
</head>
<body>
<div id="main" class="container"></div>
</body>
</html>