|
| 1 | +import React, { useEffect, useState, useRef } from 'react'; |
| 2 | +import { |
| 3 | + Tag, Loading, Link, |
| 4 | +} from 'carbon-components-react'; |
| 5 | +import PropTypes from 'prop-types'; |
| 6 | +import { workflowStatusData, workflowStateTypes } from './data'; |
| 7 | +import MiqDataTable from '../miq-data-table'; |
| 8 | +import NotificationMessage from '../notification-message'; |
| 9 | + |
| 10 | +/** Component to render the Workflow status in /miq_request/show/#{id} page */ |
| 11 | +const RequestWorkflowStatusItem = ({ recordId }) => { |
| 12 | + const RELOAD = 2000; // Time interval to reload the RequestWorkflowStatus component. |
| 13 | + const reloadLimit = 5; // This is to handle the Auto refresh issue causing the the server to burn out with multiple requests. |
| 14 | + const reloadCount = useRef(0); |
| 15 | + |
| 16 | + const [data, setData] = useState( |
| 17 | + { |
| 18 | + isLoading: true, |
| 19 | + responseData: undefined, |
| 20 | + message: undefined, |
| 21 | + list: [], |
| 22 | + parentName: undefined, |
| 23 | + validType: false, |
| 24 | + } |
| 25 | + ); |
| 26 | + |
| 27 | + /** Function to get the Workflow */ |
| 28 | + const getWorkflow = async() => { |
| 29 | + reloadCount.current += 1; |
| 30 | + const url = `/api/configuration_scripts/${recordId}`; |
| 31 | + API.get(url, { skipErrors: [404, 400, 500] }) |
| 32 | + .then((response) => { |
| 33 | + const responseData = workflowStatusData(response); |
| 34 | + if (responseData) { |
| 35 | + API.get(`/api/configuration_script_payloads/${responseData.parentId}`).then((response2) => { |
| 36 | + if (response.context) { |
| 37 | + setData({ |
| 38 | + ...data, |
| 39 | + responseData, |
| 40 | + isLoading: false, |
| 41 | + parentName: response2.name, |
| 42 | + validType: true, |
| 43 | + message: responseData && responseData.status === workflowStateTypes.error.text |
| 44 | + ? __('An error has occurred with this workflow') : undefined, |
| 45 | + }); |
| 46 | + } else { |
| 47 | + setData({ |
| 48 | + ...data, |
| 49 | + responseData, |
| 50 | + isLoading: false, |
| 51 | + parentName: response2.name, |
| 52 | + validType: true, |
| 53 | + message: sprintf(__('Context is not available for "%s"'), response.name), |
| 54 | + }); |
| 55 | + } |
| 56 | + }); |
| 57 | + } else { |
| 58 | + setData({ |
| 59 | + ...data, |
| 60 | + validType: false, |
| 61 | + responseData: undefined, |
| 62 | + isLoading: false, |
| 63 | + }); |
| 64 | + } |
| 65 | + }); |
| 66 | + }; |
| 67 | + |
| 68 | + /** Logic to reload the component every so often (RELOAD). */ |
| 69 | + useEffect(() => { |
| 70 | + const omitStatus = [workflowStateTypes.success.text, workflowStateTypes.error.text]; |
| 71 | + if (reloadCount.current <= reloadLimit && data.responseData && data.responseData.status && !omitStatus.includes(data.responseData.status)) { |
| 72 | + const interval = setInterval(() => { |
| 73 | + setData({ ...data, isLoading: true }); |
| 74 | + getWorkflow(); |
| 75 | + }, RELOAD); |
| 76 | + return () => clearInterval(interval); // This represents the unmount function, in which you need to clear your interval to prevent memory leaks. |
| 77 | + } |
| 78 | + return undefined; |
| 79 | + }, [data.responseData]); |
| 80 | + |
| 81 | + useEffect(() => { |
| 82 | + if (recordId) { |
| 83 | + getWorkflow(); |
| 84 | + } |
| 85 | + }, [recordId]); |
| 86 | + |
| 87 | + /** Function to render the status of workflow. */ |
| 88 | + const renderStatusTag = () => { |
| 89 | + const status = workflowStateTypes[data.responseData.status]; |
| 90 | + return ( |
| 91 | + <Tag type={status.tagType} title={status.text}> |
| 92 | + {status.text.toUpperCase()} |
| 93 | + </Tag> |
| 94 | + ); |
| 95 | + }; |
| 96 | + |
| 97 | + /** Function to render the status of workflow status. */ |
| 98 | + const renderWorkflowStatus = () => ( |
| 99 | + <div className="workflow-status-container"> |
| 100 | + <div className="workflow-status-tag"> |
| 101 | + {data.responseData && data.responseData.status && renderStatusTag()} |
| 102 | + </div> |
| 103 | + <div className="workflow-status-label"> |
| 104 | + <Link href={`/workflow/show/${data.responseData.parentId}/`}>{data.parentName.toString()}</Link> |
| 105 | + </div> |
| 106 | + <div className="workflow-status-action"> |
| 107 | + {data.isLoading && <Loading active small withOverlay={false} className="loading" />} |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + ); |
| 111 | + |
| 112 | + /** Function to render the notification. */ |
| 113 | + const renderNotitication = () => ( |
| 114 | + <div className="workflow-notification-container"> |
| 115 | + <NotificationMessage type="error" message={data.message} /> |
| 116 | + </div> |
| 117 | + ); |
| 118 | + |
| 119 | + /** Function to render the list. */ |
| 120 | + const renderList = ({ headers, rows }) => ( |
| 121 | + <MiqDataTable |
| 122 | + headers={headers} |
| 123 | + rows={rows} |
| 124 | + mode="request-workflow-status" |
| 125 | + /> |
| 126 | + ); |
| 127 | + |
| 128 | + return ( |
| 129 | + <> |
| 130 | + { |
| 131 | + data.validType && ( |
| 132 | + <div className="workflow-states-container"> |
| 133 | + {data.responseData && renderWorkflowStatus()} |
| 134 | + {data.message && renderNotitication()} |
| 135 | + {data.responseData && data.responseData.status && renderList(data.responseData)} |
| 136 | + </div> |
| 137 | + ) |
| 138 | + } |
| 139 | + </> |
| 140 | + ); |
| 141 | +}; |
| 142 | + |
| 143 | +RequestWorkflowStatusItem.propTypes = { |
| 144 | + recordId: PropTypes.number.isRequired, |
| 145 | +}; |
| 146 | + |
| 147 | +export default RequestWorkflowStatusItem; |
0 commit comments