1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00

#4209: add 'add to draft' button for segments. (#4400)

Note: it doesn't work yet! It just throws an error.

This PR adds some logic to conditionally display "Add to draft" button
for segments if the segment is part of a project that has change
requests enabled and the flag is enabled.

Also adds a flag (`segmentChangeRequests`) to the frontend.

Holding off on actually adding the change to a draft until the API/orval
has been updated with the most recent changes.
This commit is contained in:
Thomas Heartman 2023-08-03 14:34:38 +02:00 committed by GitHub
parent 240cc2b5fa
commit 7cab19d9d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 12 deletions

View File

@ -2,10 +2,13 @@ import PermissionButton, {
IPermissionButtonProps,
} from 'component/common/PermissionButton/PermissionButton';
export const UpdateButton = ({ ...rest }: IPermissionButtonProps) => {
export const UpdateButton = ({
children = 'Save',
...rest
}: IPermissionButtonProps) => {
return (
<PermissionButton type="submit" {...rest}>
Save
{children}
</PermissionButton>
);
};

View File

@ -19,6 +19,7 @@ import { useSegmentValuesCount } from 'component/segments/hooks/useSegmentValues
import { SEGMENT_SAVE_BTN_ID } from 'utils/testIds';
import { useSegmentLimits } from 'hooks/api/getters/useSegmentLimits/useSegmentLimits';
import { useOptionalPathParam } from 'hooks/useOptionalPathParam';
import { useChangeRequestsEnabled } from 'hooks/useChangeRequestsEnabled';
interface IEditSegmentProps {
modal?: boolean;
@ -57,6 +58,13 @@ export const EditSegment = ({ modal }: IEditSegmentProps) => {
const segmentValuesCount = useSegmentValuesCount(constraints);
const { segmentValuesLimit } = useSegmentLimits();
const { isChangeRequestConfiguredInAnyEnv } = useChangeRequestsEnabled(
segment?.project || ''
);
const activateSegmentChangeRequests =
uiConfig?.flags?.segmentChangeRequests &&
isChangeRequestConfiguredInAnyEnv();
const overSegmentValuesLimit: boolean = Boolean(
segmentValuesLimit && segmentValuesCount > segmentValuesLimit
);
@ -75,17 +83,23 @@ export const EditSegment = ({ modal }: IEditSegmentProps) => {
e.preventDefault();
clearErrors();
try {
await updateSegment(segment.id, getSegmentPayload());
await refetchSegments();
if (projectId) {
navigate(`/projects/${projectId}/settings/segments/`);
if (activateSegmentChangeRequests) {
throw new Error(
"You can't add segments to change requests just yet."
);
} else {
navigate('/segments/');
await updateSegment(segment.id, getSegmentPayload());
refetchSegments();
if (projectId) {
navigate(`/projects/${projectId}/settings/segments/`);
} else {
navigate('/segments/');
}
setToastData({
title: 'Segment updated',
type: 'success',
});
}
setToastData({
title: 'Segment updated',
type: 'success',
});
} catch (error: unknown) {
setToastApiError(formatUnknownError(error));
}
@ -120,7 +134,9 @@ export const EditSegment = ({ modal }: IEditSegmentProps) => {
permission={UPDATE_SEGMENT}
disabled={!hasValidConstraints || overSegmentValuesLimit}
data-testid={SEGMENT_SAVE_BTN_ID}
/>
>
{activateSegmentChangeRequests ? 'Add to draft' : 'Save'}
</UpdateButton>
</SegmentForm>
</FormTemplate>
);

View File

@ -55,6 +55,7 @@ export interface IFlags {
newProjectLayout?: boolean;
configurableFeatureTypeLifetimes?: boolean;
frontendNavigationUpdate?: boolean;
segmentChangeRequests?: boolean;
}
export interface IVersionInfo {