-
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 9183620
Showing
7 changed files
with
221 additions
and
84 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
111 changes: 111 additions & 0 deletions
111
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,111 @@ | ||
import * as React from 'react'; | ||
import { TFunction } from 'i18next'; | ||
import { | ||
Button, | ||
WizardContextType, | ||
WizardContext, | ||
WizardFooter, | ||
} from '@patternfly/react-core'; | ||
import { useCustomTranslation } from '@odf/shared/useCustomTranslationHook'; | ||
import { AssignPolicySteps, AssignPolicyStepsNames } from '@odf/mco/constants'; | ||
import { DRPlacementControlType, DataPolicyType } from '../utils/types'; | ||
import { getName } from '@odf/shared/selectors'; | ||
|
||
const isPVCSelectorFound = (placementControlInfo?: DRPlacementControlType[]) => | ||
!!placementControlInfo?.every((drpc) => !!drpc?.pvcSelector?.length); | ||
|
||
const canJumpToNextStep = ( | ||
stepName: string, | ||
dataPolicy: DataPolicyType, | ||
t: TFunction | ||
) => { | ||
switch (stepName) { | ||
case AssignPolicyStepsNames(t)[AssignPolicySteps.Policy]: | ||
return !!getName(dataPolicy); | ||
case AssignPolicyStepsNames(t)[AssignPolicySteps.PersistentVolumeClaim]: | ||
return ( | ||
!!dataPolicy?.placementControlInfo?.length && | ||
isPVCSelectorFound(dataPolicy?.placementControlInfo) | ||
); | ||
default: | ||
return false; | ||
} | ||
}; | ||
|
||
export const AssignPolicyViewFooter: React.FC<AssignPolicyViewFooterProps> = ({ | ||
dataPolicy, | ||
stepIdReached, | ||
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 moveToNextStep = () => { | ||
setEnableValidation(true); | ||
if (canJumpToNextStep(stepName, dataPolicy, t)) { | ||
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 ( | ||
<WizardFooter> | ||
<Button | ||
isLoading={requestInProgress} | ||
isDisabled={requestInProgress} | ||
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; | ||
setStepIdReached: React.Dispatch<React.SetStateAction<number>>; | ||
onSubmit: () => void; | ||
onCancel: () => void; | ||
setEnableValidation: React.Dispatch<React.SetStateAction<boolean>>; | ||
}; |
Oops, something went wrong.