1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-12 13:48:35 +02:00

disable change request overview actions when CR are disabled (#2527)

This commit is contained in:
Mateusz Kwasniewski 2022-11-24 16:16:14 +01:00 committed by GitHub
parent 5d52216d53
commit a3957f8c99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 21 deletions

View File

@ -19,6 +19,7 @@ import {
} from 'utils/strategyNames'; } from 'utils/strategyNames';
import { hasNameField } from '../changeRequest.types'; import { hasNameField } from '../changeRequest.types';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender'; import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { useChangeRequestsEnabled } from 'hooks/useChangeRequestsEnabled';
interface IChangeRequestProps { interface IChangeRequestProps {
changeRequest: IChangeRequest; changeRequest: IChangeRequest;
@ -91,8 +92,15 @@ export const ChangeRequest: VFC<IChangeRequestProps> = ({
setToastApiError(formatUnknownError(error)); setToastApiError(formatUnknownError(error));
} }
}; };
const { isChangeRequestConfigured } = useChangeRequestsEnabled(
changeRequest.project
);
const allowChangeRequestActions = isChangeRequestConfigured(
changeRequest.environment
);
const showDiscard = const showDiscard =
allowChangeRequestActions &&
!['Cancelled', 'Applied'].includes(changeRequest.state) && !['Cancelled', 'Applied'].includes(changeRequest.state) &&
changeRequest.features.flatMap(feature => feature.changes).length > 1; changeRequest.features.flatMap(feature => feature.changes).length > 1;

View File

@ -11,9 +11,8 @@ const AddCommentWrapper = styled(Box)(({ theme }) => ({
export const AddCommentField: FC<{ export const AddCommentField: FC<{
imageUrl: string; imageUrl: string;
commentText: string; commentText: string;
onAddComment: () => void;
onTypeComment: (text: string) => void; onTypeComment: (text: string) => void;
}> = ({ imageUrl, commentText, onTypeComment, onAddComment }) => ( }> = ({ imageUrl, commentText, onTypeComment, children }) => (
<> <>
<AddCommentWrapper> <AddCommentWrapper>
<StyledAvatar src={imageUrl} /> <StyledAvatar src={imageUrl} />
@ -28,16 +27,7 @@ export const AddCommentField: FC<{
/> />
</AddCommentWrapper> </AddCommentWrapper>
<Box sx={{ display: 'flex', justifyContent: 'flex-end' }}> <Box sx={{ display: 'flex', justifyContent: 'flex-end' }}>
<Button {children}
variant="outlined"
onClick={onAddComment}
disabled={
commentText.trim().length === 0 ||
commentText.trim().length > 1000
}
>
Comment
</Button>
</Box> </Box>
</> </>
); );

View File

@ -22,6 +22,7 @@ import AccessContext from 'contexts/AccessContext';
import { ChangeRequestComment } from './ChangeRequestComments/ChangeRequestComment'; import { ChangeRequestComment } from './ChangeRequestComments/ChangeRequestComment';
import { AddCommentField } from './ChangeRequestComments/AddCommentField'; import { AddCommentField } from './ChangeRequestComments/AddCommentField';
import { usePendingChangeRequests } from 'hooks/api/getters/usePendingChangeRequests/usePendingChangeRequests'; import { usePendingChangeRequests } from 'hooks/api/getters/usePendingChangeRequests/usePendingChangeRequests';
import { useChangeRequestsEnabled } from '../../../hooks/useChangeRequestsEnabled';
const StyledAsideBox = styled(Box)(({ theme }) => ({ const StyledAsideBox = styled(Box)(({ theme }) => ({
width: '30%', width: '30%',
@ -62,11 +63,16 @@ export const ChangeRequestOverview: FC = () => {
const { refetch: refetchChangeRequestOpen } = const { refetch: refetchChangeRequestOpen } =
usePendingChangeRequests(projectId); usePendingChangeRequests(projectId);
const { setToastData, setToastApiError } = useToast(); const { setToastData, setToastApiError } = useToast();
const { isChangeRequestConfigured } = useChangeRequestsEnabled(projectId);
if (!changeRequest) { if (!changeRequest) {
return null; return null;
} }
const allowChangeRequestActions = isChangeRequestConfigured(
changeRequest.environment
);
const onApplyChanges = async () => { const onApplyChanges = async () => {
try { try {
await changeState(projectId, Number(id), { await changeState(projectId, Number(id), {
@ -163,9 +169,20 @@ export const ChangeRequestOverview: FC = () => {
<AddCommentField <AddCommentField
imageUrl={user?.imageUrl || ''} imageUrl={user?.imageUrl || ''}
commentText={commentText} commentText={commentText}
onAddComment={onAddComment}
onTypeComment={setCommentText} onTypeComment={setCommentText}
/> >
<Button
variant="outlined"
onClick={onAddComment}
disabled={
!allowChangeRequestActions ||
commentText.trim().length === 0 ||
commentText.trim().length > 1000
}
>
Comment
</Button>
</AddCommentField>
<ConditionallyRender <ConditionallyRender
condition={isSelfReview} condition={isSelfReview}
show={ show={
@ -189,7 +206,11 @@ export const ChangeRequestOverview: FC = () => {
changeRequest.state === 'In review' && changeRequest.state === 'In review' &&
!hasApprovedAlready !hasApprovedAlready
} }
show={<ReviewButton />} show={
<ReviewButton
disabled={!allowChangeRequestActions}
/>
}
/> />
<ConditionallyRender <ConditionallyRender
condition={changeRequest.state === 'Approved'} condition={changeRequest.state === 'Approved'}
@ -202,6 +223,7 @@ export const ChangeRequestOverview: FC = () => {
environmentId={ environmentId={
changeRequest.environment changeRequest.environment
} }
disabled={!allowChangeRequestActions}
> >
Apply changes Apply changes
</PermissionButton> </PermissionButton>

View File

@ -1,4 +1,4 @@
import React, { useContext } from 'react'; import React, { FC, useContext } from 'react';
import { useRequiredPathParam } from 'hooks/useRequiredPathParam'; import { useRequiredPathParam } from 'hooks/useRequiredPathParam';
import { useChangeRequest } from 'hooks/api/getters/useChangeRequest/useChangeRequest'; import { useChangeRequest } from 'hooks/api/getters/useChangeRequest/useChangeRequest';
import { useChangeRequestApi } from 'hooks/api/actions/useChangeRequestApi/useChangeRequestApi'; import { useChangeRequestApi } from 'hooks/api/actions/useChangeRequestApi/useChangeRequestApi';
@ -22,7 +22,7 @@ import { useAuthUser } from 'hooks/api/getters/useAuth/useAuthUser';
import AccessContext from 'contexts/AccessContext'; import AccessContext from 'contexts/AccessContext';
import { usePendingChangeRequests } from 'hooks/api/getters/usePendingChangeRequests/usePendingChangeRequests'; import { usePendingChangeRequests } from 'hooks/api/getters/usePendingChangeRequests/usePendingChangeRequests';
export const ReviewButton = () => { export const ReviewButton: FC<{ disabled: boolean }> = ({ disabled }) => {
const { isAdmin } = useContext(AccessContext); const { isAdmin } = useContext(AccessContext);
const projectId = useRequiredPathParam('projectId'); const projectId = useRequiredPathParam('projectId');
const id = useRequiredPathParam('id'); const id = useRequiredPathParam('id');
@ -73,7 +73,9 @@ export const ReviewButton = () => {
<React.Fragment> <React.Fragment>
<PermissionButton <PermissionButton
variant="contained" variant="contained"
disabled={data?.createdBy.id === user?.id && !isAdmin} disabled={
disabled || (data?.createdBy.id === user?.id && !isAdmin)
}
aria-controls={open ? 'review-options-menu' : undefined} aria-controls={open ? 'review-options-menu' : undefined}
aria-expanded={open ? 'true' : undefined} aria-expanded={open ? 'true' : undefined}
aria-label="review changes" aria-label="review changes"

View File

@ -261,7 +261,7 @@ export const ProjectFeatureToggles = ({
disableSortBy: true, disableSortBy: true,
}, },
], ],
[projectId, environments, loading] [projectId, environments, loading, onToggle]
); );
const [searchValue, setSearchValue] = useState( const [searchValue, setSearchValue] = useState(

View File

@ -17,7 +17,7 @@ export const useChangeRequestsEnabled = (projectId: string) => {
return Boolean(uiConfig?.flags.changeRequests) && enabled; return Boolean(uiConfig?.flags.changeRequests) && enabled;
}, },
[data] [JSON.stringify(data)]
); );
const isChangeRequestConfiguredInAnyEnv = React.useCallback((): boolean => { const isChangeRequestConfiguredInAnyEnv = React.useCallback((): boolean => {
@ -25,7 +25,7 @@ export const useChangeRequestsEnabled = (projectId: string) => {
Boolean(uiConfig?.flags.changeRequests) && Boolean(uiConfig?.flags.changeRequests) &&
data.some(draft => draft.changeRequestEnabled) data.some(draft => draft.changeRequestEnabled)
); );
}, [data]); }, [JSON.stringify(data)]);
return { return {
isChangeRequestFlagEnabled: Boolean(uiConfig?.flags.changeRequests), isChangeRequestFlagEnabled: Boolean(uiConfig?.flags.changeRequests),