1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00

Feat/update dialogues (#2559)

* Updates dialogues and strategy edit / creation to give a warning if
you have a change request in review
This commit is contained in:
Fredrik Strand Oseberg 2022-11-29 14:52:01 +01:00 committed by GitHub
parent 5b24b60d31
commit 1a19d1e6fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 29 deletions

View File

@ -1,6 +1,10 @@
import { FC } from 'react'; import { FC } from 'react';
import { Alert, Typography } from '@mui/material'; import { Alert, Typography } from '@mui/material';
import { Dialogue } from 'component/common/Dialogue/Dialogue'; import { Dialogue } from 'component/common/Dialogue/Dialogue';
import { usePendingChangeRequests } from 'hooks/api/getters/usePendingChangeRequests/usePendingChangeRequests';
import { useRequiredPathParam } from 'hooks/useRequiredPathParam';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { useChangeRequestInReviewWarning } from 'hooks/useChangeRequestInReviewWarning';
interface IChangeRequestDialogueProps { interface IChangeRequestDialogueProps {
isOpen: boolean; isOpen: boolean;
@ -18,27 +22,49 @@ export const ChangeRequestDialogue: FC<IChangeRequestDialogueProps> = ({
showBanner, showBanner,
environment, environment,
messageComponent, messageComponent,
}) => ( }) => {
const projectId = useRequiredPathParam('projectId');
const { draft } = usePendingChangeRequests(projectId);
const { changeRequestInReviewOrApproved, alert } =
useChangeRequestInReviewWarning(draft);
const hasChangeRequestInReviewForEnvironment =
changeRequestInReviewOrApproved(environment || '');
const primaryButtonText = hasChangeRequestInReviewForEnvironment
? 'Add to existing change request'
: 'Add suggestion to draft';
return (
<Dialogue <Dialogue
open={isOpen} open={isOpen}
primaryButtonText="Add suggestion to draft" primaryButtonText={primaryButtonText}
secondaryButtonText="Cancel" secondaryButtonText="Cancel"
onClick={onConfirm} onClick={onConfirm}
onClose={onClose} onClose={onClose}
title="Request changes" title="Request changes"
fullWidth fullWidth
> >
{showBanner && ( <ConditionallyRender
condition={hasChangeRequestInReviewForEnvironment}
show={alert}
/>
<ConditionallyRender
condition={Boolean(showBanner)}
show={
<Alert severity="info" sx={{ mb: 2 }}> <Alert severity="info" sx={{ mb: 2 }}>
Change requests feature is enabled for {environment}. Your Change requests feature is enabled for {environment}.
changes needs to be approved before they will be live. All the Your changes needs to be approved before they will be
changes you do now will be added into a draft that you can live. All the changes you do now will be added into a
submit for review. draft that you can submit for review.
</Alert> </Alert>
)} }
/>
<Typography variant="body2" color="text.secondary"> <Typography variant="body2" color="text.secondary">
Your suggestion: Your suggestion:
</Typography> </Typography>
{messageComponent} {messageComponent}
</Dialogue> </Dialogue>
); );
};

View File

@ -28,6 +28,8 @@ import {
useFeatureStrategyProdGuard, useFeatureStrategyProdGuard,
} from '../FeatureStrategyProdGuard/FeatureStrategyProdGuard'; } from '../FeatureStrategyProdGuard/FeatureStrategyProdGuard';
import { formatFeaturePath } from '../FeatureStrategyEdit/FeatureStrategyEdit'; import { formatFeaturePath } from '../FeatureStrategyEdit/FeatureStrategyEdit';
import { useChangeRequestInReviewWarning } from 'hooks/useChangeRequestInReviewWarning';
import { usePendingChangeRequests } from 'hooks/api/getters/usePendingChangeRequests/usePendingChangeRequests';
interface IFeatureStrategyFormProps { interface IFeatureStrategyFormProps {
feature: IFeatureToggle; feature: IFeatureToggle;
@ -64,6 +66,18 @@ export const FeatureStrategyForm = ({
const enableProdGuard = useFeatureStrategyProdGuard(feature, environmentId); const enableProdGuard = useFeatureStrategyProdGuard(feature, environmentId);
const { hasAccess } = useContext(AccessContext); const { hasAccess } = useContext(AccessContext);
const { strategyDefinition } = useStrategy(strategy?.name); const { strategyDefinition } = useStrategy(strategy?.name);
const { draft } = usePendingChangeRequests(feature.project);
const { changeRequestInReviewOrApproved, alert } =
useChangeRequestInReviewWarning(draft);
const hasChangeRequestInReviewForEnvironment =
changeRequestInReviewOrApproved(environmentId || '');
const changeRequestButtonText = hasChangeRequestInReviewForEnvironment
? 'Add to existing change request'
: 'Add change to draft';
const navigate = useNavigate(); const navigate = useNavigate();
const { const {
@ -129,6 +143,10 @@ export const FeatureStrategyForm = ({
return ( return (
<form className={styles.form} onSubmit={onSubmitWithValidation}> <form className={styles.form} onSubmit={onSubmitWithValidation}>
<ConditionallyRender
condition={hasChangeRequestInReviewForEnvironment}
show={alert}
elseShow={
<ConditionallyRender <ConditionallyRender
condition={Boolean(isChangeRequest)} condition={Boolean(isChangeRequest)}
show={ show={
@ -137,6 +155,8 @@ export const FeatureStrategyForm = ({
/> />
} }
/> />
}
/>
<FeatureStrategyEnabled <FeatureStrategyEnabled
projectId={feature.project} projectId={feature.project}
featureId={feature.name} featureId={feature.name}
@ -207,7 +227,9 @@ export const FeatureStrategyForm = ({
} }
data-testid={STRATEGY_FORM_SUBMIT_ID} data-testid={STRATEGY_FORM_SUBMIT_ID}
> >
{isChangeRequest ? 'Add change to draft' : 'Save strategy'} {isChangeRequest
? changeRequestButtonText
: 'Save strategy'}
</PermissionButton> </PermissionButton>
<Button <Button
type="button" type="button"

View File

@ -0,0 +1,29 @@
import { Alert } from '@mui/material';
import { oneOf } from 'utils/oneOf';
import { IChangeRequest } from '../component/changeRequest/changeRequest.types';
export const useChangeRequestInReviewWarning = (
draft: IChangeRequest[] | undefined
) => {
const changeRequestInReviewOrApproved = (environment: string) => {
if (!draft) return false;
return draft.some(
changeRequest =>
changeRequest.environment === environment &&
oneOf(['In review', 'Approved'], changeRequest.state)
);
};
return {
changeRequestInReviewOrApproved,
alert: (
<Alert sx={{ margin: '1rem 0' }} severity="warning">
You currently have a change request in review for this
environment. Adding a new change will add the change to the
existing change request, and all existing approvals will be
reset. Are you sure you want to continue?
</Alert>
),
};
};