-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (153 loc) · 3.9 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import "bootstrap/dist/css/bootstrap.css";
import React, { useState } from "react";
import ReactDOM from "react-dom";
function LineItem(props) {
function calculateTotal() {
let { price, amount } = props;
let p = parseFloat(price);
let a = parseFloat(amount);
return isNaN(p) || isNaN(a) ? 0 : p * a;
}
function number() {
return parseInt(props.index) + 1;
}
const {
index,
price,
priceChanged,
amount,
amountChanged,
deleteLineItem
} = props;
return (
<tr>
<td>{number()}.</td>
<td>
<input name="title" className="form-control" />
</td>
<td>
<div className="input-group">
<div className="input-group-addon">$</div>
<input
name="price"
value={price}
className="form-control"
onChange={priceChanged.bind(null, index)}
/>
</div>
</td>
<td>
<input
name="amount"
value={amount}
className="form-control"
onChange={amountChanged.bind(null, index)}
/>
</td>
<td>
<h4>${calculateTotal()}</h4>
</td>
<td>
<button
className="btn btn-danger"
onClick={deleteLineItem.bind(null, index)}
>
<span className="glyphicon glyphicon-trash" />
</button>
</td>
</tr>
);
}
function InvoiceLineItems(props) {
const [lineItems, updateLineItems] = useState([{ price: "", amount: "" }]);
function changePrice(itemIndex, event) {
const price = event.target.value;
const updatedItems = lineItems.map((item, index) => {
if (index !== itemIndex) {
return item;
}
return { ...item, price };
});
updateLineItems(updatedItems);
}
function changeAmount(itemIndex, event) {
const amount = event.target.value;
const updatedItems = lineItems.map((item, index) => {
if (index !== itemIndex) {
return item;
}
return { ...item, amount };
});
updateLineItems(updatedItems);
}
function addLineItem(event) {
const newItem = { price: "", amount: "" };
const updatedItems = lineItems.concat(newItem);
updateLineItems(updatedItems);
}
function deleteLineItem(index, event) {
const updatedItems = lineItems.filter((_, i) => index !== i);
updateLineItems(updatedItems);
}
function totalPrice(price, amount) {
const p = parseFloat(price);
const a = parseFloat(amount);
return isNaN(p) || isNaN(a) ? 0 : p * a;
}
function calculateTotal() {
return lineItems
.map(i => totalPrice(i.price, i.amount))
.reduce((pv, cv) => pv + cv, 0);
}
function tableHeader() {
return (
<thead>
<tr>
<th width="1%">Nr</th>
<th width="55%">Name</th>
<th width="20%">Price</th>
<th width="10%">Amount</th>
<th width="10%">Total</th>
<th width="4%">Action</th>
</tr>
</thead>
);
}
function tableFooter() {
return (
<tfoot>
<tr>
<td colSpan="4" />
<th>
<h4>${calculateTotal()}</h4>
</th>
<td>
<button className="btn btn-success" onClick={addLineItem}>
<span className="glyphicon glyphicon-plus" />
</button>
</td>
</tr>
</tfoot>
);
}
return (
<table className="table table-bordered table-hover">
{tableHeader()}
<tbody>
{lineItems.map((item, index) => (
<LineItem
key={`item-${index}`}
index={index}
price={item.price}
amount={item.amount}
priceChanged={changePrice}
amountChanged={changeAmount}
deleteLineItem={deleteLineItem}
/>
))}
</tbody>
{tableFooter()}
</table>
);
}
ReactDOM.render(<InvoiceLineItems />, document.getElementById("root"));