|
1 | 1 | import * as React from 'react'; |
2 | | -import { Button, Modal, ModalVariant, Stack, StackItem, Text, TextContent } from '@patternfly/react-core'; |
3 | 2 | import { Trans } from 'react-i18next'; |
| 3 | +import { Alert, Button, Modal, ModalVariant, Stack, StackItem, Text, TextContent } from '@patternfly/react-core'; |
| 4 | +import { DeviceResumeRequest, DeviceResumeResponse } from '@flightctl/types'; |
4 | 5 |
|
5 | 6 | import { useTranslation } from '../../../../hooks/useTranslation'; |
| 7 | +import { useFetch } from '../../../../hooks/useFetch'; |
| 8 | +import { getErrorMessage } from '../../../../utils/error'; |
6 | 9 |
|
7 | 10 | interface ResumeAllDevicesConfirmationModalProps { |
8 | | - deviceCountNum: number; |
9 | | - onClose: (doConfirm: boolean) => void; |
| 11 | + devicesToResume: number; |
| 12 | + onClose: (resumedCount: number | undefined) => void; |
10 | 13 | isSubmitting?: boolean; |
11 | 14 | } |
12 | 15 |
|
13 | | -const ResumeAllDevicesConfirmationModal = ({ deviceCountNum, onClose }: ResumeAllDevicesConfirmationModalProps) => { |
| 16 | +const ModalContentBeforeResume = ({ devicesToResume }: { devicesToResume: number }) => { |
14 | 17 | const { t } = useTranslation(); |
15 | 18 |
|
16 | | - const deviceCount = deviceCountNum.toString(); |
| 19 | + const deviceCount = devicesToResume.toString(); |
| 20 | + return ( |
| 21 | + <Stack hasGutter> |
| 22 | + <StackItem> |
| 23 | + <TextContent> |
| 24 | + <Text> |
| 25 | + <Trans t={t} count={devicesToResume}> |
| 26 | + You are about to resume all <strong>{deviceCount}</strong> suspended devices. |
| 27 | + </Trans> |
| 28 | + </Text> |
| 29 | + </TextContent> |
| 30 | + <TextContent> |
| 31 | + <Text> |
| 32 | + {t( |
| 33 | + 'This action is irreversible and will allow all affected devices to receive new configuration updates from the server.', |
| 34 | + )} |
| 35 | + </Text> |
| 36 | + </TextContent> |
| 37 | + </StackItem> |
| 38 | + </Stack> |
| 39 | + ); |
| 40 | +}; |
| 41 | + |
| 42 | +const ModalContentAfterResume = ({ |
| 43 | + hasResumedAll, |
| 44 | + resumedCount, |
| 45 | + submitError, |
| 46 | +}: { |
| 47 | + hasResumedAll: boolean; |
| 48 | + resumedCount: number; |
| 49 | + submitError: string | undefined; |
| 50 | +}) => { |
| 51 | + const { t } = useTranslation(); |
| 52 | + if (submitError) { |
| 53 | + return ( |
| 54 | + <Alert isInline variant="danger" title={t('Resume devices failed')}> |
| 55 | + {submitError} |
| 56 | + </Alert> |
| 57 | + ); |
| 58 | + } |
17 | 59 |
|
| 60 | + const alertType = hasResumedAll ? 'success' : 'warning'; |
| 61 | + const title = hasResumedAll ? t('Resume successful') : t('Resumed with warnings'); |
18 | 62 | return ( |
19 | | - <Modal |
20 | | - variant={ModalVariant.small} |
21 | | - title={t('Resume all {{ deviceCount }} devices?', { deviceCount })} |
22 | | - isOpen |
23 | | - onClose={() => onClose(false)} |
24 | | - actions={[ |
25 | | - <Button key="confirm" variant="primary" onClick={() => onClose(true)}> |
| 63 | + <Alert isInline variant={alertType} title={title}> |
| 64 | + {t('{{ count }} devices were resumed', { count: resumedCount })} |
| 65 | + </Alert> |
| 66 | + ); |
| 67 | +}; |
| 68 | + |
| 69 | +const ResumeAllDevicesConfirmationModal = ({ devicesToResume, onClose }: ResumeAllDevicesConfirmationModalProps) => { |
| 70 | + const { t } = useTranslation(); |
| 71 | + const { post } = useFetch(); |
| 72 | + |
| 73 | + const [resumedCount, setResumedCount] = React.useState<number | undefined>(undefined); |
| 74 | + const [isSubmitting, setIsSubmitting] = React.useState(false); |
| 75 | + const [submitError, setSubmitError] = React.useState<string | undefined>(undefined); |
| 76 | + |
| 77 | + const isBeforeResume = resumedCount === undefined; |
| 78 | + const title = isBeforeResume |
| 79 | + ? t('Resume all {{ count }} devices?', { count: devicesToResume }) |
| 80 | + : t('Resume devices result'); |
| 81 | + |
| 82 | + const buttons = isBeforeResume |
| 83 | + ? [ |
| 84 | + <Button |
| 85 | + key="confirm" |
| 86 | + variant="primary" |
| 87 | + isDisabled={isSubmitting} |
| 88 | + onClick={async () => { |
| 89 | + setIsSubmitting(true); |
| 90 | + try { |
| 91 | + const resumeRequest: DeviceResumeRequest = { |
| 92 | + labelSelector: '', // All devices |
| 93 | + }; |
| 94 | + const resumeResponse = await post<DeviceResumeRequest, DeviceResumeResponse>( |
| 95 | + 'deviceactions/resume', |
| 96 | + resumeRequest, |
| 97 | + ); |
| 98 | + setResumedCount(resumeResponse.resumedDevices || 0); |
| 99 | + } catch (error) { |
| 100 | + setResumedCount(0); |
| 101 | + setSubmitError(getErrorMessage(error)); |
| 102 | + } finally { |
| 103 | + setIsSubmitting(false); |
| 104 | + } |
| 105 | + }} |
| 106 | + > |
26 | 107 | {t('Resume all devices')} |
27 | 108 | </Button>, |
28 | | - <Button key="cancel" variant="link" onClick={() => onClose(false)}> |
| 109 | + <Button key="cancel" variant="link" isDisabled={isSubmitting} onClick={() => onClose(undefined)}> |
29 | 110 | {t('Cancel')} |
30 | 111 | </Button>, |
31 | | - ]} |
32 | | - > |
33 | | - <Stack hasGutter> |
34 | | - <StackItem> |
35 | | - <TextContent> |
36 | | - <Text> |
37 | | - <Trans t={t} count={deviceCountNum}> |
38 | | - You are about to resume all <strong>{deviceCount}</strong> suspended devices. |
39 | | - </Trans> |
40 | | - </Text> |
41 | | - </TextContent> |
42 | | - <TextContent> |
43 | | - <Text> |
44 | | - {t( |
45 | | - 'This action is irreversible and will allow all affected devices to receive new configuration updates from the server.', |
46 | | - )} |
47 | | - </Text> |
48 | | - </TextContent> |
49 | | - </StackItem> |
50 | | - </Stack> |
| 112 | + ] |
| 113 | + : [ |
| 114 | + <Button key="close" variant="primary" onClick={() => onClose(resumedCount)}> |
| 115 | + {t('Close')} |
| 116 | + </Button>, |
| 117 | + ]; |
| 118 | + |
| 119 | + return ( |
| 120 | + <Modal variant={ModalVariant.small} title={title} isOpen onClose={() => onClose(undefined)} actions={buttons}> |
| 121 | + {isBeforeResume ? ( |
| 122 | + <ModalContentBeforeResume devicesToResume={devicesToResume} /> |
| 123 | + ) : ( |
| 124 | + <ModalContentAfterResume |
| 125 | + resumedCount={resumedCount} |
| 126 | + hasResumedAll={resumedCount === devicesToResume} |
| 127 | + submitError={submitError} |
| 128 | + /> |
| 129 | + )} |
51 | 130 | </Modal> |
52 | 131 | ); |
53 | 132 | }; |
|
0 commit comments