forked from AdaGold/video-store-consumer
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCustomerCollection.js
45 lines (37 loc) · 1.18 KB
/
CustomerCollection.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
import React, { useState, useEffect} from 'react';
import PropTypes from 'prop-types';
import Customer from './Customer';
import axios from 'axios';
const API_URL_BASE = 'http://localhost:3000/customers';
const CustomerCollection = (props) => {
const [customerList, setCustomerList] = useState([]);
const [errorMessage, setErrorMessage] = useState(null);
useEffect(() => {
axios.get(API_URL_BASE)
.then((response) => {
setCustomerList(response.data);
})
.catch((error) => {
setErrorMessage(error.message);
});
}, []);
const customerComponents = customerList.map((customer, i) => {
return (
<div key={i}>
<Customer
customer={customer} onSelectCustomer={props.onSelectCustomer}
/>
</div>
);
});
return (
<div className = "CustomerCollection">
{errorMessage ? <div><h2 className="error-msg">{errorMessage}</h2></div> : ''}
{customerComponents}
</div>
);
};
CustomerCollection.propTypes = {
onSelectCustomer: PropTypes.func.isRequired,
};
export default CustomerCollection;