1
0
mirror of https://github.com/franjsco/tick3t synced 2025-01-01 04:07:28 +01:00

improvement: various

This commit is contained in:
Francesco Esposito 2019-07-25 09:54:20 +02:00
parent 2267747fcd
commit 70a378f7cd

View File

@ -1,11 +1,16 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import { import {
Row, Row,
Col, Col,
Table Table
} from 'reactstrap'; } from 'reactstrap';
import { Link } from 'react-router-dom'; import PaginationComponent from "react-reactstrap-pagination";
import { formatDate, decode } from '../../utils/helper';
import { getTickets, findTicket, filterTicketStatus } from '../../api/tickets';
import { getAllTicketStatus } from '../../api/categories';
import Card from '../../components/Card'; import Card from '../../components/Card';
import SearchForm from '../../components/SearchForm'; import SearchForm from '../../components/SearchForm';
@ -15,69 +20,109 @@ import DropDown from '../../components/DropDown';
class TicketList extends Component { class TicketList extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
totalPages: 1,
selectedPage: 1,
selectedStatus: '',
data: [], data: [],
categories: [], status: [],
type: [],
error: '' error: ''
}; };
this.API_URL= process.env.REACT_APP_API_URL; this.filterId = this.filterId.bind(this);
this.filterStatus = this.filterStatus.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
this.handleSelected = this.handleSelected.bind(this);
} }
componentDidMount() { componentDidMount() {
fetch(`${this.API_URL}tickets?_limit=10`) this.setState({ isLoading: true });
.then(response => {
if (!response.ok) { getTickets()
throw new Error('Error API'); .then((json) => {
} this.setState({ data: json.data, isLoading: false, totalPages: json.totalPages });
return response.json();
}) })
.then(json => this.setState({ data: json, isLoading: false })) .catch((error) => {
this.setState({ error, isLoading: false })
});
getAllTicketStatus()
.then((json) => this.setState({ status: json.data }))
.catch(error => this.setState({ error, isLoading: false })); .catch(error => this.setState({ error, isLoading: false }));
}
fetch(`${this.API_URL}categories?type=ticketStatus`) handleInputChange(event) {
.then((response) => { const target = event.target;
if (!response.ok) { const value = target.value;
throw new Error('Error API');
}
return response.json(); this.setState({
}) ticketId: value
});
}
filterId(event) {
event.preventDefault();
this.setState({ isLoading: true });
const ticketId = this.state.ticketId || '';
findTicket(ticketId)
.then(json => this.setState({ data: json.data, isLoading: false, totalPages: json.totalPages }))
.catch(error => this.setState({ error, isLoading: false }));
}
filterStatus(event) {
const target = event.target;
const value = target.value;
this.setState({ selectedStatus: value })
getTickets(1, value)
.then(json => this.setState({ data: json.data, isLoading: false, totalPages: json.totalPages, selectedPage: 1 }))
.catch(error => this.setState({ error, isLoading: false }));
}
handleSelected(selectedPage) {
this.setState( { isLoading: true })
const selectedStatus = this.state.selectedStatus;
getTickets(selectedPage, selectedStatus)
.then((json) => { .then((json) => {
this.setState({ this.setState({ data: json.data, isLoading: false});
categories: json
});
}) })
.catch((err) => { .catch((error) => {
this.setState({ this.setState({ error, isLoading: false })
error: err.message
});
}); });
} }
render() { render() {
let tickets = this.state.data; let tickets = this.state.data;
if (tickets) {
if (tickets.length) {
tickets = tickets.map((ticket) => { tickets = tickets.map((ticket) => {
return ( return (
<tr> <tr key={ticket.ticketId}>
<th scope="row"> <th scope="row">
<Link to={`/admin/ticket/${ticket.id}`}> <Link to={`/admin/ticket/${ticket.ticketId}`}>
{ticket.id} {ticket.ticketId}
</Link> </Link>
</th> </th>
<td> <td>
{ticket.status} {decode(ticket.status)}
</td> </td>
<td> <td>
{ticket.type} {decode(ticket.type)}
</td> </td>
<td> <td>
{ticket.subject} {ticket.subject}
</td> </td>
<td>
{formatDate(ticket.updatedAt)}
</td>
</tr>); </tr>);
}); });
} }
@ -90,9 +135,8 @@ class TicketList extends Component {
name="search" name="search"
id="search" id="search"
placeholder="ticket ID" placeholder="ticket ID"
onChange={this.handleChangeInput} onSubmit={this.filterId}
onSubmit={this.handleSubmit} onChange={this.handleInputChange}
value={this.state.ticketId}
buttonName="Search" buttonName="Search"
/> />
@ -100,7 +144,8 @@ class TicketList extends Component {
<Col md={5}></Col> <Col md={5}></Col>
<Col md={3} className="float-right"> <Col md={3} className="float-right">
<DropDown <DropDown
options={this.state.categories} options={this.state.status}
onChange={this.filterStatus}
/> />
@ -108,21 +153,22 @@ class TicketList extends Component {
</Row> </Row>
<Row> <Row>
<Col> <Col>
<Table bordered responsive> <Table responsive>
<thead> <thead>
<tr> <tr>
<th>id</th> <th>id</th>
<th>status</th> <th>status</th>
<th>type</th> <th>type</th>
<th>subject</th> <th>subject</th>
<th>updated</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{tickets.length ? tickets : ( {tickets || (
<tr> <tr>
<td <td
className="text-center font-weight-bold" className="text-center font-weight-bold"
colspan="4" colspan="5"
> >
no ticket no ticket
</td> </td>
@ -131,6 +177,15 @@ class TicketList extends Component {
</Table> </Table>
</Col> </Col>
</Row> </Row>
<Row>
<Col>
<PaginationComponent
totalItems={this.state.totalPages}
pageSize={1}
onSelect={this.handleSelected}
/>
</Col>
</Row>
</Card> </Card>
); );
} }