-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathCustomerList.js
87 lines (77 loc) · 2.41 KB
/
CustomerList.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
import React, { Component } from 'react';
import { Button, ButtonGroup, Container, Table } from 'reactstrap';
import AppNavbar from './AppNavbar';
import { Link } from 'react-router-dom';
class CustomerList extends Component {
constructor(props) {
super(props);
this.state = {customers: [], isLoading: true};
this.remove = this.remove.bind(this);
}
componentDidMount() {
this.setState({isLoading: true});
fetch('api/customers')
.then(response => response.json())
.then(data => this.setState({customers: data, isLoading: false}));
}
async remove(id) {
await fetch(`/api/customer/${id}`, {
method: 'DELETE',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).then(() => {
let updatedCustomers = [...this.state.customers].filter(i => i.id !== id);
this.setState({customers: updatedCustomers});
});
}
render() {
const {customers, isLoading} = this.state;
if (isLoading) {
return <p>Loading...</p>;
}
const customerList = customers.map(customer => {
return <tr key={customer.id}>
<td style={{whiteSpace: 'nowrap'}}>{customer.firstname}</td>
<td>{customer.lastname}</td>
<td>{customer.age}</td>
<td>{customer.address}</td>
<td><a href={customer.copyright}>{customer.copyright}</a></td>
<td>
<ButtonGroup>
<Button size="sm" color="primary" tag={Link} to={"/customers/" + customer.id}>Edit</Button>
<Button size="sm" color="danger" onClick={() => this.remove(customer.id)}>Delete</Button>
</ButtonGroup>
</td>
</tr>
});
return (
<div>
<AppNavbar/>
<Container fluid>
<div className="float-right">
<Button color="success" tag={Link} to="/customers/new">Add Customer</Button>
</div>
<h3>Customer List</h3>
<Table className="mt-4">
<thead>
<tr>
<th width="20%">Firstname</th>
<th width="20%">Lastname</th>
<th width="10%">Age</th>
<th>Address</th>
<th>Copyrightby</th>
<th width="10%">Actions</th>
</tr>
</thead>
<tbody>
{customerList}
</tbody>
</Table>
</Container>
</div>
);
}
}
export default CustomerList;