blob: a7b294695306ad5a960e2a786c7a6ef0357fcf38 [file] [log] [blame]
arseniy.sorokinbdf2def2023-10-05 00:05:32 +02001import React, {Component} from 'react';
2import {Button, ButtonGroup, Container, Table} from 'reactstrap';
3import AppNavbar from './AppNavbar';
4import {Link} from 'react-router-dom';
5import {saveAs} from 'file-saver';
6
7class ClientList extends Component {
8
9 constructor(props) {
10 super(props);
11 this.state = {clients: []};
12 this.remove = this.remove.bind(this);
13 }
14
15 componentDidMount() {
16 fetch('/clients')
17 .then(response => response.json())
18 .then(data => this.setState({clients: data}));
19 }
20
21 async remove(id) {
22 await fetch(`/clients/${id}`, {
23 method: 'DELETE',
24 headers: {
25 'Accept': 'application/json',
26 'Content-Type': 'application/json'
27 }
28 }).then(() => {
29 let updatedClients = [...this.state.clients].filter(i => i.id !== id);
30 this.setState({clients: updatedClients});
31 });
32 }
33
34 async generateInvoice(nip) {
35 fetch(`/invoices`, {
36 method: 'POST',
37 body: JSON.stringify({nip: `${nip}`, monthOfInvoice: 'a1'}),
38 headers: {
39 'Accept': 'application/pdf',
40 'Content-Type': 'application/json'
41 }
42 })
43 .then(response => {
44 if (response.status === 401 || response.status === 403) {
45 alert("Error " + response.status);
46 sessionStorage.clear();
47 return;
48 }
49 const filename = response.headers.get("Content-Disposition").split("filename=")[1];
50 response.blob()
51 .then(blob => saveAs(new Blob([blob]), filename));
52 });
53 }
54
55 render() {
56 const {clients} = this.state;
57
58 const clientList = clients.map(client => {
59 return <tr key={client.nip}>
60 <td style={{whiteSpace: 'nowrap'}}>{client.name}</td>
61 <td>{client.nip}</td>
62 <td>
63 <ButtonGroup>
64 <Button size="sm" color="primary" tag={Link} to={"/clients/" + client.nip}>Edit</Button>
65 <Button size="sm" color="danger" onClick={() => this.remove(client.nip)}>Delete</Button>
66 <Button size="sm" color="primary" onClick={() => this.generateInvoice(client.nip)}>Generate
67 invoice</Button>
68 </ButtonGroup>
69 </td>
70 </tr>
71 });
72
73 return (
74 <div>
75 <AppNavbar/>
76 <Container fluid>
77 <div className="float-right">
78 <Button color="success" tag={Link} to="/clients/new">Add Client</Button>
79 </div>
80 <h3>Clients</h3>
81 <Table className="mt-4">
82 <thead>
83 <tr>
84 <th width="30%">Name</th>
85 <th width="30%">NIP</th>
86 <th width="40%">Actions</th>
87 </tr>
88 </thead>
89 <tbody>
90 {clientList}
91 </tbody>
92 </Table>
93 </Container>
94 </div>
95 );
96 }
97}
98
99export default ClientList;