Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: take into account project segments permission in form #5352

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions frontend/src/component/segments/SegmentEmpty.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { styled, Typography } from '@mui/material';
import { Link } from 'react-router-dom';
import { CREATE_SEGMENT } from 'component/providers/AccessProvider/permissions';
import {
CREATE_SEGMENT,
UPDATE_PROJECT_SEGMENT,
} from 'component/providers/AccessProvider/permissions';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import AccessContext from 'contexts/AccessContext';
import { useContext } from 'react';
import { useOptionalPathParam } from 'hooks/useOptionalPathParam';

const StyledDiv = styled('div')(({ theme }) => ({
display: 'flex',
Expand Down Expand Up @@ -35,6 +39,7 @@ const StyledLink = styled(Link)(({ theme }) => ({
}));

export const SegmentEmpty = () => {
const projectId = useOptionalPathParam('projectId');
const { hasAccess } = useContext(AccessContext);

return (
Expand All @@ -46,7 +51,10 @@ export const SegmentEmpty = () => {
and can be reused.
</StyledParagraph>
<ConditionallyRender
condition={hasAccess(CREATE_SEGMENT)}
condition={hasAccess(
[CREATE_SEGMENT, UPDATE_PROJECT_SEGMENT],
projectId,
)}
show={
<StyledLink to='/segments/create'>
Create your first segment
Expand Down
1 change: 1 addition & 0 deletions frontend/src/component/segments/SegmentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const SegmentForm: React.FC<ISegmentProps> = ({
condition={currentStep === 2}
show={
<SegmentFormStepTwo
project={project}
constraints={constraints}
setConstraints={setConstraints}
setCurrentStep={setCurrentStep}
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/component/segments/SegmentFormStepTwo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CreateUnleashContext } from 'component/context/CreateUnleashContext/Cre
import {
CREATE_CONTEXT_FIELD,
CREATE_SEGMENT,
UPDATE_PROJECT_SEGMENT,
UPDATE_SEGMENT,
} from 'component/providers/AccessProvider/permissions';
import useUnleashContext from 'hooks/api/getters/useUnleashContext/useUnleashContext';
Expand All @@ -32,6 +33,7 @@ import { useSegmentLimits } from 'hooks/api/getters/useSegmentLimits/useSegmentL
import { GO_BACK } from 'constants/navigate';

interface ISegmentFormPartTwoProps {
project?: string;
constraints: IConstraint[];
setConstraints: React.Dispatch<React.SetStateAction<IConstraint[]>>;
setCurrentStep: React.Dispatch<React.SetStateAction<SegmentFormStep>>;
Expand Down Expand Up @@ -101,6 +103,7 @@ const StyledCancelButton = styled(Button)(({ theme }) => ({

export const SegmentFormStepTwo: React.FC<ISegmentFormPartTwoProps> = ({
children,
project,
constraints,
setConstraints,
setCurrentStep,
Expand All @@ -112,7 +115,10 @@ export const SegmentFormStepTwo: React.FC<ISegmentFormPartTwoProps> = ({
const { context = [] } = useUnleashContext();
const [open, setOpen] = useState(false);
const segmentValuesCount = useSegmentValuesCount(constraints);
const modePermission = mode === 'create' ? CREATE_SEGMENT : UPDATE_SEGMENT;
const modePermission =
mode === 'create'
? [CREATE_SEGMENT, UPDATE_PROJECT_SEGMENT]
: [UPDATE_SEGMENT, UPDATE_PROJECT_SEGMENT];
const { segmentValuesLimit } = useSegmentLimits();

const overSegmentValuesLimit: boolean = Boolean(
Expand Down Expand Up @@ -197,7 +203,7 @@ export const SegmentFormStepTwo: React.FC<ISegmentFormPartTwoProps> = ({
ref={constraintsAccordionListRef}
constraints={constraints}
setConstraints={
hasAccess(modePermission)
hasAccess(modePermission, project)
? setConstraints
: undefined
}
Expand Down
Loading