1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01: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, IPermissionButtonProps,
} from 'component/common/PermissionButton/PermissionButton'; } from 'component/common/PermissionButton/PermissionButton';
export const UpdateButton = ({ ...rest }: IPermissionButtonProps) => { export const UpdateButton = ({
children = 'Save',
...rest
}: IPermissionButtonProps) => {
return ( return (
<PermissionButton type="submit" {...rest}> <PermissionButton type="submit" {...rest}>
Save {children}
</PermissionButton> </PermissionButton>
); );
}; };

View File

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

View File

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