-
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.
Added required field check in wizard flow
Signed-off-by: Gowtham Shanmugasundaram <[email protected]>
- Loading branch information
1 parent
77210bd
commit 8973ca2
Showing
9 changed files
with
270 additions
and
92 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
138 changes: 138 additions & 0 deletions
138
packages/mco/components/modals/app-manage-policies/helper/assign-policy-view-footer.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,138 @@ | ||
import * as React from 'react'; | ||
import { AssignPolicySteps, AssignPolicyStepsNames } from '@odf/mco/constants'; | ||
import { getName } from '@odf/shared/selectors'; | ||
import { useCustomTranslation } from '@odf/shared/useCustomTranslationHook'; | ||
import { TFunction } from 'i18next'; | ||
import { | ||
Button, | ||
WizardContextType, | ||
WizardContext, | ||
WizardFooter, | ||
Alert, | ||
AlertVariant, | ||
} from '@patternfly/react-core'; | ||
import { DRPolicyType, DataPolicyType } from '../utils/types'; | ||
import '../../../../style.scss'; | ||
import '../style.scss'; | ||
|
||
const isPVCSelectorFound = (dataPolicy: DRPolicyType) => | ||
!!dataPolicy?.placementControlInfo?.length && | ||
!!dataPolicy?.placementControlInfo?.every( | ||
(drpc) => !!drpc?.pvcSelector?.length | ||
); | ||
|
||
const isDRPolicySelected = (dataPolicy: DRPolicyType) => !!getName(dataPolicy); | ||
|
||
const canJumpToNextStep = ( | ||
stepName: string, | ||
dataPolicy: DataPolicyType, | ||
t: TFunction | ||
) => { | ||
switch (stepName) { | ||
case AssignPolicyStepsNames(t)[AssignPolicySteps.Policy]: | ||
return isDRPolicySelected(dataPolicy); | ||
case AssignPolicyStepsNames(t)[AssignPolicySteps.PersistentVolumeClaim]: | ||
return isPVCSelectorFound(dataPolicy); | ||
default: | ||
return false; | ||
} | ||
}; | ||
|
||
export const AssignPolicyViewFooter: React.FC<AssignPolicyViewFooterProps> = ({ | ||
dataPolicy, | ||
stepIdReached, | ||
isValidationEnabled, | ||
setStepIdReached, | ||
onSubmit, | ||
onCancel, | ||
setEnableValidation, | ||
}) => { | ||
const { t } = useCustomTranslation(); | ||
const [requestInProgress, setRequestInProgress] = React.useState(false); | ||
const { activeStep, onNext, onBack } = | ||
React.useContext<WizardContextType>(WizardContext); | ||
|
||
const stepId = activeStep.id as number; | ||
const stepName = activeStep.name as string; | ||
|
||
const canJumpToNext = canJumpToNextStep(stepName, dataPolicy, t); | ||
const validationError = isValidationEnabled && !canJumpToNext; | ||
|
||
const moveToNextStep = () => { | ||
setEnableValidation(true); | ||
if (canJumpToNext) { | ||
setStepIdReached(stepIdReached <= stepId ? stepId + 1 : stepIdReached); | ||
onNext(); | ||
setEnableValidation(false); | ||
} | ||
}; | ||
|
||
const handleNext = async () => { | ||
switch (stepName) { | ||
case AssignPolicyStepsNames(t)[AssignPolicySteps.ReviewAndAssign]: | ||
setRequestInProgress(true); | ||
await onSubmit(); | ||
setRequestInProgress(false); | ||
break; | ||
default: | ||
moveToNextStep(); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
{validationError && ( | ||
<Alert | ||
title={t( | ||
'Cannnot proceed because one or more mandatory fields have been left empty. Fill in the required information before proceeding.' | ||
)} | ||
variant={AlertVariant.danger} | ||
isInline | ||
className="odf-alert mco-manage-policies__alert--margin-left" | ||
/> | ||
)} | ||
<WizardFooter> | ||
<Button | ||
isLoading={requestInProgress} | ||
isDisabled={requestInProgress || validationError} | ||
variant="primary" | ||
type="submit" | ||
onClick={handleNext} | ||
> | ||
{stepName === | ||
AssignPolicyStepsNames(t)[AssignPolicySteps.ReviewAndAssign] | ||
? t('Assign') | ||
: t('Next')} | ||
</Button> | ||
{/* Disabling the back button for the first step (Policy) in wizard */} | ||
<Button | ||
variant="secondary" | ||
onClick={onBack} | ||
isDisabled={ | ||
stepName === AssignPolicyStepsNames(t)[AssignPolicySteps.Policy] || | ||
requestInProgress | ||
} | ||
> | ||
{t('Back')} | ||
</Button> | ||
<Button | ||
variant="link" | ||
onClick={onCancel} | ||
isDisabled={requestInProgress} | ||
> | ||
{t('Cancel')} | ||
</Button> | ||
</WizardFooter> | ||
</> | ||
); | ||
}; | ||
|
||
type AssignPolicyViewFooterProps = { | ||
dataPolicy: DataPolicyType; | ||
stepIdReached: number; | ||
isValidationEnabled: boolean; | ||
setStepIdReached: React.Dispatch<React.SetStateAction<number>>; | ||
onSubmit: () => void; | ||
onCancel: () => void; | ||
setEnableValidation: React.Dispatch<React.SetStateAction<boolean>>; | ||
}; |
Oops, something went wrong.