|
| 1 | +import React from 'react'; |
| 2 | +import Router from 'next/router'; |
| 3 | +import Table from '@material-ui/core/Table'; |
| 4 | +import TableBody from '@material-ui/core/TableBody'; |
| 5 | +import TableCell from '@material-ui/core/TableCell'; |
| 6 | +import TableContainer from '@material-ui/core/TableContainer'; |
| 7 | +import Grid from "@material-ui/core/Grid"; |
| 8 | +import TableHead from '@material-ui/core/TableHead'; |
| 9 | +import TableRow from '@material-ui/core/TableRow'; |
| 10 | +import Paper from '@material-ui/core/Paper'; |
| 11 | +import Chip from "@material-ui/core/Chip"; |
| 12 | +import * as controller from "../../controllers/buyLink"; |
| 13 | +import CircularProgress from "@material-ui/core/CircularProgress"; |
| 14 | +import TablePagination from "@material-ui/core/TablePagination"; |
| 15 | +import { withStyles } from '@material-ui/core'; |
| 16 | +import Button from '@material-ui/core/Button'; |
| 17 | +import Swal from 'sweetalert2'; |
| 18 | +import Link from "next/link"; |
| 19 | +import withReactContent from 'sweetalert2-react-content'; |
| 20 | + |
| 21 | +const PaginationTheme = withStyles({ |
| 22 | + actions: { |
| 23 | + color: "red", |
| 24 | + backgroundColor: 'white', |
| 25 | + } |
| 26 | +})(TablePagination); |
| 27 | + |
| 28 | +const useStyles = theme => ({ |
| 29 | + root: { |
| 30 | + background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)', |
| 31 | + color: 'white', |
| 32 | + border: 0, |
| 33 | + borderRadius: 3, |
| 34 | + boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)' |
| 35 | + }, |
| 36 | +}); |
| 37 | + |
| 38 | +const ReactSwal = withReactContent(Swal); |
| 39 | + |
| 40 | +class BuyLinksTable extends React.Component { |
| 41 | + |
| 42 | + constructor(props) { |
| 43 | + super(props); |
| 44 | + this.state = { |
| 45 | + buyLinks: [], |
| 46 | + totalBuyLinks: 0, |
| 47 | + rowsPerPage: 10, |
| 48 | + currentPage: 0, |
| 49 | + totalPages: 0, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + resetPageInfo = () => { |
| 54 | + this.setState({ |
| 55 | + rowsPerPage: 10, |
| 56 | + currentPage: 0, |
| 57 | + totalPages: 0, |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + fillTable = () => { |
| 62 | + controller.searchBuyLinks(this.props.filterParams, { |
| 63 | + rowsPerPage: this.state.rowsPerPage, |
| 64 | + currentPage: this.state.currentPage, |
| 65 | + }).then((response) => { |
| 66 | + this.setState({ |
| 67 | + buyLinks: response.data.buyLinks, |
| 68 | + totalBuyLinks: response.data.totalBuyLinks, |
| 69 | + totalPages: response.data.pagesInfo.pageCount |
| 70 | + }) |
| 71 | + }).catch((error) => { |
| 72 | + Swal.fire({ |
| 73 | + type: 'error', |
| 74 | + title: 'Error while fetching buy links!', |
| 75 | + text: error |
| 76 | + }); |
| 77 | + this.setState({ |
| 78 | + loading: false |
| 79 | + }); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + componentDidMount() { |
| 84 | + this.fillTable() |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + handleChangePage = (event, newPage) => { |
| 89 | + this.setState({ |
| 90 | + currentPage: newPage |
| 91 | + }, () => { |
| 92 | + this.fillTable() |
| 93 | + }) |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + handleChangeRowsPerPage = (event) => { |
| 98 | + this.setState({ |
| 99 | + rowsPerPage: parseInt(event.target.value, 10), |
| 100 | + currentPage: 0 |
| 101 | + }, () => { |
| 102 | + this.fillTable() |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + render() { |
| 107 | + const { classes } = this.props; |
| 108 | + |
| 109 | + if (!this.state.buyLinks) { |
| 110 | + return ( |
| 111 | + <CircularProgress/> |
| 112 | + ) |
| 113 | + } |
| 114 | + return ( |
| 115 | + <div> |
| 116 | + <Grid xs={11} className={"mt-5 mr-5"}> |
| 117 | + <Paper> |
| 118 | + <TableContainer> |
| 119 | + <Grid container justify="center" className={"mb-1"}> |
| 120 | + <h2 className={"title"}> Buy Links</h2> |
| 121 | + </Grid> |
| 122 | + <Table aria-label="simple table"> |
| 123 | + <TableHead> |
| 124 | + <TableRow> |
| 125 | + <TableCell align="center" className={"red"}>USER</TableCell> |
| 126 | + <TableCell align="center" className={"red"}>PRODUCT</TableCell> |
| 127 | + <TableCell align="center" className={"red"}>URL</TableCell> |
| 128 | + <TableCell align="center" className={"red"}>CODE</TableCell> |
| 129 | + <TableCell align="center" className={"red"}>STATUS</TableCell> |
| 130 | + <TableCell align="center" className={"red"}>COUPON</TableCell> |
| 131 | + <TableCell align="center" className={"red"}>STATE</TableCell> |
| 132 | + <TableCell align="center" className={"red"}>CREDITS</TableCell> |
| 133 | + <TableCell align="center" className={"red"}>CREATED BY</TableCell> |
| 134 | + |
| 135 | + </TableRow> |
| 136 | + </TableHead> |
| 137 | + <TableBody> |
| 138 | + {this.state.buyLinks.map((buyLink) => ( |
| 139 | + <TableRow key={buyLink.id} |
| 140 | + style={{borderTop: '1px solid lightgrey'}}> |
| 141 | + <TableCell align="center"> |
| 142 | + {buyLink.user.firstname} {buyLink.user.lastname} |
| 143 | + </TableCell> |
| 144 | + <TableCell align="center"> |
| 145 | + {buyLink.product.description} |
| 146 | + </TableCell> |
| 147 | + <TableCell align="center"> |
| 148 | + {buyLink.short_url} |
| 149 | + </TableCell> |
| 150 | + <TableCell align="center"> |
| 151 | + {buyLink.code} |
| 152 | + </TableCell> |
| 153 | + <TableCell align="center"> |
| 154 | + {buyLink.active === true ? 'Active' : 'Inactive'} |
| 155 | + </TableCell> |
| 156 | + <TableCell align="center"> |
| 157 | + {buyLink.coupon ? buyLink.coupon.code : '-'} |
| 158 | + </TableCell> |
| 159 | + <TableCell align="center"> |
| 160 | + {buyLink.state ? buyLink.state.name : '-'} |
| 161 | + </TableCell> |
| 162 | + <TableCell align="center"> |
| 163 | + {buyLink.use_credits ? 'Applicable' : 'Not Applicable'} |
| 164 | + </TableCell> |
| 165 | + <TableCell align="center"> |
| 166 | + {buyLink.buyLink_creator.firstname} {buyLink.buyLink_creator.lastname} |
| 167 | + </TableCell> |
| 168 | + |
| 169 | + </TableRow> |
| 170 | + ))} |
| 171 | + </TableBody> |
| 172 | + </Table> |
| 173 | + </TableContainer> |
| 174 | + <PaginationTheme |
| 175 | + rowsPerPageOptions={[5,10]} |
| 176 | + component="div" |
| 177 | + count={this.state.totalBuyLinks} |
| 178 | + rowsPerPage={this.state.rowsPerPage} |
| 179 | + page={this.state.currentPage} |
| 180 | + onChangePage={this.handleChangePage} |
| 181 | + onChangeRowsPerPage={this.handleChangeRowsPerPage} |
| 182 | + /> |
| 183 | + </Paper> |
| 184 | + </Grid> |
| 185 | + </div> |
| 186 | + ); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +export default withStyles(useStyles)(BuyLinksTable) |
0 commit comments