|
| 1 | +import { Prompt } from 'react-router-dom' |
| 2 | +import { useEffect, useRef, useState } from 'react' |
| 3 | +import { getIsRequestAborted } from '@Common/Api' |
| 4 | +import { showError } from '@Common/Helper' |
| 5 | +import { ServerErrors } from '@Common/ServerError' |
| 6 | +import { ApiQueuingWithBatch } from '@Shared/API' |
| 7 | +import { usePrompt } from '@Shared/Hooks' |
| 8 | +import { ConfirmationModal, ToastManager, ToastVariantType } from '@Shared/index' |
| 9 | +import { noop } from 'rxjs' |
| 10 | +import { OperationResultStore } from './OperationResultStore' |
| 11 | +import BulkOperationsResultModal from './BulkOperationsResultModal' |
| 12 | +import { OperationResultStoreType, BulkOperationModalProps } from './types' |
| 13 | +import './styles.scss' |
| 14 | + |
| 15 | +const BulkOperations = ({ |
| 16 | + handleModalClose: handleModalCloseProp, |
| 17 | + operations: userOperations = [], |
| 18 | + getResultsChartSummaryText, |
| 19 | + handleReloadDataAfterBulkOperation = noop, |
| 20 | + hideResultsDrawer = false, |
| 21 | + textConfig, |
| 22 | + shouldSkipConfirmation, |
| 23 | + confirmationModalConfig, |
| 24 | + data, |
| 25 | +}: BulkOperationModalProps) => { |
| 26 | + const [apiCallInProgress, setApiCallInProgress] = useState(!!shouldSkipConfirmation) |
| 27 | + const [, setLastUpdateTimeStamp] = useState(new Date().toISOString()) |
| 28 | + const operationsRef = useRef<BulkOperationModalProps['operations']>(userOperations) |
| 29 | + const resultsStoreRef = useRef<OperationResultStoreType>(new OperationResultStore(operationsRef.current.length)) |
| 30 | + const abortControllerRef = useRef<AbortController>(new AbortController()) |
| 31 | + |
| 32 | + const showResultsDrawer = !hideResultsDrawer && !!resultsStoreRef.current.getSize() |
| 33 | + |
| 34 | + usePrompt({ |
| 35 | + shouldPrompt: apiCallInProgress, |
| 36 | + }) |
| 37 | + |
| 38 | + const handleModalClose = () => { |
| 39 | + const hasAnyOperationSucceeded = resultsStoreRef.current.getHasAnyOperationSucceeded() |
| 40 | + |
| 41 | + if (hasAnyOperationSucceeded || hideResultsDrawer) { |
| 42 | + handleReloadDataAfterBulkOperation() |
| 43 | + } |
| 44 | + |
| 45 | + handleModalCloseProp() |
| 46 | + } |
| 47 | + |
| 48 | + const handleBulkOperations = async () => { |
| 49 | + try { |
| 50 | + setApiCallInProgress(true) |
| 51 | + |
| 52 | + let timeout = -1 |
| 53 | + |
| 54 | + const triggerUpdate = () => { |
| 55 | + if (timeout >= 0) { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + timeout = setTimeout(() => { |
| 60 | + setLastUpdateTimeStamp(new Date().toISOString()) |
| 61 | + |
| 62 | + timeout = -1 |
| 63 | + }, 10) |
| 64 | + } |
| 65 | + |
| 66 | + const calls = operationsRef.current.map((op) => async () => { |
| 67 | + const { operation, name, additionalKeys } = op |
| 68 | + |
| 69 | + if (abortControllerRef.current.signal.aborted) { |
| 70 | + throw new Error('bulk operations aborted') |
| 71 | + } |
| 72 | + |
| 73 | + let id = -1 |
| 74 | + |
| 75 | + try { |
| 76 | + id = resultsStoreRef.current.addResult({ |
| 77 | + name, |
| 78 | + additionalKeys, |
| 79 | + status: 'Progressing', |
| 80 | + message: '-', |
| 81 | + }) |
| 82 | + |
| 83 | + triggerUpdate() |
| 84 | + |
| 85 | + await operation(abortControllerRef, data) |
| 86 | + |
| 87 | + resultsStoreRef.current.updateResultStatus(id, { status: 'Completed' }) |
| 88 | + |
| 89 | + triggerUpdate() |
| 90 | + } catch (err) { |
| 91 | + if (getIsRequestAborted(err)) { |
| 92 | + resultsStoreRef.current.updateResultStatus(id, { |
| 93 | + status: 'Failed', |
| 94 | + message: 'Aborted by you', |
| 95 | + retryOperation: op, |
| 96 | + }) |
| 97 | + |
| 98 | + triggerUpdate() |
| 99 | + |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + resultsStoreRef.current.updateResultStatus(id, { |
| 104 | + status: 'Failed', |
| 105 | + message: |
| 106 | + (err && |
| 107 | + (err.message || |
| 108 | + (err instanceof ServerErrors && |
| 109 | + Array.isArray(err.errors) && |
| 110 | + err.errors[0].userMessage))) ?? |
| 111 | + '', |
| 112 | + retryOperation: op, |
| 113 | + }) |
| 114 | + |
| 115 | + triggerUpdate() |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | + await ApiQueuingWithBatch(calls, true) |
| 120 | + |
| 121 | + ToastManager.showToast({ |
| 122 | + variant: ToastVariantType.info, |
| 123 | + description: 'Bulk action completed', |
| 124 | + }) |
| 125 | + |
| 126 | + if (hideResultsDrawer) { |
| 127 | + handleModalClose() |
| 128 | + } |
| 129 | + } catch (err) { |
| 130 | + showError(err) |
| 131 | + } finally { |
| 132 | + setApiCallInProgress(false) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // NOTE: this should really only be run at mount |
| 137 | + // therefore empty dependency array |
| 138 | + useEffect(() => { |
| 139 | + if (shouldSkipConfirmation) { |
| 140 | + handleBulkOperations().then(noop).catch(noop) |
| 141 | + } |
| 142 | + |
| 143 | + return () => { |
| 144 | + abortControllerRef.current?.abort() |
| 145 | + } |
| 146 | + }, []) |
| 147 | + |
| 148 | + const handleDrawerClose = apiCallInProgress ? noop : handleModalClose |
| 149 | + |
| 150 | + const handleRetryFailedOperations = async () => { |
| 151 | + operationsRef.current = resultsStoreRef.current.getRetryOperations() |
| 152 | + resultsStoreRef.current = new OperationResultStore(operationsRef.current.length) |
| 153 | + |
| 154 | + abortControllerRef.current = new AbortController() |
| 155 | + |
| 156 | + await handleBulkOperations() |
| 157 | + } |
| 158 | + |
| 159 | + const handleAbortBulkOperation = () => { |
| 160 | + abortControllerRef.current.abort() |
| 161 | + } |
| 162 | + |
| 163 | + const renderConfirmationDialog = () => ( |
| 164 | + <ConfirmationModal |
| 165 | + {...{ |
| 166 | + ...confirmationModalConfig, |
| 167 | + handleClose: handleModalClose, |
| 168 | + showConfirmationModal: true, |
| 169 | + buttonConfig: { |
| 170 | + primaryButtonConfig: { |
| 171 | + ...confirmationModalConfig.buttonConfig.primaryButtonConfig, |
| 172 | + onClick: handleBulkOperations, |
| 173 | + disabled: apiCallInProgress, |
| 174 | + isLoading: apiCallInProgress, |
| 175 | + }, |
| 176 | + secondaryButtonConfig: { |
| 177 | + ...confirmationModalConfig.buttonConfig.secondaryButtonConfig, |
| 178 | + onClick: handleModalClose, |
| 179 | + disabled: apiCallInProgress, |
| 180 | + }, |
| 181 | + }, |
| 182 | + }} |
| 183 | + /> |
| 184 | + ) |
| 185 | + |
| 186 | + const renderResultsDrawer = () => ( |
| 187 | + <BulkOperationsResultModal |
| 188 | + apiCallInProgress={apiCallInProgress} |
| 189 | + handleAbortBulkOperation={handleAbortBulkOperation} |
| 190 | + handleModalClose={handleDrawerClose} |
| 191 | + isOperationAborted={abortControllerRef.current?.signal.aborted ?? false} |
| 192 | + resultsStore={resultsStoreRef.current} |
| 193 | + handleRetryFailedOperations={handleRetryFailedOperations} |
| 194 | + getResultsChartSummaryText={getResultsChartSummaryText} |
| 195 | + resultsHeader={textConfig.resultsHeader} |
| 196 | + /> |
| 197 | + ) |
| 198 | + |
| 199 | + return ( |
| 200 | + <div className="bulk-operations"> |
| 201 | + <Prompt when={apiCallInProgress} message={textConfig.prompt} /> |
| 202 | + {!shouldSkipConfirmation && !showResultsDrawer ? renderConfirmationDialog() : renderResultsDrawer()} |
| 203 | + </div> |
| 204 | + ) |
| 205 | +} |
| 206 | + |
| 207 | +export default BulkOperations |
0 commit comments