|
1 | 1 | import * as React from 'react';
|
2 |
| -import { DRApplication } from '@odf/mco/constants'; |
| 2 | +import { SearchResultItemType } from '@odf/mco/types'; |
| 3 | +import { convertSearchResult } from '@odf/mco/utils'; |
3 | 4 | import { getName, getNamespace } from '@odf/shared/selectors';
|
4 | 5 | import { useCustomTranslation } from '@odf/shared/useCustomTranslationHook';
|
| 6 | +import { K8sResourceCommon } from '@openshift-console/dynamic-plugin-sdk'; |
5 | 7 | import { Trans } from 'react-i18next';
|
6 | 8 | import { Modal, ModalVariant } from '@patternfly/react-core';
|
7 |
| -import { AssignPolicyView } from './assign-policy-view'; |
8 |
| -import { ManagePolicyView } from './manage-policy-view'; |
9 |
| -import { |
10 |
| - ManagePolicyStateAction, |
11 |
| - ManagePolicyStateType, |
12 |
| - ModalActionContext, |
13 |
| - ModalViewContext, |
14 |
| - initialPolicyState, |
15 |
| - managePolicyStateReducer, |
16 |
| - ManagePolicyState, |
17 |
| -} from './utils/reducer'; |
18 |
| -import { ApplicationType, DRInfoType, DRPolicyType } from './utils/types'; |
| 9 | +import { AppManagePoliciesModalBody } from './app-manage-policies-modal-body'; |
| 10 | +import { ModalViewContext } from './utils/reducer'; |
19 | 11 |
|
20 |
| -export const ModalContextViewer: React.FC<ModalContextViewerProps> = ({ |
21 |
| - applicationInfo, |
22 |
| - state, |
23 |
| - matchingPolicies, |
24 |
| - dispatch, |
25 |
| - loaded, |
26 |
| - loadError, |
| 12 | +export const AppManagePoliciesModal: React.FC<AppManagePoliciesModalProps> = ({ |
| 13 | + resource, |
| 14 | + cluster, |
| 15 | + isOpen, |
| 16 | + close, |
27 | 17 | }) => {
|
28 |
| - const setModalContext = React.useCallback( |
29 |
| - (modalViewContext: ModalViewContext) => |
30 |
| - dispatch({ |
31 |
| - type: ManagePolicyStateType.SET_MODAL_VIEW_CONTEXT, |
32 |
| - payload: modalViewContext, |
33 |
| - }), |
34 |
| - [dispatch] |
| 18 | + const { t } = useCustomTranslation(); |
| 19 | + const [currentModalContext, setCurrentModalContext] = React.useState( |
| 20 | + ModalViewContext.MANAGE_POLICY_VIEW |
35 | 21 | );
|
36 | 22 |
|
37 |
| - const setModalActionContext = React.useCallback( |
38 |
| - (modalActionContext: ModalActionContext) => |
39 |
| - dispatch({ |
40 |
| - type: ManagePolicyStateType.SET_MODAL_ACTION_CONTEXT, |
41 |
| - payload: modalActionContext, |
42 |
| - }), |
43 |
| - [dispatch] |
44 |
| - ); |
| 23 | + const application = |
| 24 | + 'apigroup' in resource ? convertSearchResult(resource) : resource; |
| 25 | + const applicationName = getName(application) ?? application?.['name']; |
| 26 | + const applicationNamespace = |
| 27 | + getNamespace(application) ?? application?.['namespace']; |
45 | 28 |
|
46 |
| - return ( |
47 |
| - <> |
48 |
| - {state.modalViewContext === ModalViewContext.MANAGE_POLICY_VIEW && ( |
49 |
| - <ManagePolicyView |
50 |
| - drInfo={applicationInfo?.drInfo as DRInfoType} |
51 |
| - workloadNamespace={applicationInfo?.workloadNamespace} |
52 |
| - eligiblePolicies={matchingPolicies} |
53 |
| - isSubscriptionAppType={ |
54 |
| - applicationInfo?.type === DRApplication.SUBSCRIPTION |
55 |
| - } |
56 |
| - unProtectedPlacementCount={applicationInfo?.placements?.length} |
57 |
| - dispatch={dispatch} |
58 |
| - setModalContext={setModalContext} |
59 |
| - setModalActionContext={setModalActionContext} |
60 |
| - loaded={loaded} |
61 |
| - loadError={loadError} |
62 |
| - modalActionContext={state.modalActionContext} |
63 |
| - /> |
64 |
| - )} |
65 |
| - {state.modalViewContext === ModalViewContext.ASSIGN_POLICY_VIEW && ( |
66 |
| - <AssignPolicyView |
67 |
| - applicationInfo={applicationInfo} |
68 |
| - matchingPolicies={matchingPolicies} |
69 |
| - state={state.assignPolicyView} |
70 |
| - modalActionContext={state.modalActionContext} |
71 |
| - dispatch={dispatch} |
72 |
| - setModalContext={setModalContext} |
73 |
| - setModalActionContext={setModalActionContext} |
74 |
| - /> |
75 |
| - )} |
76 |
| - </> |
77 |
| - ); |
78 |
| -}; |
| 29 | + const title = |
| 30 | + currentModalContext === ModalViewContext.ASSIGN_POLICY_VIEW |
| 31 | + ? t('Enroll managed application') |
| 32 | + : t('Manage disaster recovery'); |
79 | 33 |
|
80 |
| -export const AppManagePoliciesModal: React.FC<AppManagePoliciesModalProps> = ({ |
81 |
| - applicationInfo, |
82 |
| - matchingPolicies, |
83 |
| - loaded, |
84 |
| - loadError, |
85 |
| - isOpen, |
86 |
| - close, |
87 |
| -}) => { |
88 |
| - const [state, dispatch] = React.useReducer( |
89 |
| - managePolicyStateReducer, |
90 |
| - initialPolicyState |
| 34 | + const description = ( |
| 35 | + <Trans t={t}> |
| 36 | + <strong>Application:</strong> {applicationName} (Namespace:{' '} |
| 37 | + {applicationNamespace}) |
| 38 | + </Trans> |
91 | 39 | );
|
92 |
| - const { t } = useCustomTranslation(); |
93 | 40 |
|
94 | 41 | return (
|
95 | 42 | <Modal
|
96 |
| - title={ |
97 |
| - state.modalViewContext === ModalViewContext.ASSIGN_POLICY_VIEW |
98 |
| - ? t('Enroll managed application') |
99 |
| - : t('Manage disaster recovery') |
100 |
| - } |
101 |
| - description={ |
102 |
| - loaded && |
103 |
| - !loadError && ( |
104 |
| - <Trans t={t}> |
105 |
| - <strong>Application:</strong> {getName(applicationInfo)} (Namespace:{' '} |
106 |
| - {getNamespace(applicationInfo)}) |
107 |
| - </Trans> |
108 |
| - ) |
109 |
| - } |
| 43 | + title={title} |
| 44 | + description={description} |
110 | 45 | variant={ModalVariant.large}
|
111 | 46 | isOpen={isOpen}
|
112 | 47 | aria-label="Manage policy modal"
|
113 | 48 | aria-describedby="manage-policy-modal"
|
114 | 49 | onClose={close}
|
115 | 50 | >
|
116 |
| - <ModalContextViewer |
117 |
| - applicationInfo={applicationInfo} |
118 |
| - matchingPolicies={matchingPolicies} |
119 |
| - state={state} |
120 |
| - dispatch={dispatch} |
121 |
| - loaded={loaded} |
122 |
| - loadError={loadError} |
| 51 | + <AppManagePoliciesModalBody |
| 52 | + application={application} |
| 53 | + cluster={cluster} |
| 54 | + setCurrentModalContext={setCurrentModalContext} |
123 | 55 | />
|
124 | 56 | </Modal>
|
125 | 57 | );
|
126 | 58 | };
|
127 | 59 |
|
| 60 | +// ACM action custom plugin callback, |
| 61 | +// For more: https://github.com/stolostron/console/blob/main/frontend/src/plugin-extensions/properties/actionExtensionProps.ts#L27 |
128 | 62 | type AppManagePoliciesModalProps = {
|
129 |
| - applicationInfo: ApplicationType; |
130 |
| - matchingPolicies: DRPolicyType[]; |
131 |
| - loaded: boolean; |
132 |
| - loadError: any; |
| 63 | + resource: K8sResourceCommon | SearchResultItemType; |
133 | 64 | isOpen: boolean;
|
134 |
| - close?: () => void; |
| 65 | + close: () => void; |
| 66 | + // Specific field for the ACM VM list page |
| 67 | + cluster?: string; |
135 | 68 | };
|
136 | 69 |
|
137 |
| -type ModalContextViewerProps = { |
138 |
| - state: ManagePolicyState; |
139 |
| - applicationInfo: ApplicationType; |
140 |
| - matchingPolicies: DRPolicyType[]; |
141 |
| - dispatch: React.Dispatch<ManagePolicyStateAction>; |
142 |
| - loaded: boolean; |
143 |
| - loadError: any; |
144 |
| -}; |
| 70 | +export default AppManagePoliciesModal; |
0 commit comments