|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import Box from '@material-ui/core/Box'; |
| 3 | +import Collapse from '@material-ui/core/Collapse'; |
| 4 | +import CircularProgress from '@material-ui/core/CircularProgress'; |
| 5 | +import Table from '@material-ui/core/Table'; |
| 6 | +import TableBody from '@material-ui/core/TableBody'; |
| 7 | +import TableCell from '@material-ui/core/TableCell'; |
| 8 | +import TableHead from '@material-ui/core/TableHead'; |
| 9 | +import TableRow from '@material-ui/core/TableRow'; |
| 10 | +import Typography from '@material-ui/core/Typography'; |
| 11 | +import moment from 'moment'; |
| 12 | +import { PrimaryButton } from '../../common'; |
| 13 | +import { axios, getAuthHeaderConfig } from '../../../utils'; |
| 14 | +import * as API from '../../../constants/api'; |
| 15 | +import { useUser } from '../../../providers/UserProvider'; |
| 16 | +import { isUserDecisionMaker, isUserAdmin } from '../../../utils/helper/user'; |
| 17 | +import useConfirm from '../../../providers/ConfrimationModalProvider'; |
| 18 | +import { downloadAttachment } from '../attachments/AttachmentRow'; |
| 19 | +import { ATTACHMENT_TYPE, EXEMPTION_STATUS } from '../../../constants/variables'; |
| 20 | + |
| 21 | +const ExemptionDropdownList = ({ exemptions = [], open, onExemptionUpdate, onEditExemption }) => { |
| 22 | + const user = useUser(); |
| 23 | + const confirm = useConfirm(); |
| 24 | + const isAdminOrDecisionMaker = isUserDecisionMaker(user) || isUserAdmin(user); |
| 25 | + const [submittingId, setSubmittingId] = useState(null); |
| 26 | + const [error, setError] = useState(null); |
| 27 | + |
| 28 | + const onDownloadClicked = async (exemption) => { |
| 29 | + await axios |
| 30 | + .get(API.DOWNLOAD_AGREEMENT_EXEMPTION(exemption.agreementId, exemption.id), { |
| 31 | + ...getAuthHeaderConfig(), |
| 32 | + }) |
| 33 | + .then((response) => { |
| 34 | + const blob = new Blob([Buffer.from(response.data, 'base64')], { |
| 35 | + type: 'application/pdf', |
| 36 | + }); |
| 37 | + const url = window.URL.createObjectURL(blob); |
| 38 | + const link = document.createElement('a'); |
| 39 | + link.href = url; |
| 40 | + link.download = `${exemption.agreementId}_Exemption${exemption.id}.pdf`; |
| 41 | + link.style.display = 'none'; |
| 42 | + link.click(); |
| 43 | + window.URL.revokeObjectURL(url); |
| 44 | + link.remove(); |
| 45 | + |
| 46 | + exemption.attachments.forEach((attachment) => { |
| 47 | + downloadAttachment(attachment.id, attachment.name, ATTACHMENT_TYPE.EXEMPTION_ATTACHMENT); |
| 48 | + }); |
| 49 | + }); |
| 50 | + }; |
| 51 | + |
| 52 | + const onTransitionClicked = async (exemption, action) => { |
| 53 | + const actionTexts = { |
| 54 | + approve: 'Approve', |
| 55 | + reject: 'Reject', |
| 56 | + cancel: 'Cancel', |
| 57 | + }; |
| 58 | + |
| 59 | + const choice = await confirm({ |
| 60 | + titleText: `${actionTexts[action]} Exemption`, |
| 61 | + contentText: `Are you sure you want to ${action.toLowerCase()} this exemption?`, |
| 62 | + confirmText: actionTexts[action], |
| 63 | + cancelText: 'Back', |
| 64 | + }); |
| 65 | + |
| 66 | + if (!choice) return; |
| 67 | + |
| 68 | + setSubmittingId(exemption.id); |
| 69 | + setError(null); |
| 70 | + try { |
| 71 | + await axios.post( |
| 72 | + API.TRANSITION_EXEMPTION(exemption.agreementId, exemption.id), |
| 73 | + { action, comment: '' }, |
| 74 | + { ...getAuthHeaderConfig() }, |
| 75 | + ); |
| 76 | + if (onExemptionUpdate) { |
| 77 | + onExemptionUpdate(); |
| 78 | + } |
| 79 | + } catch (err) { |
| 80 | + setError(err.response?.data?.message || 'Failed to perform action'); |
| 81 | + console.error('Error transitioning exemption:', err); |
| 82 | + } finally { |
| 83 | + setSubmittingId(null); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + return ( |
| 88 | + <TableRow> |
| 89 | + <TableCell |
| 90 | + colSpan={isAdminOrDecisionMaker ? 14 : 12} |
| 91 | + style={{ paddingBottom: 0, paddingTop: 0, borderBottom: 'none' }} |
| 92 | + > |
| 93 | + <Table> |
| 94 | + <TableRow> |
| 95 | + <TableCell style={{ paddingBottom: 0, paddingTop: 0, borderBottom: 'none' }}> |
| 96 | + <Collapse in={open} timeout="auto" unmountOnExit> |
| 97 | + <Box margin={0} style={{ marginBottom: '10px' }}> |
| 98 | + <Typography variant="h6" gutterBottom component="div"> |
| 99 | + Exemption |
| 100 | + </Typography> |
| 101 | + {error && ( |
| 102 | + <Typography style={{ color: 'red', marginBottom: '10px' }} variant="body2"> |
| 103 | + {error} |
| 104 | + </Typography> |
| 105 | + )} |
| 106 | + <Table size="small"> |
| 107 | + <TableHead> |
| 108 | + <TableRow> |
| 109 | + <TableCell style={{ color: 'grey', width: 175 }}>Reason</TableCell> |
| 110 | + <TableCell style={{ color: 'grey', width: 175 }}>Submited By</TableCell> |
| 111 | + <TableCell style={{ color: 'grey', width: 175 }}>Submission Date</TableCell> |
| 112 | + <TableCell style={{ color: 'grey', width: 175 }}>Start Date</TableCell> |
| 113 | + <TableCell style={{ color: 'grey', width: 175 }}>End Date</TableCell> |
| 114 | + <TableCell style={{ color: 'grey', width: 175 }}>Approved By</TableCell> |
| 115 | + <TableCell style={{ color: 'grey', width: 175 }}>Approval Date</TableCell> |
| 116 | + <TableCell style={{ color: 'grey', width: 175, align: 'left' }}>Status</TableCell> |
| 117 | + <TableCell style={{ color: 'grey', width: 175 }}>Download</TableCell> |
| 118 | + {isAdminOrDecisionMaker && ( |
| 119 | + <> |
| 120 | + <TableCell style={{ color: 'grey', width: 175 }}>Approve/Reject</TableCell> |
| 121 | + <TableCell style={{ color: 'grey', width: 175 }}>Cancel</TableCell> |
| 122 | + <TableCell style={{ color: 'grey', width: 175 }}>View/Edit</TableCell> |
| 123 | + </> |
| 124 | + )} |
| 125 | + </TableRow> |
| 126 | + </TableHead> |
| 127 | + <TableBody> |
| 128 | + {exemptions.map((exemption, index) => { |
| 129 | + return ( |
| 130 | + <React.Fragment key={index}> |
| 131 | + <TableRow key={index} hover={true}> |
| 132 | + <TableCell>{exemption.reason}</TableCell> |
| 133 | + <TableCell> |
| 134 | + {exemption?.user.givenName} {exemption?.user.familyName} |
| 135 | + </TableCell> |
| 136 | + <TableCell> |
| 137 | + {exemption.createdAt ? moment(exemption.createdAt).format('MMM DD YYYY') : ''} |
| 138 | + </TableCell> |
| 139 | + <TableCell> |
| 140 | + {exemption.startDate ? moment(exemption.startDate).format('MMM DD YYYY') : ''} |
| 141 | + </TableCell> |
| 142 | + <TableCell> |
| 143 | + {exemption.endDate ? moment(exemption.endDate).format('MMM DD YYYY') : ''} |
| 144 | + </TableCell> |
| 145 | + <TableCell> |
| 146 | + {exemption.approvedByUser |
| 147 | + ? `${exemption.approvedByUser.givenName} ${exemption.approvedByUser.familyName}` |
| 148 | + : exemption.approvedBy} |
| 149 | + </TableCell> |
| 150 | + <TableCell> |
| 151 | + {exemption.approvalDate |
| 152 | + ? moment(exemption.approvalDate).format('MMM DD YYYY') |
| 153 | + : exemption.approvedAt |
| 154 | + ? moment(exemption.approvedAt).format('MMM DD YYYY') |
| 155 | + : ''} |
| 156 | + </TableCell> |
| 157 | + <TableCell>{exemption.status}</TableCell> |
| 158 | + <TableCell> |
| 159 | + <PrimaryButton |
| 160 | + icon |
| 161 | + inverted |
| 162 | + onClick={() => { |
| 163 | + onDownloadClicked(exemption); |
| 164 | + }} |
| 165 | + > |
| 166 | + <i className="download icon" /> |
| 167 | + </PrimaryButton> |
| 168 | + </TableCell> |
| 169 | + {isAdminOrDecisionMaker && ( |
| 170 | + <> |
| 171 | + <TableCell> |
| 172 | + {exemption.status === EXEMPTION_STATUS.PENDING_APPROVAL && |
| 173 | + (submittingId === exemption.id ? ( |
| 174 | + <CircularProgress size={24} /> |
| 175 | + ) : ( |
| 176 | + <> |
| 177 | + <PrimaryButton |
| 178 | + icon |
| 179 | + inverted |
| 180 | + onClick={() => onTransitionClicked(exemption, 'approve')} |
| 181 | + > |
| 182 | + <i className="thumbs up icon" /> |
| 183 | + </PrimaryButton> |
| 184 | + <PrimaryButton |
| 185 | + icon |
| 186 | + inverted |
| 187 | + onClick={() => onTransitionClicked(exemption, 'reject')} |
| 188 | + > |
| 189 | + <i className="thumbs down icon" /> |
| 190 | + </PrimaryButton> |
| 191 | + </> |
| 192 | + ))} |
| 193 | + </TableCell> |
| 194 | + <TableCell> |
| 195 | + {exemption.status !== EXEMPTION_STATUS.CANCELLED && |
| 196 | + (submittingId === exemption.id ? ( |
| 197 | + <CircularProgress size={24} /> |
| 198 | + ) : ( |
| 199 | + <PrimaryButton |
| 200 | + icon |
| 201 | + inverted |
| 202 | + onClick={() => onTransitionClicked(exemption, 'cancel')} |
| 203 | + > |
| 204 | + <i className="x icon" /> |
| 205 | + </PrimaryButton> |
| 206 | + ))} |
| 207 | + </TableCell> |
| 208 | + <TableCell> |
| 209 | + {exemption.status === EXEMPTION_STATUS.REJECTED || |
| 210 | + (isAdminOrDecisionMaker && |
| 211 | + exemption.status === EXEMPTION_STATUS.PENDING_APPROVAL) ? ( |
| 212 | + submittingId === exemption.id ? ( |
| 213 | + <CircularProgress size={24} /> |
| 214 | + ) : ( |
| 215 | + <PrimaryButton |
| 216 | + icon |
| 217 | + inverted |
| 218 | + onClick={() => onEditExemption && onEditExemption(exemption)} |
| 219 | + > |
| 220 | + <i className="edit icon" /> |
| 221 | + </PrimaryButton> |
| 222 | + ) |
| 223 | + ) : ( |
| 224 | + <PrimaryButton |
| 225 | + icon |
| 226 | + inverted |
| 227 | + onClick={() => |
| 228 | + onEditExemption && onEditExemption({ ...exemption, viewOnly: true }) |
| 229 | + } |
| 230 | + > |
| 231 | + <i className="eye icon" /> |
| 232 | + </PrimaryButton> |
| 233 | + )} |
| 234 | + </TableCell> |
| 235 | + </> |
| 236 | + )} |
| 237 | + </TableRow> |
| 238 | + </React.Fragment> |
| 239 | + ); |
| 240 | + })} |
| 241 | + </TableBody> |
| 242 | + </Table> |
| 243 | + </Box> |
| 244 | + </Collapse> |
| 245 | + </TableCell> |
| 246 | + </TableRow> |
| 247 | + </Table> |
| 248 | + </TableCell> |
| 249 | + </TableRow> |
| 250 | + ); |
| 251 | +}; |
| 252 | + |
| 253 | +export default ExemptionDropdownList; |
0 commit comments