Newer
Older
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
import React from 'react';
import { Form, Row, Col, Button } from 'react-bootstrap';
import { Typeahead } from 'react-bootstrap-typeahead';
import Fetcher from './Fetcher';
import { printMoney } from './util';
class ProductPicker extends React.Component {
constructor(props) {
super(props);
this.state = {
code: "",
products: [],
};
}
delPick(index) {
let picks = this.props.picks;
picks.splice(index, 1);
this.props.setPicks(picks);
}
setAmmount(index, ammount) {
let picks = this.props.picks;
picks[index].ammount = parseInt(ammount);
this.props.setPicks(picks);
}
setCode(codeStr) {
const code = parseInt(codeStr);
const product = this.state.products.find(p => p.code === code);
if (product === undefined) {
this.setState({ code: codeStr });
} else {
this.pickProduct(product);
}
}
pickProduct(product) {
let picks = this.props.picks;
picks.push({
code: product.code,
name: product.name,
price: product.price,
ammount: 1
});
this.props.setPicks(picks);
this.setState({ code: "" });
}
render() {
const rows = this.props.picks.map((p, i) => {
return (
<Form.Group key={p.code} as={Row}>
<Col>
<p>{p.code}</p>
</Col>
<Col sm={4}>
<p>{p.name}</p>
</Col>
<Col>
{printMoney(p.price)+"€"}
</Col>
{this.props.ammount &&
<Col>
<Form.Control
type="number" min="1"
placeholder="cantidad"
value={p.ammount}
onChange={e => this.setAmmount(i, e.target.value)}
/>
</Col>
}
<Col sm={1}>
<Button variant="danger" onClick={() => this.delPick(i)}>-</Button>
</Col>
</Form.Group>
)
});
return (
<Fetcher url="/api/product" onFetch={products => this.setState({ products })} >
<Row>
<Col>
<h6>Código</h6>
</Col>
<Col sm={4}>
<h6>Nombre</h6>
</Col>
<Col>
<h6>Precio</h6>
</Col>
{this.props.ammount &&
<Col>
<h6>Cantidad</h6>
</Col>
}
<Col sm={1}>
</Col>
</Row>
{rows}
<Form.Group as={Row}>
<Col>
<Form.Control
placeholder="codigo"
value={this.state.code}
onChange={e => this.setCode(e.target.value)}
/>
</Col>
<Col sm={4}>
<Typeahead
id="product-name"
labelKey="name"
options={this.state.products}
onChange={name => this.pickProduct(name[0])}
selected={[]}
/>
</Col>
<Col></Col>
{this.props.ammount &&
<Col></Col>
}
<Col sm={1}></Col>
</Form.Group>
</Fetcher>
);
}
}
export default ProductPicker;