Skip to content

Commit

Permalink
#4209: add 'add to draft' button for segments. (#4400)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thomasheartman authored Aug 3, 2023
1 parent 240cc2b commit 7cab19d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
7 changes: 5 additions & 2 deletions frontend/src/component/common/UpdateButton/UpdateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
);
};
36 changes: 26 additions & 10 deletions frontend/src/component/segments/EditSegment/EditSegment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
Expand All @@ -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));
}
Expand Down Expand Up @@ -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>
);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/interfaces/uiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface IFlags {
newProjectLayout?: boolean;
configurableFeatureTypeLifetimes?: boolean;
frontendNavigationUpdate?: boolean;
segmentChangeRequests?: boolean;
}

export interface IVersionInfo {
Expand Down

0 comments on commit 7cab19d

Please sign in to comment.