arseniy.sorokin | bdf2def | 2023-10-05 00:05:32 +0200 | [diff] [blame] | 1 | import React, { Component } from 'react'; |
| 2 | import { Button, ButtonGroup, Container, Table } from 'reactstrap'; |
| 3 | import AppNavbar from './AppNavbar'; |
| 4 | import { Link } from 'react-router-dom'; |
| 5 | |
| 6 | class InvoiceList extends Component { |
| 7 | |
| 8 | constructor(props) { |
| 9 | super(props); |
| 10 | this.state = {invoices: []}; |
| 11 | this.remove = this.remove.bind(this); |
| 12 | } |
| 13 | |
| 14 | componentDidMount() { |
| 15 | fetch('/invoices') |
| 16 | .then(response => response.json()) |
| 17 | .then(data => this.setState({invoices: data})); |
| 18 | } |
| 19 | |
| 20 | async remove(id) { |
| 21 | await fetch(`/invoices/${id}`, { |
| 22 | method: 'DELETE', |
| 23 | headers: { |
| 24 | 'Accept': 'application/json', |
| 25 | 'Content-Type': 'application/json' |
| 26 | } |
| 27 | }).then(() => { |
| 28 | let updatedClients = [...this.state.invoices].filter(i => i.id !== id); |
| 29 | this.setState({invoices: updatedClients}); |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | render() { |
| 34 | const {invoices} = this.state; |
| 35 | |
| 36 | const clientList = invoices.map(invoice => { |
| 37 | return <tr key={invoice.id}> |
| 38 | <td style={{whiteSpace: 'nowrap'}}>{invoice.invoiceTitle}</td> |
| 39 | <td>{invoice.id}</td> |
| 40 | <td> |
| 41 | <ButtonGroup> |
| 42 | <Button size="sm" color="primary" tag={Link} to={"/invoices/" + invoice.id}>Edit</Button> |
| 43 | <Button size="sm" color="danger" onClick={() => this.remove(invoice.id)}>Delete</Button> |
| 44 | </ButtonGroup> |
| 45 | </td> |
| 46 | </tr> |
| 47 | }); |
| 48 | |
| 49 | return ( |
| 50 | <div> |
| 51 | <AppNavbar/> |
| 52 | <Container fluid> |
| 53 | <div className="float-right"> |
| 54 | <Button color="success" tag={Link} to="/invoices/new">Add Invoice</Button> |
| 55 | </div> |
| 56 | <h3>Clients</h3> |
| 57 | <Table className="mt-4"> |
| 58 | <thead> |
| 59 | <tr> |
| 60 | <th width="30%">Id</th> |
| 61 | <th width="30%">Invoice Title</th> |
| 62 | <th width="40%">Actions</th> |
| 63 | </tr> |
| 64 | </thead> |
| 65 | <tbody> |
| 66 | {clientList} |
| 67 | </tbody> |
| 68 | </Table> |
| 69 | </Container> |
| 70 | </div> |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | export default InvoiceList; |