/
solution.js
151 lines (138 loc) · 3.76 KB
/
solution.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
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
import React from 'react';
import ReactDOM from 'react-dom';
import 'bulma/css/bulma.css';
import './style.css';
import foods from './foods.js'
class Search extends React.Component {
render() {
return (
<div>
<input className="input search-bar" type="text" name="search" placeholder="Search"
value={this.props.search}
onChange={this.props.onChange}
/>
</div>
)
}
}
class FoodBox extends React.Component {
constructor(props) {
super(props);
this.state = {
qty: 1
}
}
handleChange(event) {
let v = event.target.value
this.setState({qty: v})
}
render() {
return (
<div className="box">
<article className="media">
<div className="media-left">
<figure className="image is-64x64">
<img src={this.props.food.image} />
</figure>
</div>
<div className="media-content">
<div className="content">
<p>
<strong>{this.props.food.name}</strong> <br />
<small>{this.props.food.calories} cal</small>
</p>
</div>
</div>
<div className="media-right">
<div className="field has-addons">
<div className="control">
<input
className="input"
type="number"
value={this.state.qty}
onChange={(event) => {this.handleChange(event)}}
/>
</div>
<div className="control">
<button
className="button is-info"
onClick={() => {this.props.onClick(this.props.food, this.state.qty)}}
>
+
</button>
</div>
</div>
</div>
</article>
</div>
)
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
search: "",
todaysFoods: []
}
}
handleClick(food, qty) {
let todaysFoods = this.state.todaysFoods.slice();
todaysFoods.push({...food, qty});
this.setState({
todaysFoods
})
}
displayFoods(search) {
search = search.toUpperCase();
let result = [];
for (let i = 0; i < foods.length; i++) {
if (foods[i].name.toUpperCase().indexOf(search) !== -1) {
result.push(
<FoodBox key={i} food={foods[i]} onClick={this.handleClick.bind(this)} />
)
}
}
return result;
}
displaySelectedFoods() {
let result = [];
for (let i = 0; i < this.state.todaysFoods.length; i++) {
result.push(
<li key={i}>{this.state.todaysFoods[i].qty} {this.state.todaysFoods[i].name} = {this.state.todaysFoods[i].qty * this.state.todaysFoods[i].calories} cal</li>
)
}
return result;
}
displaySelectedFoodsTotCal() {
let result = 0;
for (let i = 0; i < this.state.todaysFoods.length; i++) {
result += this.state.todaysFoods[i].qty * this.state.todaysFoods[i].calories;
}
return result;
}
render() {
return (
<div className="container">
<h1 className="title">IronNutrition</h1>
<Search search={this.state.search} onChange={(e) => {this.setState({search: e.target.value})}} />
<div className="columns">
<div className="column">
{this.displayFoods(this.state.search)}
</div>
<div className="column content">
<h2 className="subtitle">Today's foods</h2>
<ul>
{this.displaySelectedFoods()}
</ul>
Total: {this.displaySelectedFoodsTotCal()} cal
</div>
</div>
</div>
);
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
);