-
Notifications
You must be signed in to change notification settings - Fork 52
/
product.tsx
188 lines (174 loc) · 7.32 KB
/
product.tsx
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import React from 'react';
import { connect } from 'react-redux';
import { Link, RouteComponentProps } from 'react-router-dom';
import { Button, Col, Row, Table } from 'reactstrap';
// tslint:disable-next-line:no-unused-variable
import {
openFile,
byteSize,
Translate,
ICrudGetAllAction,
getSortState,
IPaginationBaseState,
getPaginationItemsNumber,
JhiPagination
} from 'react-jhipster';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { IRootState } from 'app/shared/reducers';
import { getEntities } from './product.reducer';
import { IProduct } from 'app/shared/model/product.model';
// tslint:disable-next-line:no-unused-variable
import { APP_DATE_FORMAT, APP_LOCAL_DATE_FORMAT } from 'app/config/constants';
import { ITEMS_PER_PAGE } from 'app/shared/util/pagination.constants';
export interface IProductProps extends StateProps, DispatchProps, RouteComponentProps<{ url: string }> {}
export type IProductState = IPaginationBaseState;
export class Product extends React.Component<IProductProps, IProductState> {
state: IProductState = {
...getSortState(this.props.location, ITEMS_PER_PAGE)
};
componentDidMount() {
this.getEntities();
}
sort = prop => () => {
this.setState(
{
order: this.state.order === 'asc' ? 'desc' : 'asc',
sort: prop
},
() => this.sortEntities()
);
};
sortEntities() {
this.getEntities();
this.props.history.push(`${this.props.location.pathname}?page=${this.state.activePage}&sort=${this.state.sort},${this.state.order}`);
}
handlePagination = activePage => this.setState({ activePage }, () => this.sortEntities());
getEntities = () => {
const { activePage, itemsPerPage, sort, order } = this.state;
this.props.getEntities(activePage - 1, itemsPerPage, `${sort},${order}`);
};
render() {
const { productList, match, totalItems } = this.props;
return (
<div>
<h2 id="product-heading">
<Translate contentKey="storeApp.product.home.title">Products</Translate>
<Link to={`${match.url}/new`} className="btn btn-primary float-right jh-create-entity" id="jh-create-entity">
<FontAwesomeIcon icon="plus" />
<Translate contentKey="storeApp.product.home.createLabel">Create new Product</Translate>
</Link>
</h2>
<div className="table-responsive">
<Table responsive>
<thead>
<tr>
<th className="hand" onClick={this.sort('id')}>
<Translate contentKey="global.field.id">ID</Translate> <FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={this.sort('name')}>
<Translate contentKey="storeApp.product.name">Name</Translate> <FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={this.sort('description')}>
<Translate contentKey="storeApp.product.description">Description</Translate> <FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={this.sort('price')}>
<Translate contentKey="storeApp.product.price">Price</Translate> <FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={this.sort('size')}>
<Translate contentKey="storeApp.product.size">Size</Translate> <FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={this.sort('image')}>
<Translate contentKey="storeApp.product.image">Image</Translate> <FontAwesomeIcon icon="sort" />
</th>
<th>
<Translate contentKey="storeApp.product.productCategory">Product Category</Translate> <FontAwesomeIcon icon="sort" />
</th>
<th />
</tr>
</thead>
<tbody>
{productList.map((product, i) => (
<tr key={`entity-${i}`}>
<td>
<Button tag={Link} to={`${match.url}/${product.id}`} color="link" size="sm">
{product.id}
</Button>
</td>
<td>{product.name}</td>
<td>{product.description}</td>
<td>{product.price}</td>
<td>
<Translate contentKey={`storeApp.Size.${product.size}`} />
</td>
<td>
{product.image ? (
<div>
<a onClick={openFile(product.imageContentType, product.image)}>
<img src={`data:${product.imageContentType};base64,${product.image}`} style={{ maxHeight: '30px' }} />
</a>
<span>
{product.imageContentType}, {byteSize(product.image)}
</span>
</div>
) : null}
</td>
<td>
{product.productCategory ? (
<Link to={`product-category/${product.productCategory.id}`}>{product.productCategory.name}</Link>
) : (
''
)}
</td>
<td className="text-right">
<div className="btn-group flex-btn-group-container">
<Button tag={Link} to={`${match.url}/${product.id}`} color="info" size="sm">
<FontAwesomeIcon icon="eye" />{' '}
<span className="d-none d-md-inline">
<Translate contentKey="entity.action.view">View</Translate>
</span>
</Button>
<Button tag={Link} to={`${match.url}/${product.id}/edit`} color="primary" size="sm">
<FontAwesomeIcon icon="pencil-alt" />{' '}
<span className="d-none d-md-inline">
<Translate contentKey="entity.action.edit">Edit</Translate>
</span>
</Button>
<Button tag={Link} to={`${match.url}/${product.id}/delete`} color="danger" size="sm">
<FontAwesomeIcon icon="trash" />{' '}
<span className="d-none d-md-inline">
<Translate contentKey="entity.action.delete">Delete</Translate>
</span>
</Button>
</div>
</td>
</tr>
))}
</tbody>
</Table>
</div>
<Row className="justify-content-center">
<JhiPagination
items={getPaginationItemsNumber(totalItems, this.state.itemsPerPage)}
activePage={this.state.activePage}
onSelect={this.handlePagination}
maxButtons={5}
/>
</Row>
</div>
);
}
}
const mapStateToProps = ({ product }: IRootState) => ({
productList: product.entities,
totalItems: product.totalItems
});
const mapDispatchToProps = {
getEntities
};
type StateProps = ReturnType<typeof mapStateToProps>;
type DispatchProps = typeof mapDispatchToProps;
export default connect(
mapStateToProps,
mapDispatchToProps
)(Product);