|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + Modal, |
| 5 | + ModalVariant, |
| 6 | + ButtonVariant, |
| 7 | + Text, |
| 8 | + TextContent, |
| 9 | +} from '@patternfly/react-core'; |
| 10 | +import { useModalDialogsContext, ClustersService } from '@openshift-assisted/ui-lib/ocm'; |
| 11 | +import { getApiErrorMessage, handleApiError, ErrorState } from '@openshift-assisted/ui-lib/common'; |
| 12 | +import { useNavigate } from 'react-router-dom-v5-compat'; |
| 13 | + |
| 14 | +const ResetSingleClusterModal: React.FC = () => { |
| 15 | + const navigate = useNavigate(); |
| 16 | + const [error, setError] = React.useState<{ title: string; message: string } | null>(null); |
| 17 | + const { resetSingleClusterDialog } = useModalDialogsContext(); |
| 18 | + const { data, isOpen, close: onClose } = resetSingleClusterDialog; |
| 19 | + const cluster = data?.cluster; |
| 20 | + |
| 21 | + console.log('resetSingleClusterDialog', useModalDialogsContext()); |
| 22 | + |
| 23 | + const handleClose = () => { |
| 24 | + setError(null); |
| 25 | + onClose(); |
| 26 | + }; |
| 27 | + |
| 28 | + const handleResetAsync = async () => { |
| 29 | + try { |
| 30 | + await ClustersService.remove(cluster.id); |
| 31 | + navigate(`/`); |
| 32 | + } catch (e) { |
| 33 | + handleApiError(e, () => { |
| 34 | + setError({ |
| 35 | + title: 'Failed to reset cluster installation', |
| 36 | + message: getApiErrorMessage(e), |
| 37 | + }); |
| 38 | + }); |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + const getModalContent = () => { |
| 43 | + if (error) { |
| 44 | + return <ErrorState title={error.title} content={error.message} />; |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <TextContent> |
| 49 | + <Text component="p"> |
| 50 | + This will reset the installation and return to the cluster configuration. Some hosts may |
| 51 | + need to be re-registered by rebooting into the Discovery ISO. |
| 52 | + </Text> |
| 53 | + |
| 54 | + <Text component="p">Are you sure you want to reset the cluster?</Text> |
| 55 | + </TextContent> |
| 56 | + ); |
| 57 | + }; |
| 58 | + |
| 59 | + const actions = [ |
| 60 | + <Button key="reset" variant={ButtonVariant.danger} onClick={() => void handleResetAsync()}> |
| 61 | + Reset Cluster |
| 62 | + </Button>, |
| 63 | + ]; |
| 64 | + |
| 65 | + actions.push( |
| 66 | + <Button key="cancel" variant={ButtonVariant.link} onClick={handleClose}> |
| 67 | + Cancel |
| 68 | + </Button>, |
| 69 | + ); |
| 70 | + |
| 71 | + return ( |
| 72 | + <Modal |
| 73 | + title="Reset Single Cluster Installation" |
| 74 | + isOpen={isOpen} |
| 75 | + variant={ModalVariant.small} |
| 76 | + actions={actions} |
| 77 | + onClose={handleClose} |
| 78 | + > |
| 79 | + {getModalContent()} |
| 80 | + </Modal> |
| 81 | + ); |
| 82 | +}; |
| 83 | + |
| 84 | +export default ResetSingleClusterModal; |
0 commit comments