1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-02 01:17:58 +02:00

feat: add changes to draft (#2271)

* feat: add changes to draft

* Make domain type and schema match

* Deleting change from changeset

* Add ability to merge

* Revert "Add ability to merge"

This reverts commit 504e7e796e.

* gRevert "Deleting change from changeset"

This reverts commit 2effc20378.

* Revert "Make domain type and schema match"

This reverts commit 079f46c0db.

Co-authored-by: sjaanus <sellinjaanus@gmail.com>
This commit is contained in:
Tymoteusz Czech 2022-10-28 09:37:55 +02:00 committed by GitHub
parent b2c099a1c0
commit c6c873d67d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 45 deletions

View File

@ -38,8 +38,9 @@ const FeatureOverviewEnvSwitch = ({
const { const {
onSuggestToggle, onSuggestToggle,
onSuggestToggleClose, onSuggestToggleClose,
onSuggestToggleConfirm,
suggestChangesDialogDetails, suggestChangesDialogDetails,
} = useSuggestToggle(); } = useSuggestToggle(projectId);
const handleToggleEnvironmentOn = async () => { const handleToggleEnvironmentOn = async () => {
try { try {
@ -123,7 +124,7 @@ const FeatureOverviewEnvSwitch = ({
onClose={onSuggestToggleClose} onClose={onSuggestToggleClose}
featureName={featureId} featureName={featureId}
environment={suggestChangesDialogDetails?.environment} environment={suggestChangesDialogDetails?.environment}
onConfirm={() => {}} onConfirm={onSuggestToggleConfirm}
/> />
</div> </div>
); );

View File

@ -104,8 +104,9 @@ export const ProjectFeatureToggles = ({
const { const {
onSuggestToggle, onSuggestToggle,
onSuggestToggleClose, onSuggestToggleClose,
onSuggestToggleConfirm,
suggestChangesDialogDetails, suggestChangesDialogDetails,
} = useSuggestToggle(); } = useSuggestToggle(projectId);
const onToggle = useCallback( const onToggle = useCallback(
async ( async (
@ -521,7 +522,7 @@ export const ProjectFeatureToggles = ({
onClose={onSuggestToggleClose} onClose={onSuggestToggleClose}
featureName={suggestChangesDialogDetails?.featureName} featureName={suggestChangesDialogDetails?.featureName}
environment={suggestChangesDialogDetails?.environment} environment={suggestChangesDialogDetails?.environment}
onConfirm={() => {}} onConfirm={onSuggestToggleConfirm}
/> />
</PageContent> </PageContent>
); );

View File

@ -17,7 +17,7 @@ export const DraftBanner: VFC<IDraftBannerProps> = ({ environment }) => {
<Box <Box
sx={{ sx={{
position: 'sticky', position: 'sticky',
top: 0, top: -1,
zIndex: theme => theme.zIndex.appBar, zIndex: theme => theme.zIndex.appBar,
borderTop: theme => `1px solid ${theme.palette.warning.border}`, borderTop: theme => `1px solid ${theme.palette.warning.border}`,
borderBottom: theme => borderBottom: theme =>

View File

@ -1,8 +1,6 @@
import { FC } from 'react'; import { FC } from 'react';
import { Alert, Box, 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 useToast from 'hooks/useToast';
import { formatUnknownError } from 'utils/formatUnknownError';
interface ISuggestChangesDialogueProps { interface ISuggestChangesDialogueProps {
isOpen: boolean; isOpen: boolean;
@ -20,39 +18,26 @@ export const SuggestChangesDialogue: FC<ISuggestChangesDialogueProps> = ({
enabled, enabled,
featureName, featureName,
environment, environment,
}) => { }) => (
const { setToastData, setToastApiError } = useToast(); <Dialogue
open={isOpen}
const onSuggestClick = async () => { primaryButtonText="Add to draft"
try { secondaryButtonText="Cancel"
alert('Suggesting changes'); onClick={onConfirm}
onConfirm(); onClose={onClose}
} catch (error: unknown) { title="Suggest changes"
setToastApiError(formatUnknownError(error)); >
} <Alert severity="info" sx={{ mb: 2 }}>
}; Suggest changes is enabled for {environment}. Your changes needs to
be approved before they will be live. All the changes you do now
return ( will be added into a draft that you can submit for review.
<Dialogue </Alert>
open={isOpen} <Typography variant="body2" color="text.secondary">
primaryButtonText="Add to draft" Suggested changes:
secondaryButtonText="Cancel" </Typography>
onClick={onSuggestClick} <Typography>
onClose={onClose} <strong>{enabled ? 'Disable' : 'Enable'}</strong> feature toggle{' '}
title="Suggest changes" <strong>{featureName}</strong> in <strong>{environment}</strong>
> </Typography>
<Alert severity="info" sx={{ mb: 2 }}> </Dialogue>
Suggest changes is enabled for {environment}. Your changes needs );
to be approved before they will be live. All the changes you do
now will be added into a draft that you can submit for review.
</Alert>
<Typography variant="body2" color="text.secondary">
Suggested changes:
</Typography>
<Typography>
<strong>{enabled ? 'Disable' : 'Enable'}</strong> feature toggle{' '}
<strong>{featureName}</strong> in <strong>{environment}</strong>
</Typography>
</Dialogue>
);
};

View File

@ -0,0 +1,40 @@
import useAPI from '../useApi/useApi';
interface ISuggestChangeSchema {
feature: string;
action:
| 'updateEnabled'
| 'strategyAdd'
| 'strategyUpdate'
| 'strategyDelete';
payload: string | boolean | object | number;
}
export const useSuggestChangeApi = (project: string) => {
const { makeRequest, createRequest, errors, loading } = useAPI({
propagateErrors: true,
});
const addSuggestion = async (
environment: string,
payload: ISuggestChangeSchema
) => {
const path = `api/admin/projects/${project}/environments/${environment}/suggest-changes`;
const req = createRequest(path, {
method: 'POST',
body: JSON.stringify(payload),
});
try {
const response = await makeRequest(req.caller, req.id);
return await response.json();
} catch (e) {
throw e;
}
};
return {
addSuggestion,
errors,
loading,
};
};

View File

@ -1,6 +1,11 @@
import { useCallback, useState } from 'react'; import { useCallback, useState } from 'react';
import useToast from 'hooks/useToast';
import { formatUnknownError } from 'utils/formatUnknownError';
import { useSuggestChangeApi } from './api/actions/useSuggestChangeApi/useSuggestChangeApi';
export const useSuggestToggle = () => { export const useSuggestToggle = (project: string) => {
const { setToastData, setToastApiError } = useToast();
const { addSuggestion } = useSuggestChangeApi(project);
const [suggestChangesDialogDetails, setSuggestChangesDialogDetails] = const [suggestChangesDialogDetails, setSuggestChangesDialogDetails] =
useState<{ useState<{
enabled?: boolean; enabled?: boolean;
@ -25,9 +30,30 @@ export const useSuggestToggle = () => {
setSuggestChangesDialogDetails({ isOpen: false }); setSuggestChangesDialogDetails({ isOpen: false });
}, []); }, []);
const onSuggestToggleConfirm = useCallback(async () => {
try {
await addSuggestion(suggestChangesDialogDetails.environment!, {
feature: suggestChangesDialogDetails.featureName!,
action: 'updateEnabled',
payload: {
enabled: Boolean(suggestChangesDialogDetails.enabled),
},
});
setSuggestChangesDialogDetails({ isOpen: false });
setToastData({
type: 'success',
title: 'Changes added to the draft!',
});
} catch (error) {
setToastApiError(formatUnknownError(error));
setSuggestChangesDialogDetails({ isOpen: false });
}
}, [addSuggestion]);
return { return {
onSuggestToggle, onSuggestToggle,
onSuggestToggleClose, onSuggestToggleClose,
onSuggestToggleConfirm,
suggestChangesDialogDetails, suggestChangesDialogDetails,
}; };
}; };