-
Notifications
You must be signed in to change notification settings - Fork 1
/
Reserves.js
369 lines (336 loc) · 11.3 KB
/
Reserves.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
358
359
360
361
362
363
364
365
366
367
368
369
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import queryString from 'query-string';
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 FilterPaneSearch from '@folio/stripes-components/lib/FilterPaneSearch';
import Button from '@folio/stripes-components/lib/Button';
import MultiColumnList from '@folio/stripes-components/lib/MultiColumnList';
import makeQueryFunction from '@folio/stripes-components/util/makeQueryFunction';
import transitionToParams from '@folio/stripes-components/util/transitionToParams';
import packageInfo from './package';
/** stripes connect */
const INITIAL_RESULT_COUNT = 30;
const RESULT_COUNT_INCREMENT = 30;
const BIG_REQUEST_COUNT = 1000;
const reservesQueryFunc = makeQueryFunction(
'cql.allRecords=1',
'title="$QUERY*" or instructor="$QUERY*" or course="$QUERY*"',
{},
[{
label: 'Instructor',
name: 'instructor',
cql: 'instructorId',
values: []
}, {
label: 'Course',
name: 'course',
cql: 'courseId',
values: []
}],
);
class Reserves extends React.Component {
static manifest = Object.freeze({
itemCount: {
initialValue: INITIAL_RESULT_COUNT
},
items: {
type: 'okapi',
path: 'reserves',
records: 'items',
recordsRequired: '%{itemCount}',
perRequest: RESULT_COUNT_INCREMENT,
GET: {
params: {
query: reservesQueryFunc,
},
staticFallback: { params: {} },
},
},
courses: {
type: 'okapi',
path: 'courses',
records: 'courses',
perRequest: BIG_REQUEST_COUNT,
recordsRequired: BIG_REQUEST_COUNT
},
instructors: {
type: 'okapi',
path: 'instructors',
records: 'instructors',
perRequest: BIG_REQUEST_COUNT,
recordsRequired: BIG_REQUEST_COUNT
},
});
constructor(props) {
super(props);
// logger
const logger = props.stripes.logger;
this.log = logger.log.bind(logger);
// query
const query = props.location.search ? queryString.parse(props.location.search) : {};
this.transitionToParams = transitionToParams.bind(this);
// search
this.onChangeSearch = this.onChangeSearch.bind(this);
this.onClearSearch = this.onClearSearch.bind(this);
// filter
this.onChangeFilter = this.onChangeFilter.bind(this);
this.updateFilters = this.updateFilters.bind(this);
// multi-column-list
this.resultsList = null;
this.onSelectRow = this.onSelectRow.bind(this);
this.onSort = this.onSort.bind(this);
// react state
const filters = (query.filters || '').split(',').reduce(function (acc, filter) {
const tokens = filter.split(".");
if (tokens.length === 2) acc[tokens[0]] = tokens[1];
return acc;
}, {});
this.state = {
filters: filters,
searchTerm: query.query || '',
sortOrder: query.sort || '',
selectedCourseId: filters['course'] || '',
selectedInstructorId: filters['instructor'] || '',
selectedReserve: {},
};
};
/** react life-cycle */
componentWillReceiveProps(nextProps) {
const resource = this.props.resources.items;
if (resource) {
const sm = nextProps.resources.items.successfulMutations;
if (sm.length > resource.successfulMutations.length) {
this.onSelectRow(undefined, { id: sm[0].record.id });
}
}
};
/** filters */
onChangeFilter(e) {
this.log('action', 'onChangeFilter');
if (e.target.name === 'course') {
this.setState({ selectedCourseId: e.target.value });
} else if (e.target.name === 'instructor') {
this.setState({ selectedInstructorId: e.target.value });
}
const filters = Object.assign({}, this.state.filters);
filters[e.target.name] = e.target.value;
this.setState({ filters });
this.updateFilters(filters);
};
updateFilters(filters) {
this.transitionToParams({
filters: Object.keys(filters)
.filter(key => filters[key].length)
.map(key => `${key}.${filters[key]}`)
.join(',')
});
};
/** search */
performSearch = _.debounce((query) => {
this.log('action', `searched for '${query}'`);
this.transitionToParams({ query });
}, 250);
onChangeSearch(e) {
this.log('action', 'onChangeSearch');
this.props.mutator.itemCount.replace(INITIAL_RESULT_COUNT);
const query = e.target.value;
this.setState({ searchTerm: query });
this.log('action', `will search for '${query}'`);
this.performSearch(query);
}
onClearSearch() {
this.log('action', 'onClearSearch');
const appPath = (_.get(packageInfo, ['stripes', 'home']) ||
_.get(packageInfo, ['stripes', 'route']));
const path = `${appPath}/reserves`;
this.setState({
searchTerm: '',
sortOrder: 'title',
filters: {},
selectedCourseId: '',
selectedInstructorId: '',
selectedReserve: {},
});
this.props.history.push(path);
}
/** mutli-column-list */
onSelectRow(e, reserve) {
const reserveId = reserve.id;
this.log('action', `onSelectRow ${reserveId}`);
this.setState({ selectedReserve: reserve });
};
onSort(e, meta) {
const newOrder = meta.alias;
const oldOrder = this.state.sortOrder || '';
const orders = oldOrder ? oldOrder.split(',') : [];
if (orders[0] && newOrder === orders[0].replace(/^-/, '')) {
orders[0] = `-${orders[0]}`.replace(/^--/, '');
} else {
orders.unshift(newOrder);
}
const sortOrder = orders.slice(0, 2).join(',');
this.log('action', `sorted by ${sortOrder}`);
this.setState({ sortOrder });
this.transitionToParams({ sort: sortOrder });
};
onNeedMore = () => {
this.log('action', 'onNeedMore');
this.props.mutator.itemCount.replace(this.props.resources.itemCount + RESULT_COUNT_INCREMENT);
};
anchoredRowFormatter(
{ rowIndex,
rowClass,
rowData,
cells,
rowProps,
labelStrings,
},
) {
return (
<a
href={`/waitlists/reserves/${rowData.id}`}
key={`row-${rowIndex}`}
aria-label={labelStrings && labelStrings.join('...')}
role="listitem"
className={rowClass}
{...rowProps}
>
{cells}
</a>
);
};
render() {
this.log('action', 'render Reserves');
const {
onSubmit,
onClose,
resources,
} = this.props;
const closeButton = (
<PaneMenu>
<button onClick={onClose} title="close" aria-label="Close New Waitlist Dialog">
<span style={{ fontSize: '30px', color: '#999', lineHeight: '18px' }} >×</span>
</button>
</PaneMenu>
);
const submitButton = (
<PaneMenu>
<Button
id="clickable-create-waitlist"
title="Save"
onClick={() => onSubmit(this.state.selectedReserve)}
disabled={Object.keys(this.state.selectedReserve).length === 0}
buttonStyle="primary paneHeaderNewButton">
Save
</Button>
</PaneMenu>
);
const searchHeader = (
<FilterPaneSearch
id="SearchField"
onChange={this.onChangeSearch}
onClear={this.onClearSearch}
resultsList={this.resultsList}
value={this.state.searchTerm} />
);
const courses = (resources.courses || {}).records || [];
const courseOptions = courses.map(c => ({
label: c.name,
value: c.id,
selected: false,
}));
const instructors = (resources.instructors || {}).records || [];
const instructorOptions = instructors.map(i => ({
label: i.name,
value: i.id,
selected: false,
}));
const resultsFormatter = {};
const maybeTerm = this.state.searchTerm ? ` for "${this.state.searchTerm}"` : '';
const maybeSpelling = this.state.searchTerm ? 'spelling and ' : '';
const reserves = (resources.items || {}).records || [];
const itemCount = resources.itemCount || 0;
return (
<Layer isOpen={true} label="Add New Waitlist">
<Paneset>
{/** container pane */}
<Pane
defaultWidth="100%"
firstMenu={closeButton}
lastMenu={submitButton}>
<Paneset>
{/** filter pane */}
<Pane
id="filter-pane"
defaultWidth="30%"
header={searchHeader}>
<Row>
<Col xs={12}>
<Select
id="filteritem_course"
label="Course:"
name="course"
value={this.state.selectedCourseId}
fullWidth
onChange={this.onChangeFilter}
dataOptions={[{ label: 'Select Course', value: '' }, ...courseOptions]} />
</Col>
</Row>
<Row>
<Col xs={12}>
<Select
id="filteritem_instructor"
label="Instructor:"
name="instructor"
value={this.state.selectedInstructorId}
fullWidth
onChange={this.onChangeFilter}
dataOptions={[{ label: 'Select Instructor', value: '' }, ...instructorOptions]} />
</Col>
</Row>
</Pane>
{/** results pane */}
<Pane
id="results-pane"
paneTitle={
<div style={{ textAlign: 'center' }}>
<strong>Select Course Reserve</strong>
<div>
<em>{this.props.resources.items && this.props.resources.items.hasLoaded && this.props.resources.items.other ? this.props.resources.items.other.totalRecords : ''} Result{reserves.length === 1 ? '' : 's'} Found</em>
</div>
</div>
}
defaultWidth="70%">
<MultiColumnList
contentData={reserves}
selectedRow={this.state.selectedReserve}
rowMetadata={['title', 'instructor', 'course']}
formatter={{}}
onRowClick={this.onSelectRow}
onHeaderClick={this.onSort}
onNeedMoreData={this.onNeedMore}
visibleColumns={['title', 'barcode', 'location', 'instructor', 'course']}
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.reserves ? this.props.reserves.isPending : false}
autosize
virtualize
ariaLabel={'Item search results'}
rowFormatter={this.anchoredRowFormatter}
containerRef={(ref) => { this.resultsList = ref; }} />
</Pane>
</Paneset>
</Pane>
</Paneset>
</Layer>
);
}
}
export default Reserves;