forked from AdaGold/video-store-consumer
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCustomers.js
61 lines (54 loc) · 1.53 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
import React, { useState, useEffect } 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('');
useEffect(() => {
axios.get(`${props.url}/customers`)
.then((response) => {
const apiCustomerList = response.data
setCustomerList(apiCustomerList);
})
.catch((error) => {
setErrorMessage(error.message);
});
}, []);
const customerComponents = customerList.map((customer) => {
return (<Customer
id={customer.id}
name={customer.name}
onClickCallback={props.onClickCallback}
registeredAt={customer.registered_at}
phone={customer.phone}
key={customer.id}
address={customer.address}
city={customer.city}
postalCode={customer.postal_code}
accountCredit={customer.account_credit}
videosCheckedOutCount={customer.videos_checked_out_count}
/>);
});
return (
<div>
<h2 className="header">Our Customers 📇</h2>
{errorMessage ? <p>{errorMessage}</p> : ''}
<table>
<tr>
<th>Name</th>
<th>Member Since</th>
<th># Videos Checked Out</th>
<th className="select-header"></th>
</tr>
{customerComponents}
</table>
</div>
);
};
Customers.propTypes = {
url: PropTypes.string.isRequired,
onClickCallback: PropTypes.string.isRequired
};
export default Customers;