-
Notifications
You must be signed in to change notification settings - Fork 13
/
points.tsx
237 lines (217 loc) · 8.5 KB
/
points.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import './point.scss';
import React from 'react';
import { connect } from 'react-redux';
import { Link, RouteComponentProps } from 'react-router-dom';
import { Button, InputGroup, Col, Row, Table, Tooltip, Popover, PopoverHeader, PopoverBody } from 'reactstrap';
import { AvForm, AvGroup, AvInput } from 'availity-reactstrap-validation';
// tslint:disable-next-line:no-unused-variable
import {
ICrudSearchAction,
ICrudGetAllAction,
TextFormat,
getSortState,
IPaginationBaseState,
getPaginationItemsNumber,
JhiPagination
} from 'react-jhipster';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { library } from '@fortawesome/fontawesome-svg-core';
import { faCheck } from '@fortawesome/free-solid-svg-icons/faCheck';
import { faTimes } from '@fortawesome/free-solid-svg-icons/faTimes';
library.add(faCheck);
library.add(faTimes);
import { IRootState } from 'app/shared/reducers';
import { getSearchEntities, getEntities } from './points.reducer';
import { IPoints } from 'app/shared/model/points.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 IPointsProps extends StateProps, DispatchProps, RouteComponentProps<{ url: string }> {}
export interface IPointsState extends IPaginationBaseState {
search: string;
tooltipOpen: boolean;
popoverOpen: boolean;
}
export class Points extends React.Component<IPointsProps, IPointsState> {
state: IPointsState = {
search: '',
tooltipOpen: false,
popoverOpen: false,
...getSortState(this.props.location, ITEMS_PER_PAGE)
};
componentDidMount() {
this.getEntities();
}
search = () => {
if (this.state.search) {
this.props.getSearchEntities(this.state.search);
}
};
toggleTooltip = () => {
this.setState({
tooltipOpen: !this.state.tooltipOpen
});
};
togglePopover = () => {
this.setState({
popoverOpen: !this.state.popoverOpen
});
}
clear = () => {
this.props.getEntities();
this.setState({
search: ''
});
};
handleSearch = event => this.setState({ search: event.target.value });
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 { pointsList, match, totalItems } = this.props;
return (
<div>
<Row>
<Col sm="8">
<h2 id="points-heading">
Daily Points
</h2>
</Col>
<Col sm="4">
<AvForm onSubmit={this.search}>
<AvGroup>
<InputGroup className="w-100 mr-1">
<AvInput type="text" name="search" value={this.state.search} onChange={this.handleSearch} placeholder="Search" />
<Button className="input-group-addon">
<FontAwesomeIcon icon="search" />
</Button>
<Button type="reset" className="input-group-addon" onClick={this.clear}>
<FontAwesomeIcon icon="trash" />
</Button>
<Link to={`${match.url}/new`} className="btn btn-primary float-right jh-create-entity" id="jh-create-entity">
<FontAwesomeIcon icon="plus" />
</Link>
<Tooltip placement="top" isOpen={this.state.tooltipOpen} target="jh-create-entity" toggle={this.toggleTooltip}>
Add Points
</Tooltip>
</InputGroup>
</AvGroup>
</AvForm>
</Col>
</Row>
<div className="table-responsive">
<Table responsive striped size="sm">
<thead>
<tr>
<th className="hand" onClick={this.sort('date')}>
Date <FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={this.sort('excercise')}>
Excercise <FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={this.sort('meals')}>
Meals <FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={this.sort('alcohol')}>
Alcohol <FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={this.sort('notes')}>
Notes <FontAwesomeIcon icon="sort" />
</th>
<th>
User <FontAwesomeIcon icon="sort" />
</th>
<th />
</tr>
</thead>
<tbody>
{pointsList.map((points, i) => (
<tr key={`entity-${i}`}>
<td>
<Button tag={Link} to={`${match.url}/${points.id}`} color="link" size="sm">
<TextFormat type="date" value={points.date} format={APP_LOCAL_DATE_FORMAT} />
</Button>
</td>
<td className="text-center">
{ points.excercise ?
<i><FontAwesomeIcon icon={faCheck} className="text-success"/></i>
: <i><FontAwesomeIcon icon={faTimes} className="text-danger"/></i> }
</td>
<td className="text-center">
{ points.meals ?
<i><FontAwesomeIcon icon={faCheck} className="text-success"/></i>
: <i><FontAwesomeIcon icon={faTimes} className="text-danger"/></i> }
</td>
<td className="text-center">
{ points.alcohol ?
<i><FontAwesomeIcon icon={faCheck} className="text-success"/></i>
: <i><FontAwesomeIcon icon={faTimes} className="text-danger"/></i> }
</td>
<td>
<div>
<div className="truncate" id="Popover1" onClick={this.togglePopover}>{points.notes}</div>
<Popover placement="right" isOpen={this.state.popoverOpen} target="Popover1" toggle={this.togglePopover}>
<PopoverBody>{points.notes}</PopoverBody>
</Popover>
</div>
</td>
<td>{points.userLogin ? points.userLogin : ''}</td>
<td className="text-right">
<div className="btn-group flex-btn-group-container">
<Button tag={Link} to={`${match.url}/${points.id}`} color="info" size="sm">
<FontAwesomeIcon icon="eye" /> <span className="d-none d-md-inline">View</span>
</Button>
<Button tag={Link} to={`${match.url}/${points.id}/edit`} color="primary" size="sm">
<FontAwesomeIcon icon="pencil-alt" /> <span className="d-none d-md-inline">Edit</span>
</Button>
<Button tag={Link} to={`${match.url}/${points.id}/delete`} color="danger" size="sm">
<FontAwesomeIcon icon="trash" /> <span className="d-none d-md-inline">Delete</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 = ({ points }: IRootState) => ({
pointsList: points.entities,
totalItems: points.totalItems
});
const mapDispatchToProps = {
getSearchEntities,
getEntities
};
type StateProps = ReturnType<typeof mapStateToProps>;
type DispatchProps = typeof mapDispatchToProps;
export default connect(
mapStateToProps,
mapDispatchToProps
)(Points);