forked from folio-org/ui-inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Instances.js
379 lines (341 loc) · 12 KB
/
Instances.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
370
371
372
373
374
375
376
377
378
379
import React from 'react';
import PropTypes from 'prop-types';
import queryString from 'query-string';
import _ from 'lodash';
import SearchAndSort from '@folio/stripes-smart-components/lib/SearchAndSort';
import { filters2cql, onChangeFilter as commonChangeFilter } from '@folio/stripes-components/lib/FilterGroups';
import transitionToParams from '@folio/stripes-components/util/transitionToParams';
import removeQueryParam from '@folio/stripes-components/util/removeQueryParam';
import packageInfo from './package';
import InstanceForm from './edit/InstanceForm';
import ViewInstance from './ViewInstance';
import formatters from './referenceFormatters';
const INITIAL_RESULT_COUNT = 30;
const RESULT_COUNT_INCREMENT = 30;
const emptyObj = {};
const emptyArr = [];
const languages = [
{ code: 'eng', name: 'English' },
{ code: 'spa', name: 'Spanish' },
{ code: 'fre', name: 'French' },
{ code: 'ger', name: 'German' },
{ code: 'chi', name: 'Mandarin' },
{ code: 'rus', name: 'Russian' },
{ code: 'ara', name: 'Arabic' },
];
// the empty 'values' properties will be filled in by componentWillUpdate
// as those are pulled from the backend
const filterConfig = [
{
label: 'Resource Type',
name: 'resource',
cql: 'instanceTypeId',
values: [],
},
{
label: 'Language',
name: 'language',
cql: 'languages',
values: languages.map(rec => ({ name: rec.name, cql: rec.code })),
},
{
label: 'Location',
name: 'location',
cql: 'holdingsRecords.permanentLocationId',
values: [],
},
];
class Instances extends React.Component {
static manifest = Object.freeze({
query: {
initialValue: {
query: '',
filters: '',
sort: 'title',
},
},
resultCount: { initialValue: INITIAL_RESULT_COUNT },
records: {
type: 'okapi',
records: 'instances',
recordsRequired: '%{resultCount}',
perRequest: 30,
path: 'instance-storage/instances',
GET: {
params: {
query: (...args) => {
/*
This code is not DRY as it is copied from makeQueryFunction in stripes-components.
This is necessary, as makeQueryFunction only references query parameters as a data source.
STRIPES-480 is intended to correct this and allow this query function to be replace with a call
to makeQueryFunction.
https://issues.folio.org/browse/STRIPES-480
*/
const resourceData = args[2];
const sortMap = {
Title: 'title',
publishers: 'publication',
Contributors: 'contributors',
};
let cql = `(title="${resourceData.query.query}*" or contributors adj "\\"name\\": \\"${resourceData.query.query}*\\"" or identifiers adj "\\"value\\": \\"${resourceData.query.query}*\\"")`;
const filterCql = filters2cql(filterConfig, resourceData.query.filters);
if (filterCql) {
if (cql) {
cql = `(${cql}) and ${filterCql}`;
} else {
cql = filterCql;
}
}
const { sort } = resourceData.query;
if (sort) {
const sortIndexes = sort.split(',').map((sort1) => {
let reverse = false;
if (sort1.startsWith('-')) {
// eslint-disable-next-line no-param-reassign
sort1 = sort1.substr(1);
reverse = true;
}
let sortIndex = sortMap[sort1] || sort1;
if (reverse) {
sortIndex = `${sortIndex.replace(' ', '/sort.descending ')}/sort.descending`;
}
return sortIndex;
});
cql += ` sortby ${sortIndexes.join(' ')}`;
}
return cql;
},
},
staticFallback: { params: {} },
},
},
identifierTypes: {
type: 'okapi',
records: 'identifierTypes',
path: 'identifier-types?limit=100&query=cql.allRecords=1 sortby name',
},
contributorTypes: {
type: 'okapi',
records: 'contributorTypes',
path: 'contributor-types?limit=100&query=cql.allRecords=1 sortby name',
},
contributorNameTypes: {
type: 'okapi',
records: 'contributorNameTypes',
path: 'contributor-name-types?limit=100&query=cql.allRecords=1 sortby name',
},
instanceFormats: {
type: 'okapi',
records: 'instanceFormats',
path: 'instance-formats?limit=100&query=cql.allRecords=1 sortby name',
},
instanceTypes: {
type: 'okapi',
records: 'instanceTypes',
path: 'instance-types?limit=100&query=cql.allRecords=1 sortby name',
},
classificationTypes: {
type: 'okapi',
records: 'classificationTypes',
path: 'classification-types?limit=100&query=cql.allRecords=1 sortby name',
},
locations: {
type: 'okapi',
records: 'shelflocations',
path: 'shelf-locations?limit=100&query=cql.allRecords=1 sortby name',
},
});
constructor(props) {
super(props);
const query = props.location.search ? queryString.parse(props.location.search) : {};
this.state = {
sortOrder: query.sort || '',
};
this.transitionToParams = transitionToParams.bind(this);
this.removeQueryParam = removeQueryParam.bind(this);
this.cViewInstance = this.props.stripes.connect(ViewInstance);
this.resultsList = null;
this.SRStatus = null;
this.onChangeFilter = commonChangeFilter.bind(this);
this.copyInstance = this.copyInstance.bind(this);
}
/**
* fill in the filter values
*/
componentWillUpdate() {
// resource types
const rt = (this.props.resources.instanceTypes || {}).records || [];
if (rt && rt.length) {
filterConfig[0].values = rt.map(rec => ({ name: rec.name, cql: rec.id }));
}
// locations
const locations = (this.props.resources.locations || {}).records || [];
if (locations && locations.length) {
filterConfig[2].values = locations.map(rec => ({ name: rec.name, cql: rec.id }));
}
}
onClearSearch = () => {
const path = (_.get(packageInfo, ['stripes', 'home']) ||
_.get(packageInfo, ['stripes', 'route']));
this.setState({
sortOrder: 'title',
});
this.props.history.push(path);
}
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.setState({ sortOrder });
this.transitionToParams({ sort: sortOrder });
}
onClickAddNewInstance = (e) => {
if (e) e.preventDefault();
this.transitionToParams({ layer: 'create' });
}
onChangeSearch = (e) => {
this.props.mutator.resultCount.replace(INITIAL_RESULT_COUNT);
const query = e.target.value;
this.performSearch(query);
}
onChangeFilter = (e) => {
this.props.mutator.resultCount.replace(INITIAL_RESULT_COUNT);
this.commonChangeFilter(e);
}
openInstance(selectedInstance) {
const instanceId = selectedInstance.id;
this.props.history.push(`/inventory/view/${instanceId}${this.props.location.search}`);
}
updateFilters(filters) { // provided for onChangeFilter
this.transitionToParams({ filters: Object.keys(filters).filter(key => filters[key]).join(',') });
}
closeNewInstance = (e) => {
if (e) e.preventDefault();
this.setState({ copiedInstance: null });
this.removeQueryParam('layer');
}
copyInstance(instance) {
this.setState({ copiedInstance: _.omit(instance, ['id']) });
this.transitionToParams({ layer: 'create' });
}
createInstance = (instance) => {
// POST item record
this.props.mutator.records.POST(instance).then(() => {
this.closeNewInstance();
});
}
performSearch = _.debounce((query) => {
this.transitionToParams({ query });
}, 250);
collapseDetails = () => {
this.props.history.push(`${this.props.match.path}${this.props.location.search}`);
};
render() {
const { resources } = this.props;
const contributorTypes = (resources.contributorTypes || emptyObj).records || emptyArr;
const contributorNameTypes = (resources.contributorNameTypes || emptyObj).records || emptyArr;
const identifierTypes = (resources.identifierTypes || emptyObj).records || emptyArr;
const classificationTypes = (resources.classificationTypes || emptyObj).records || emptyArr;
const instanceTypes = (resources.instanceTypes || emptyObj).records || emptyArr;
const instanceFormats = (resources.instanceFormats || emptyObj).records || emptyArr;
const shelfLocations = (resources.locations || emptyObj).records || emptyArr;
const referenceTables = {
contributorTypes,
contributorNameTypes,
identifierTypes,
classificationTypes,
instanceTypes,
instanceFormats,
shelfLocations,
};
const initialPath = (_.get(packageInfo, ['stripes', 'home']) ||
_.get(packageInfo, ['stripes', 'route']));
const resultsFormatter = {
publishers: r => r.publication.map(p => `${p.publisher} ${p.dateOfPublication ? `(${p.dateOfPublication})` : ''}`).join(', '),
'publication date': r => r.publication.map(p => p.dateOfPublication).join(', '),
contributors: r => formatters.contributorsFormatter(r, contributorTypes),
};
return (<SearchAndSort
moduleName={packageInfo.name.replace(/.*\//, '')}
moduleTitle={packageInfo.stripes.displayName}
objectName="inventory"
baseRoute={packageInfo.stripes.route}
initialPath={initialPath}
maxSortKeys={1}
filterConfig={filterConfig}
initialResultCount={INITIAL_RESULT_COUNT}
resultCountIncrement={RESULT_COUNT_INCREMENT}
viewRecordComponent={ViewInstance}
editRecordComponent={InstanceForm}
newRecordInitialValues={(this.state.copiedInstance) ? this.state.copiedInstance : { source: 'manual' }}
visibleColumns={['title', 'contributors', 'publishers']}
columnWidths={{ title: '40%' }}
resultsFormatter={resultsFormatter}
onCreate={this.createInstance}
viewRecordPerms="inventory-storage.instances.item.get"
newRecordPerms="inventory-storage.instances.item.post"
disableRecordCreation={false}
parentResources={this.props.resources}
parentMutator={this.props.mutator}
detailProps={{ referenceTables, onCopy: this.copyInstance }}
path={`${this.props.match.path}/view/:id/:holdingsrecordid?/:itemid?`}
/>);
}
}
Instances.propTypes = {
stripes: PropTypes.shape({
connect: PropTypes.func.isRequired,
locale: PropTypes.string.isRequired,
}).isRequired,
resources: PropTypes.shape({
records: PropTypes.shape({
hasLoaded: PropTypes.bool.isRequired,
other: PropTypes.shape({
totalRecords: PropTypes.number,
total_records: PropTypes.number,
}),
successfulMutations: PropTypes.arrayOf(
PropTypes.shape({
record: PropTypes.shape({
id: PropTypes.string.isRequired,
}).isRequired,
}),
),
}),
resultCount: PropTypes.number,
instanceTypes: PropTypes.shape({
records: PropTypes.arrayOf(PropTypes.object),
}),
locations: PropTypes.shape({
records: PropTypes.arrayOf(PropTypes.object),
}),
}).isRequired,
history: PropTypes.shape({
push: PropTypes.func.isRequired,
}).isRequired,
location: PropTypes.shape({
pathname: PropTypes.string.isRequired,
search: PropTypes.string,
}).isRequired,
match: PropTypes.shape({
path: PropTypes.string.isRequired,
}).isRequired,
mutator: PropTypes.shape({
addInstanceMode: PropTypes.shape({
replace: PropTypes.func,
}),
records: PropTypes.shape({
POST: PropTypes.func,
}),
resultCount: PropTypes.shape({
replace: PropTypes.func,
}),
}).isRequired,
};
export default Instances;