Skip to content

Commit

Permalink
feat: add changes to draft (#2271)
Browse files Browse the repository at this point in the history
* 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 504e7e7.

* gRevert "Deleting change from changeset"

This reverts commit 2effc20.

* Revert "Make domain type and schema match"

This reverts commit 079f46c.

Co-authored-by: sjaanus <sellinjaanus@gmail.com>
  • Loading branch information
Tymek and sjaanus committed Oct 28, 2022
1 parent b2c099a commit c6c873d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 45 deletions.
Expand Up @@ -38,8 +38,9 @@ const FeatureOverviewEnvSwitch = ({
const {
onSuggestToggle,
onSuggestToggleClose,
onSuggestToggleConfirm,
suggestChangesDialogDetails,
} = useSuggestToggle();
} = useSuggestToggle(projectId);

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

const onToggle = useCallback(
async (
Expand Down Expand Up @@ -521,7 +522,7 @@ export const ProjectFeatureToggles = ({
onClose={onSuggestToggleClose}
featureName={suggestChangesDialogDetails?.featureName}
environment={suggestChangesDialogDetails?.environment}
onConfirm={() => {}}
onConfirm={onSuggestToggleConfirm}
/>
</PageContent>
);
Expand Down
Expand Up @@ -17,7 +17,7 @@ export const DraftBanner: VFC<IDraftBannerProps> = ({ environment }) => {
<Box
sx={{
position: 'sticky',
top: 0,
top: -1,
zIndex: theme => theme.zIndex.appBar,
borderTop: theme => `1px solid ${theme.palette.warning.border}`,
borderBottom: theme =>
Expand Down
@@ -1,8 +1,6 @@
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 useToast from 'hooks/useToast';
import { formatUnknownError } from 'utils/formatUnknownError';

interface ISuggestChangesDialogueProps {
isOpen: boolean;
Expand All @@ -20,39 +18,26 @@ export const SuggestChangesDialogue: FC<ISuggestChangesDialogueProps> = ({
enabled,
featureName,
environment,
}) => {
const { setToastData, setToastApiError } = useToast();

const onSuggestClick = async () => {
try {
alert('Suggesting changes');
onConfirm();
} catch (error: unknown) {
setToastApiError(formatUnknownError(error));
}
};

return (
<Dialogue
open={isOpen}
primaryButtonText="Add to draft"
secondaryButtonText="Cancel"
onClick={onSuggestClick}
onClose={onClose}
title="Suggest changes"
>
<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 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>
);
};
}) => (
<Dialogue
open={isOpen}
primaryButtonText="Add to draft"
secondaryButtonText="Cancel"
onClick={onConfirm}
onClose={onClose}
title="Suggest changes"
>
<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
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>
);
@@ -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,
};
};
28 changes: 27 additions & 1 deletion frontend/src/hooks/useSuggestToggle.ts
@@ -1,6 +1,11 @@
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] =
useState<{
enabled?: boolean;
Expand All @@ -25,9 +30,30 @@ export const useSuggestToggle = () => {
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 {
onSuggestToggle,
onSuggestToggleClose,
onSuggestToggleConfirm,
suggestChangesDialogDetails,
};
};

0 comments on commit c6c873d

Please sign in to comment.