-
Notifications
You must be signed in to change notification settings - Fork 42
/
LoanActionsHistory.js
329 lines (307 loc) · 13.1 KB
/
LoanActionsHistory.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
import _ from 'lodash';
import React from 'react';
import { FormattedMessage } from 'react-intl';
import Link from 'react-router-dom/Link';
import PropTypes from 'prop-types';
import Popover from '@folio/stripes-components/lib/Popover';
import { Row, Col } from '@folio/stripes-components/lib/LayoutGrid';
import Modal from '@folio/stripes-components/lib/Modal';
import KeyValue from '@folio/stripes-components/lib/KeyValue';
import MultiColumnList from '@folio/stripes-components/lib/MultiColumnList';
import Pane from '@folio/stripes-components/lib/Pane';
import Paneset from '@folio/stripes-components/lib/Paneset';
import Button from '@folio/stripes-components/lib/Button';
import Callout from '@folio/stripes-components/lib/Callout';
import SafeHTMLMessage from '@folio/react-intl-safe-html';
import { getFullName } from './util';
import loanActionMap from './data/loanActionMap';
import LoanActionsHistoryProxy from './LoanActionsHistoryProxy';
import withRenew from './withRenew';
/**
* Detail view of a user's loan.
*/
class LoanActionsHistory extends React.Component {
static propTypes = {
stripes: PropTypes.object.isRequired,
resources: PropTypes.shape({
loanActions: PropTypes.object,
loanActionsWithUser: PropTypes.object,
userIds: PropTypes.object,
timestamp: PropTypes.object,
}).isRequired,
mutator: PropTypes.shape({
loanActionsWithUser: PropTypes.shape({
replace: PropTypes.func,
}),
userIds: PropTypes.shape({
replace: PropTypes.func,
}),
timestamp: PropTypes.shape({
replace: PropTypes.func,
}),
}).isRequired,
loan: PropTypes.object,
patronGroup: PropTypes.object,
loanid: PropTypes.string,
user: PropTypes.object,
onCancel: PropTypes.func.isRequired,
onClickUser: PropTypes.func.isRequired,
renew: PropTypes.func,
};
static manifest = Object.freeze({
userIds: {},
loanActionsWithUser: {},
// this is another hack used to refresh loanActions after renew is executed
timestamp: { initialValue: { time: Date.now() } },
users: {
type: 'okapi',
records: 'users',
resourceShouldRefresh: true,
path: 'users?query=(%{userIds.query})',
},
loanActions: {
type: 'okapi',
records: 'loans',
resourceShouldRefresh: true,
GET: {
path: 'loan-storage/loan-history?query=(id==!{loanid})×tamp=%{timestamp.time}',
},
},
});
// TODO: refactor after join is supported in stripes-connect
static getDerivedStateFromProps(nextProps, prevState) {
const { loan, resources: { loanActions, userIds, users, loanActionsWithUser } } = nextProps;
if (!loanActions.records.length ||
loanActions.records[0].id !== loan.id) return null;
if (!userIds.query || userIds.loan.id !== loan.id) {
const query = loanActions.records.map(r => {
return `id==${r.metadata.updatedByUserId}`;
}).join(' or ');
this.props.mutator.userIds.replace({ query, loan });
}
if (!users.records.length) return null;
if (!loanActionsWithUser.records || loanActionsWithUser.loan.id !== loan.id
|| prevState.loanActionCount !== loanActions.other.totalRecords) {
this.joinLoanActionsWithUser(loanActions.records, users.records, loan);
return ({ loanActionCount: loanActions.other.totalRecords });
}
return null;
}
constructor(props) {
super(props);
this.connectedProxy = props.stripes.connect(LoanActionsHistoryProxy);
this.renew = this.renew.bind(this);
this.getContributorslist = this.getContributorslist.bind(this);
this.showContributors = this.showContributors.bind(this);
this.formatDateTime = this.props.stripes.formatDateTime;
this.hideNonRenewedLoansModal = this.hideNonRenewedLoansModal.bind(this);
this.showTitle = this.showTitle.bind(this);
this.state = {
loanActionCount: 0,
nonRenewedLoanItems: [],
nonRenewedLoansModalOpen: false
};
}
joinLoanActionsWithUser(loanActions, users, loan) {
const userMap = users.reduce((memo, user) => {
return Object.assign(memo, { [user.id]: user });
}, {});
const records = loanActions.map(la => {
return Object.assign({}, la, { user: userMap[la.metadata.updatedByUserId] });
});
this.props.mutator.loanActionsWithUser.replace({ loan, records });
}
showNonRenewedLoansModal() {
this.setState({ nonRenewedLoansModalOpen: true });
}
hideNonRenewedLoansModal() {
this.setState({ nonRenewedLoansModalOpen: false });
}
openNonRenewedLoansModal(nonRenewedLoanItems) {
this.setState({ nonRenewedLoanItems });
this.showNonRenewedLoansModal();
}
renew() {
const loanToRenew = this.props.loan;
const promise = this.props.renew(loanToRenew);
const singleRenewalFailure = [];
promise
.then(() => this.showCallout())
.catch(() => {
singleRenewalFailure.push(loanToRenew);
});
return promise;
}
getContributorslist(loan) {
this.loan = loan;
const contributors = _.get(this.loan, ['item', 'contributors']);
const contributorsList = [];
if (typeof contributors !== 'undefined') {
Object.keys(contributors).forEach(contributor => contributorsList.push(`${contributors[contributor].name}; `));
} else {
contributorsList.push('-');
}
return contributorsList;
}
showContributors(list, listTodisplay, contributorsLength) {
this.list = list;
return (contributorsLength >= 77) ?
(
<Popover>
<div data-role="target" style={{ cursor: 'pointer' }}><KeyValue label="Contributors" value={listTodisplay} /></div>
<div data-role="popover">
{
this.list.map(contributor => <p key={contributor}>{contributor}</p>)
}
</div>
</Popover>
) :
<KeyValue label={this.props.stripes.intl.formatMessage({ id: 'ui-users.loans.columns.contributors' })} value={listTodisplay} />;
}
showTitle(loan) {
this.loan = loan;
const title = `${_.get(this.loan, ['item', 'title'], '')}`;
if (title) {
const titleTodisplay = (title.length >= 77) ? `${title.substring(0, 77)}...` : title;
return <KeyValue label={this.props.stripes.intl.formatMessage({ id: 'ui-users.loans.columns.title' })} value={<Link to={`/inventory/view/${_.get(this.loan, ['item', 'instanceId'], '')}`}>{titleTodisplay}({_.get(this.loan, ['item', 'materialType', 'name'])})</Link>} />;
}
return '-';
}
showCallout() {
const message = (
<span>
<SafeHTMLMessage
id="ui-users.loans.item.renewed.callout"
values={{ title: this.props.loan.item.title }}
/>
</span>
);
this.callout.sendCallout({ message });
}
render() {
const { onCancel, loan, patronGroup, user, resources: { loanActionsWithUser }, stripes: { intl } } = this.props;
const { nonRenewedLoanItems } = this.state;
const loanActionsFormatter = {
action: la => intl.formatMessage({ id: loanActionMap[la.action] }),
actionDate: la => this.formatDateTime(la.loanDate),
dueDate: la => this.formatDateTime(la.dueDate),
itemStatus: la => la.itemStatus,
Source: la => <Link to={`/users/view/${la.user.id}`}>{getFullName(la.user)}</Link>,
};
const contributorsList = this.getContributorslist(loan);
const contributorsListString = contributorsList.join(' ');
const contributorsLength = contributorsListString.length;
// Number of characters to trucate the string = 77
const listTodisplay = (contributorsList === '-') ? '-' : (contributorsListString.length >= 77) ? `${contributorsListString.substring(0, 77)}...` : `${contributorsListString.substring(0, contributorsListString.length - 2)}`;
const nonRenewedLabel = this.props.stripes.intl.formatMessage({ id: 'ui-users.loans.items.nonRenewed.label' });
const failedRenewalsSubHeading = (
<p>
<FormattedMessage
id="ui-users.loans.items.nonRenewed.subHeading"
values={{
strongCount: <strong>{nonRenewedLoanItems.length}</strong>,
count: nonRenewedLoanItems.length,
verb: <strong>{this.props.stripes.intl.formatMessage({ id: 'ui-users.loans.item.nonRenewed.callout.verb' })}</strong>,
}}
/>
</p>
);
return (
<Paneset isRoot>
<Pane id="pane-loandetails" defaultWidth="100%" dismissible onClose={onCancel} paneTitle={`${intl.formatMessage({ id: 'ui-users.loans.loanDetails' })} - ${getFullName(user)} (${_.upperFirst(patronGroup.group)})`} >
<Row>
<Col>
<Button buttonStyle="primary" onClick={this.renew}>{this.props.stripes.intl.formatMessage({ id: 'ui-users.renew' })}</Button>
</Col>
</Row>
<Row>
<Col xs={2}>
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.details.borrower' })} value={`${getFullName(user)}`} />
</Col>
<Col xs={2} >
<this.connectedProxy id={loan.proxyUserId} onClick={this.props.onClickUser} {...this.props} />
</Col>
</Row>
<Row>
<Col xs={2}>
{this.showTitle(loan)}
</Col>
<Col xs={2}>
{this.showContributors(contributorsList, listTodisplay, contributorsLength)}
</Col>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.columns.barcode' })} value={<Link to={`/inventory/view/${_.get(loan, ['item', 'instanceId'], '')}/${_.get(loan, ['item', 'holdingsRecordId'], '')}/${_.get(loan, ['itemId'], '')}`}>{_.get(loan, ['item', 'barcode'], '')}</Link>} />
</Col>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.details.callNumber' })} value={_.get(loan, ['item', 'callNumber'], '-')} />
</Col>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.details.location' })} value={_.get(loan, ['item', 'location', 'name'], '-')} />
</Col>
</Row>
<Row>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.columns.itemStatus' })} value={_.get(loan, ['item', 'status', 'name'], '-')} />
</Col>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.columns.dueDate' })} value={this.formatDateTime(loan.dueDate) || '-'} />
</Col>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.columns.returnDate' })} value={this.formatDateTime(loan.systemReturnDate) || '-'} />
</Col>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.details.renewalCount' })} value={_.get(loan, ['renewalCount'], '-')} />
</Col>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.details.claimedReturned' })} value="TODO" />
</Col>
</Row>
<Row>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.details.loanPolicy' })} value="TODO" />
</Col>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.columns.loanDate' })} value={this.formatDateTime(loan.loanDate) || '-'} />
</Col>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.details.fine' })} value="TODO" />
</Col>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.details.requestQueue' })} value="TODO" />
</Col>
<Col xs={2} >
<KeyValue label={intl.formatMessage({ id: 'ui-users.loans.details.lost' })} value="TODO" />
</Col>
</Row>
<br />
{loanActionsWithUser && loanActionsWithUser.records &&
<MultiColumnList
id="list-loanactions"
formatter={loanActionsFormatter}
visibleColumns={['actionDate', 'action', 'dueDate', 'itemStatus', 'Source']}
columnMapping={{
action: intl.formatMessage({ id: 'ui-users.loans.columns.action' }),
actionDate: intl.formatMessage({ id: 'ui-users.loans.columns.actionDate' }),
dueDate: intl.formatMessage({ id: 'ui-users.loans.columns.dueDate' }),
itemStatus: intl.formatMessage({ id: 'ui-users.loans.columns.itemStatus' }),
Source: intl.formatMessage({ id: 'ui-users.loans.columns.source' }),
}}
contentData={loanActionsWithUser.records}
/>
}
<Modal dismissible closeOnBackgroundClick onClose={this.hideNonRenewedLoansModal} open={this.state.nonRenewedLoansModalOpen} label={nonRenewedLabel}>
{failedRenewalsSubHeading}
{
nonRenewedLoanItems.map((loanItem, index) => (
<li key={index}>
<span>{loanItem.item.title}</span>
</li>))
}
</Modal>
<Callout ref={(ref) => { this.callout = ref; }} />
</Pane>
</Paneset>
);
}
}
export default withRenew(LoanActionsHistory);