|
| 1 | +import { loanApi } from '@api/loans'; |
1 | 2 | import { invenioConfig } from '@config/index'; |
| 3 | +import { OverbookedConfirmModal } from '@components/OverbookedConfirmModal'; |
| 4 | +import { isDocumentOverbooked, isPrivilegedUser } from '@components/utils'; |
2 | 5 | import PropTypes from 'prop-types'; |
3 | 6 | import React, { Component } from 'react'; |
4 | 7 | import { Button, Icon, List, Modal } from 'semantic-ui-react'; |
| 8 | +import _get from 'lodash/get'; |
5 | 9 |
|
6 | 10 | export default class PatronBulkExtendLoans extends Component { |
7 | | - state = { open: false }; |
| 11 | + state = { |
| 12 | + open: false, |
| 13 | + showOverbookedConfirm: false, |
| 14 | + overbookedDocuments: [], |
| 15 | + isChecking: false, |
| 16 | + }; |
| 17 | + |
| 18 | + open = async () => { |
| 19 | + this.setState({ open: true }); |
| 20 | + |
| 21 | + if (!isPrivilegedUser()) return; |
| 22 | + |
| 23 | + this.setState({ isChecking: true }); |
| 24 | + |
| 25 | + try { |
| 26 | + const { patronPid } = this.props; |
| 27 | + // Get all active loans for the patron |
| 28 | + const query = loanApi |
| 29 | + .query() |
| 30 | + .withPatronPid(patronPid) |
| 31 | + .withState(invenioConfig.CIRCULATION.loanActiveStates) |
| 32 | + .withSize(invenioConfig.APP.PATRON_PROFILE_MAX_RESULTS_SIZE) |
| 33 | + .sortByNewest() |
| 34 | + .qs(); |
| 35 | + |
| 36 | + const response = await loanApi.list(query); |
| 37 | + const loans = response.data.hits; |
| 38 | + |
| 39 | + const checks = loans.map(async (loan) => { |
| 40 | + const documentPid = _get(loan, 'metadata.document.pid'); |
| 41 | + const isOverbooked = await isDocumentOverbooked(documentPid); |
| 42 | + |
| 43 | + if (isOverbooked) { |
| 44 | + return { |
| 45 | + title: loan.metadata.document.title, |
| 46 | + loanRequestId: loan.id, |
| 47 | + }; |
| 48 | + } |
| 49 | + return null; |
| 50 | + }); |
| 51 | + |
| 52 | + const results = await Promise.all(checks); |
| 53 | + const overbookedDocuments = results.filter(Boolean); |
| 54 | + |
| 55 | + this.setState({ |
| 56 | + overbookedDocuments, |
| 57 | + isChecking: false, |
| 58 | + }); |
| 59 | + } catch (error) { |
| 60 | + console.error('Failed to fetch overbooked documents', error); |
| 61 | + this.setState({ isChecking: false }); |
| 62 | + } |
| 63 | + }; |
8 | 64 |
|
9 | | - open = () => this.setState({ open: true }); |
10 | | - close = () => this.setState({ open: false }); |
| 65 | + close = () => { |
| 66 | + this.setState({ |
| 67 | + open: false, |
| 68 | + showOverbookedConfirm: false, |
| 69 | + overbookedDocuments: [], |
| 70 | + }); |
| 71 | + }; |
11 | 72 |
|
12 | 73 | handleSubmitExtend = () => { |
13 | 74 | const { bulkLoanExtension, patronPid } = this.props; |
| 75 | + const { overbookedDocuments } = this.state; |
| 76 | + |
| 77 | + // If the user is privileged and there are overbooked docs |
| 78 | + if (isPrivilegedUser() && overbookedDocuments.length > 0) { |
| 79 | + this.setState({ showOverbookedConfirm: true }); |
| 80 | + return; |
| 81 | + } |
| 82 | + // Otherwise extend directly |
| 83 | + bulkLoanExtension(patronPid); |
| 84 | + this.close(); |
| 85 | + }; |
| 86 | + |
| 87 | + confirmBulkExtension = () => { |
| 88 | + const { bulkLoanExtension, patronPid } = this.props; |
14 | 89 |
|
15 | 90 | bulkLoanExtension(patronPid); |
16 | 91 | this.close(); |
17 | 92 | }; |
18 | 93 |
|
19 | 94 | render() { |
20 | | - const { patronPid, bulkLoanExtension, isLoading, disabled, ...uiProps } = |
| 95 | + const { isLoading, disabled, patronPid, bulkLoanExtension, ...uiProps } = |
21 | 96 | this.props; |
22 | | - const { open } = this.state; |
| 97 | + const { open, showOverbookedConfirm, overbookedDocuments, isChecking } = |
| 98 | + this.state; |
| 99 | + |
23 | 100 | return ( |
24 | | - <Modal |
25 | | - open={open} |
26 | | - onClose={this.close} |
27 | | - onOpen={this.open} |
28 | | - trigger={ |
29 | | - <Button |
30 | | - labelPosition="left" |
31 | | - fluid |
32 | | - icon |
33 | | - primary |
34 | | - loading={isLoading} |
35 | | - disabled={disabled} |
36 | | - {...uiProps} |
37 | | - > |
38 | | - <Icon name="refresh" /> |
39 | | - Extend all loans |
40 | | - </Button> |
41 | | - } |
42 | | - > |
43 | | - <Modal.Header>Confirm extension action</Modal.Header> |
44 | | - <Modal.Content> |
45 | | - <Modal.Description> |
46 | | - The loan duration will be extended for:{' '} |
47 | | - <List bulleted> |
48 | | - <List.Item> |
49 | | - loans that will have to be returned in next{' '} |
50 | | - <b>{invenioConfig.CIRCULATION.loanWillExpireDays} days,</b> |
51 | | - </List.Item> |
52 | | - <List.Item> |
53 | | - loans that do not involve third party libraries (Interlibrary |
54 | | - Loans), |
55 | | - </List.Item> |
56 | | - <List.Item> |
57 | | - loans for <b>literature not in high demand by other users.</b> |
58 | | - </List.Item> |
59 | | - </List> |
60 | | - Do you want to continue? |
61 | | - </Modal.Description> |
62 | | - </Modal.Content> |
63 | | - <Modal.Actions> |
64 | | - <Button onClick={this.close}>Cancel</Button> |
65 | | - <Button onClick={this.handleSubmitExtend} primary> |
66 | | - Extend the loans |
67 | | - </Button> |
68 | | - </Modal.Actions> |
69 | | - </Modal> |
| 101 | + <> |
| 102 | + <OverbookedConfirmModal |
| 103 | + open={showOverbookedConfirm} |
| 104 | + onClose={() => this.setState({ showOverbookedConfirm: false })} |
| 105 | + onConfirm={this.confirmBulkExtension} |
| 106 | + overbookedDocuments={overbookedDocuments} |
| 107 | + /> |
| 108 | + |
| 109 | + <Modal |
| 110 | + open={open} |
| 111 | + onClose={this.close} |
| 112 | + onOpen={this.open} |
| 113 | + trigger={ |
| 114 | + <Button |
| 115 | + labelPosition="left" |
| 116 | + fluid |
| 117 | + icon |
| 118 | + primary |
| 119 | + loading={isLoading} |
| 120 | + disabled={disabled} |
| 121 | + {...uiProps} |
| 122 | + > |
| 123 | + <Icon name="refresh" /> |
| 124 | + Extend all loans |
| 125 | + </Button> |
| 126 | + } |
| 127 | + > |
| 128 | + <Modal.Header>Confirm extension action</Modal.Header> |
| 129 | + <Modal.Content> |
| 130 | + <Modal.Description> |
| 131 | + The loan duration will be extended for:{' '} |
| 132 | + <List bulleted> |
| 133 | + <List.Item> |
| 134 | + loans that will have to be returned in next{' '} |
| 135 | + <b>{invenioConfig.CIRCULATION.loanWillExpireDays} days,</b> |
| 136 | + </List.Item> |
| 137 | + <List.Item> |
| 138 | + loans that do not involve third party libraries (Interlibrary |
| 139 | + Loans), |
| 140 | + </List.Item> |
| 141 | + <List.Item> |
| 142 | + loans for <b>literature not in high demand by other users.</b> |
| 143 | + </List.Item> |
| 144 | + </List> |
| 145 | + Do you want to continue? |
| 146 | + </Modal.Description> |
| 147 | + </Modal.Content> |
| 148 | + <Modal.Actions> |
| 149 | + <Button onClick={this.close}>Cancel</Button> |
| 150 | + <Button |
| 151 | + onClick={this.handleSubmitExtend} |
| 152 | + primary |
| 153 | + loading={isChecking} |
| 154 | + > |
| 155 | + Extend the loans |
| 156 | + </Button> |
| 157 | + </Modal.Actions> |
| 158 | + </Modal> |
| 159 | + </> |
70 | 160 | ); |
71 | 161 | } |
72 | 162 | } |
|
0 commit comments