-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Gowtham Shanmugasundaram <[email protected]>
- Loading branch information
1 parent
4241c81
commit 0c69e3c
Showing
24 changed files
with
1,308 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
packages/mco/components/modals/app-manage-policies/assign-policy-view.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import * as React from 'react'; | ||
import { ModalBody } from '@odf/shared/modals/Modal'; | ||
import { getName } from '@odf/shared/selectors'; | ||
import { useCustomTranslation } from '@odf/shared/useCustomTranslationHook'; | ||
import { getErrorMessage } from '@odf/shared/utils'; | ||
import { TFunction } from 'i18next'; | ||
import { Wizard, WizardStep, AlertVariant } from '@patternfly/react-core'; | ||
import { PolicyConfigViewer } from './helper/policy-config-viewer'; | ||
import { PVCDetailsWizardContent } from './helper/pvc-details-wizard-content'; | ||
import { SelectPolicyWizardContent } from './helper/select-policy-wizard-content'; | ||
import { assignPromises } from './utils/k8s-utils'; | ||
import { | ||
AssignPolicyViewState, | ||
ManagePolicyStateAction, | ||
ManagePolicyStateType, | ||
MessageType, | ||
ModalActionContext, | ||
ModalViewContext, | ||
} from './utils/reducer'; | ||
import { | ||
ApplicationType, | ||
DRPolicyType, | ||
DataPolicyType, | ||
PlacementType, | ||
} from './utils/types'; | ||
|
||
export const createSteps = ( | ||
workloadNamespace: string, | ||
placements: PlacementType[], | ||
dataPolicies: DRPolicyType[], | ||
state: AssignPolicyViewState, | ||
stepIdReached: number, | ||
t: TFunction, | ||
setPolicy: (policy?: DataPolicyType) => void | ||
): WizardStep[] => [ | ||
{ | ||
id: 1, | ||
name: t('Select policy'), | ||
component: ( | ||
<SelectPolicyWizardContent | ||
dataPolicies={dataPolicies} | ||
state={state} | ||
setPolicy={setPolicy} | ||
/> | ||
), | ||
enableNext: !!getName(state.policy), | ||
}, | ||
{ | ||
id: 2, | ||
name: t('PVC details'), | ||
component: ( | ||
<PVCDetailsWizardContent | ||
placements={placements} | ||
state={state} | ||
workloadNamespace={workloadNamespace} | ||
setPolicy={setPolicy} | ||
/> | ||
), | ||
canJumpTo: stepIdReached >= 2, | ||
enableNext: !!state.policy?.placementControInfo?.length, | ||
}, | ||
{ | ||
id: 3, | ||
name: t('Review and assign'), | ||
component: ( | ||
<PolicyConfigViewer | ||
policyName={getName(state.policy)} | ||
clusters={state.policy.drClusters} | ||
isPolicyValidated={state.policy.isValidated} | ||
replicationType={state.policy.replicationType} | ||
syncInterval={state.policy.schedulingInterval} | ||
drPlacementControlInfo={state.policy.placementControInfo} | ||
defaultSelectionText="" | ||
hideSelector | ||
/> | ||
), | ||
nextButtonText: t('Assign'), | ||
canJumpTo: stepIdReached >= 3, | ||
}, | ||
]; | ||
|
||
export const AssignPolicyView: React.FC<AssignPolicyViewProps> = ({ | ||
state, | ||
applicaitonInfo, | ||
dataPolicies, | ||
setModalContext, | ||
setModalActionContext, | ||
setMessage, | ||
dispatch, | ||
}) => { | ||
const { t } = useCustomTranslation(); | ||
const [stepIdReached, setStepIdReached] = React.useState(1); | ||
|
||
const onNext = ({ id }: WizardStep) => { | ||
if (id) { | ||
if (typeof id === 'string') { | ||
id = parseInt(id, 10); | ||
} | ||
setStepIdReached(stepIdReached < id ? id : stepIdReached); | ||
} | ||
}; | ||
|
||
const setPolicy = (policy: DataPolicyType = {}) => | ||
dispatch({ | ||
type: ManagePolicyStateType.SET_SELECTED_POLICY, | ||
context: ModalViewContext.ASSIGN_POLICY_VIEW, | ||
payload: policy, | ||
}); | ||
|
||
const assignPolicy = () => { | ||
const updateContext = async ( | ||
title: string, | ||
description: string, | ||
variant: AlertVariant, | ||
actionContext: ModalActionContext | ||
) => { | ||
// inject a message into list view | ||
setMessage( | ||
{ | ||
title, | ||
description, | ||
variant, | ||
}, | ||
ModalViewContext.POLICY_LIST_VIEW | ||
); | ||
setModalActionContext(actionContext, ModalViewContext.POLICY_LIST_VIEW); | ||
// switch to list policy view | ||
setModalContext(ModalViewContext.POLICY_LIST_VIEW); | ||
// reset policy info | ||
setPolicy(); | ||
}; | ||
// assign DRPolicy | ||
const promises = assignPromises(state.policy); | ||
Promise.all(promises) | ||
.then(() => { | ||
updateContext( | ||
t('New policy assigned to application.'), | ||
'', | ||
AlertVariant.success, | ||
ModalActionContext.ASSIGN_POLICY_SUCCEEDED | ||
); | ||
}) | ||
.catch((error) => { | ||
updateContext( | ||
t('Unable to assign policy to application.'), | ||
getErrorMessage(error), | ||
AlertVariant.danger, | ||
ModalActionContext.ASSIGN_POLICY_FAILED | ||
); | ||
}); | ||
}; | ||
|
||
return ( | ||
<ModalBody> | ||
<Wizard | ||
cancelButtonText={t('Cancel')} | ||
nextButtonText={t('Next')} | ||
backButtonText={t('Back')} | ||
navAriaLabel={t('Assign policy nav')} | ||
mainAriaLabel={t('Assign policy content')} | ||
onSave={assignPolicy} | ||
onClose={() => { | ||
setModalContext(ModalViewContext.POLICY_LIST_VIEW); | ||
// reset policy info | ||
setPolicy(); | ||
}} | ||
steps={createSteps( | ||
applicaitonInfo?.workloadNamespace, | ||
applicaitonInfo?.placements, | ||
dataPolicies, | ||
state, | ||
stepIdReached, | ||
t, | ||
setPolicy | ||
)} | ||
onNext={onNext} | ||
height={450} | ||
/> | ||
</ModalBody> | ||
); | ||
}; | ||
|
||
type AssignPolicyViewProps = { | ||
state: AssignPolicyViewState; | ||
applicaitonInfo: ApplicationType; | ||
dataPolicies: DataPolicyType[]; | ||
dispatch: React.Dispatch<ManagePolicyStateAction>; | ||
setModalContext: (modalViewContext: ModalViewContext) => void; | ||
setModalActionContext: ( | ||
modalActionContext: ModalActionContext, | ||
modalViewContext?: ModalViewContext | ||
) => void; | ||
setMessage: (error: MessageType, modalViewContext?: ModalViewContext) => void; | ||
}; |
Oops, something went wrong.