-
Notifications
You must be signed in to change notification settings - Fork 1
/
Queuers.js
357 lines (319 loc) · 12.1 KB
/
Queuers.js
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import _ from 'lodash';
import moment from 'moment';
import uuid from 'uuid';
import React from 'react';
import PropTypes from 'prop-types';
import queryString from 'query-string';
import { Field, reduxForm } from 'redux-form';
import Link from 'react-router-dom/Link';
import Route from 'react-router-dom/Route';
import Layer from '@folio/stripes-components/lib/Layer';
import Paneset from '@folio/stripes-components/lib/Paneset';
import Pane from '@folio/stripes-components/lib/Pane';
import PaneMenu from '@folio/stripes-components/lib/PaneMenu';
import { Row, Col } from '@folio/stripes-components/lib/LayoutGrid';
import Select from '@folio/stripes-components/lib/Select';
import Button from '@folio/stripes-components/lib/Button';
import TextField from '@folio/stripes-components/lib/TextField';
import MultiColumnList from '@folio/stripes-components/lib/MultiColumnList';
import { onChangeFilter as commonChangeFilter } from '@folio/stripes-components/lib/FilterGroups';
import Timer from './Timer';
import QueuerForm from './QueuerForm';
// configuration
const TICK_INTERVAL = 5000;
const STOPPED = "stopped";
const STARTED = "started";
const PAUSED = "paused";
class Queuers extends React.Component {
static manifest = Object.freeze({
addQueuerMode: {
initialValue: {
mode: false
}
},
selectedWaitlist: {
type: 'okapi',
path: 'waitlists/:{waitlistid}',
},
queuers: {
type: 'okapi',
path: 'queuers',
records: 'queuers',
GET: {
params: {
query: 'waitlistId=:{waitlistid}',
},
},
},
pollWaitlist: {
type: 'okapi',
accumulate: true,
path: 'waitlists/:{waitlistid}',
},
pollQueuers: {
type: 'okapi',
accumulate: true,
path: 'queuers',
records: 'queuers',
GET: {
params: {
query: 'waitlistId=:{waitlistid}',
},
},
},
});
constructor(props, context) {
super(props);
// logger
const logger = props.stripes.logger;
this.log = logger.log.bind(logger);
// waitlist timer
this.onStopTimer = this.onStopTimer.bind(this);
this.onPauseTimer = this.onPauseTimer.bind(this);
this.onStartTimer = this.onStartTimer.bind(this);
// queuer layer
this.toggleNewQueuerLayer = this.toggleNewQueuerLayer.bind(this);
this.onClickSubmitNewQueuer = this.onClickSubmitNewQueuer.bind(this);
// multi-column-list
this.resultsList = null;
this.onSelectRow = this.onSelectRow.bind(this);
this.anchoredRowFormatter = this.anchoredRowFormatter.bind(this);
// react state
const query = props.location.search ? queryString.parse(props.location.search) : {};
this.state = {
selectedQueuer: {},
searchTerm: query.query || '',
sortOrder: query.sort || '',
waitlist: {},
queuers: [],
};
};
/** react life-cycle */
componentDidMount() {
this.timerId = setInterval(() => this.tick(), TICK_INTERVAL);
}
componentWillReceiveProps(nextProps) {
const next = nextProps.resources.queuers;
const current = this.props.resources.queuers;
const currentLoaded = current && current.hasLoaded;
const firstLoad = !currentLoaded && next.hasLoaded && next.records && next.records.length;
const diffEndpoint = current && next && next.url != current.url;
if (firstLoad || diffEndpoint) {
const waitlistid = nextProps.match.params.waitlistid;
const selectedWaitlist = (nextProps.resources.selectedWaitlist || {}).records || [];
const waitlist = selectedWaitlist.find(i => i.id === waitlistid) || {};
const queuers = (nextProps.resources.queuers || {}).records || [];
this.setState({
waitlist: waitlist,
queuers: queuers,
});
}
};
componentWillUnmount() {
clearInterval(this.timerId);
}
/** short polling */
tick() {
const { match: { params: { waitlistid } } } = this.props;
this.props.mutator.pollWaitlist.reset()
this.props.mutator.pollWaitlist.GET()
.then(waitlist => {
this.setState({ waitlist: waitlist });
});
this.props.mutator.pollQueuers.reset()
this.props.mutator.pollQueuers.GET({ params: { query: `waitlistId=${waitlistid}` } })
.then(queuers => {
this.setState({ queuers: queuers });
});
}
/** waitlist timer */
onStartTimer(e) {
// this.log('action', 'onStartTimer');
if (e) e.preventDefault();
this.updateTimer(STARTED);
}
onPauseTimer(e) {
// this.log('action', 'onPauseTimer');
if (e) e.preventDefault();
this.updateTimer(PAUSED);
}
onStopTimer(e) {
// this.log('action', 'onStopTimer');
if (e) e.preventDefault();
this.updateTimer(STOPPED);
}
updateTimer(timerState) {
const waitlist = Object.assign({}, this.state.waitlist);
waitlist['timerState'] = timerState;
this.props.mutator.selectedWaitlist.PUT(waitlist);
}
/** queuer */
createQueuer(queuerForm, waitlistId) {
return {
id: uuid(),
username: queuerForm.username,
email: queuerForm.email,
waitlistId: waitlistId
};
}
onClickSubmitNewQueuer(queuerForm) {
const isValid = queuerForm && queuerForm.username && queuerForm.email;
if (isValid) {
const { match: { params: { waitlistid } } } = this.props;
const queuer = this.createQueuer(queuerForm, waitlistid);
this.props.mutator.queuers.POST(queuer);
}
this.toggleNewQueuerLayer();
}
toggleNewQueuerLayer(e) {
if (e) e.preventDefault();
const { resources } = this.props;
const isOpen = resources.addQueuerMode ? !resources.addQueuerMode.mode : true;
this.props.mutator.addQueuerMode.replace({ mode: isOpen });
}
/** filters */
onChangeFilter(e) {
// this.log('action', 'onChangeFilter');
this.commonChangeFilter(e);
}
/** mutli-column-list */
onSelectRow(e, queuer) {
// this.log('action', `onSelectRow`);
};
onSort(e, meta) {
// this.log('action', `onSort`);
};
onNeedMore = () => {
// this.log('action', 'onNeedMore');
}
anchoredRowFormatter(
{
rowIndex,
rowClass,
rowData,
cells,
rowProps,
labelStrings,
},
) {
const { match: { params: { waitlistid } } } = this.props;
return (
<a
href={`/waitlists/${waitlistid}/queuers/${rowData.id}`}
key={`row-${rowIndex}`}
aria-label={labelStrings && labelStrings.join('...')}
role="listitem"
className={rowClass}
{...rowProps}
>
{cells}
</a>
);
};
render() {
// this.log('action', `render Queuers`);
const {
resources,
onCloseQueuers,
} = this.props;
// waitlist timer
const waitlist = this.state.waitlist;
const remainingTime = _.get(waitlist, 'remainingTime', -1);
const timerState = _.get(waitlist, 'timerState', STOPPED);
const isStarted = timerState === STARTED;
const isStopped = timerState === STOPPED;
const isPaused = timerState === PAUSED;
// queuers
const isQueuer = this.state.queuers.length === 0;
// queuer pane menu
const firstMenu = (
<PaneMenu>
<button onClick={onCloseQueuers} title="close" aria-label="Close New Waitlist Dialog">
<span style={{ fontSize: '30px', color: '#999', lineHeight: '18px' }} >×</span>
</button>
</PaneMenu>
);
const startBtn = (<Button id="clickable-start-queuer"
onClick={this.onStartTimer}
disabled={!isStarted && isQueuer}
title={isPaused ? "Resume" : "Start"}
buttonStyle="primary paneHeaderNewButton">{isPaused ? "Resume" : "Start"}</Button>)
const pauseBtn = (<Button id="clickable-pause-queuer"
onClick={this.onPauseTimer}
disabled={!isStarted}
title="Pause"
buttonStyle="primary paneHeaderNewButton">Pause</Button>)
const stopBtn = (<Button id="clickable-stop-timer"
onClick={this.onStopTimer}
disabled={isStopped}
title="Stop"
buttonStyle="primary paneHeaderNewButton">Stop</Button>)
const createBtn = (<Button id="clickable-new-queuer"
onClick={this.toggleNewQueuerLayer}
title="Create"
buttonStyle="primary paneHeaderNewButton">Create</Button>)
const lastMenu = (
<PaneMenu>
{isStarted ? pauseBtn : startBtn}
{stopBtn}
{createBtn}
</PaneMenu>
);
// multi-column-list
const resultsFormatter = {
'Date Created': x => moment(x['createDate']).format('MMMM Do YYYY, h:mm a')
};
const maybeTerm = false ? ` for "${this.state.searchTerm}"` : '';
const maybeSpelling = false ? 'spelling and ' : '';
return (
<Paneset>
{/** queuers */}
<Pane
id="main-pane"
paneTitle={
<div style={{ textAlign: 'center' }}>
<strong>Queuers</strong>
<div>
{!isStopped ? (
<Timer
startDate={moment()}
totalTime={remainingTime}
timerState={timerState} />
) : null}
</div>
</div>
}
defaultWidth="fill"
firstMenu={firstMenu}
lastMenu={lastMenu}>
<MultiColumnList
contentData={this.state.queuers}
selectedRow={this.state.selectedQueuer}
rowMetadata={['username']}
formatter={resultsFormatter}
onRowClick={this.onSelectRow}
onHeaderClick={this.onSort}
onNeedMoreData={this.onNeedMore}
visibleColumns={['username', 'email', 'Date Created']}
sortOrder={this.state.sortOrder.replace(/^-/, '').replace(/,.*/, '')}
sortDirection={this.state.sortOrder.startsWith('-') ? 'descending' : 'ascending'}
isEmptyMessage={`No results found${maybeTerm}. Please check your ${maybeSpelling}filters.`}
loading={this.props.queuers ? this.props.queuers.isPending : false}
autosize
virtualize
ariaLabel={'Queuer results'}
rowFormatter={this.anchoredRowFormatter}
containerRef={(ref) => { this.resultsList = ref; }} />
</Pane>
{/** queuer form */}
<Layer isOpen={resources.addQueuerMode ? resources.addQueuerMode.mode : false} label="Add New Queuer Dialog">
<QueuerForm
onSubmit={this.onClickSubmitNewQueuer}
onCancelNewQueuer={this.toggleNewQueuerLayer}
{...this.props} />
</Layer>
</Paneset>
);
}
}
export default Queuers;