forked from AdaGold/video-store-consumer
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCustomers.js
64 lines (50 loc) · 1.55 KB
/
Customers.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
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
import Customer from './Customer';
import './Customers.css'
const Customers = (props) => {
const [customerList, setCustomerList] = useState([]);
const [errorMessage, setErrorMessage] = useState(null);
// initial API call to set state of customerList
useEffect(() => {
axios.get(`${props.url}/customers`)
.then((response) => {
const apiCustomerList = response.data;
setCustomerList(apiCustomerList);
})
.catch((error) => {
// Still need to handle errors
setErrorMessage(error.message);
});
}, []);
const onChangeValue = (event) => {
const customerID = Number(event.target.value)
const custObj = customerList.find((cust) => {
return (
cust.id === customerID
)
})
props.setCustomerIDCallback(customerID)
props.setCustomerNameCallback(custObj.name)
}
const customerComponents = customerList.map((cust) => {
return (
<div>
<input type='radio' id={cust.id} name='customer' value={cust.id} onChange={ onChangeValue } />
<label htmlFor={cust.name}><Customer key={cust.id} id={cust.id} name={cust.name} tel={cust.phone} videosCheckedCount={cust.videos_checked_out_count} /></label>
</div>
);
})
return (
<div>
<h1>Customer List:</h1>
{customerComponents}
</div>
)
}
Customers.propTypes = {
url: PropTypes.string,
setCustomerCallback: PropTypes.func
};
export default Customers;