-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
60 lines (53 loc) · 1.35 KB
/
App.js
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
import React, { Component } from "react";
import Variable from "./Variable";
import { initialState, calc, modelName, ModelResult } from './model';
import "./App.css";
function getVarValues(state) {
return Object.keys(state).reduce((values, varName) => {
values[varName] = state[varName].value;
return values;
}, {});
}
class App extends Component {
state = initialState;
onChange = (variable, newVal) => {
this.setState(state => ({
...state,
[variable]: {
...state[variable],
value: newVal
}
}));
};
render() {
const varValues = getVarValues(this.state);
const result = calc(varValues).toFixed(2);
const variables = Object.keys(this.state).map(varName => {
const varData = this.state[varName];
return (
<Variable
key={varName}
onChange={this.onChange}
name={varName}
values={varValues}
fn={calc}
{...varData}
/>
);
});
return (
<div className="App">
<div className="App-header colored-background-1">
<h2>{modelName}</h2>
</div>
<div className="Calculator">
{variables}
</div>
<div className="calculatorResult colored-background-1">
<ModelResult result={result} />
</div>
</div>
);
}
}
export default App;