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

feat: Add manage segment overrides permission in UI #2936

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
305 changes: 161 additions & 144 deletions frontend/web/components/SegmentOverrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,159 +559,176 @@ class TheComponent extends Component {
const isLimitReached =
segmentOverrideLimitAlert.percentage &&
segmentOverrideLimitAlert.percentage >= 100
const manageSegmentsEnabled = Utils.getFlagsmithHasFeature(
'manage_segment_overrides_env_role',
)
return (
<div>
<div className='mt-2 mb-2'>
{!this.props.id &&
!this.props.disableCreate &&
!this.props.showCreateSegment &&
!this.props.readOnly && (
<Flex className='text-left'>
<SegmentSelect
disabled={!!isLimitReached}
projectId={this.props.projectId}
data-test='select-segment'
placeholder='Create a Segment Override...'
filter={filter}
value={this.state.selectedSegment}
onChange={(selectedSegment) =>
this.setState({ selectedSegment }, this.addItem)
}
/>
</Flex>
)}
{!this.props.showCreateSegment &&
!this.props.readOnly &&
!this.props.disableCreate && (
<div className='text-right'>
<Button
className='mt-2'
onClick={() => {
this.setState({ selectedSegment: null })
this.props.setShowCreateSegment(true)
}}
theme='outline'
disabled={!!isLimitReached}
>
Create Feature-Specific Segment
</Button>
</div>
)}
{this.props.showCreateSegment && !this.state.segmentEditId && (
<div className='create-segment-overrides'>
<CreateSegmentModal
onComplete={(segment) => {
if (this.state.selectedSegment) {
this.props.setShowCreateSegment(false)
} else {
this.props.setShowCreateSegment(false)
this.setState(
{
selectedSegment: {
label: segment.name,
value: segment.id,
},
},
this.addItem,
)
}
}}
onCancel={() => {
this.props.setShowCreateSegment(false)
}}
condensed
feature={this.props.feature}
environmentId={this.props.environmentId}
projectId={this.props.projectId}
/>
</div>
)}
{this.props.showCreateSegment && this.state.segmentEditId && (
<CreateSegmentModal
segment={this.state.segmentEditId}
isEdit
condensed
onComplete={() => {
this.setState({
segmentEditId: undefined,
})
this.props.setShowCreateSegment(false)
}}
onCancel={() => {
this.setState({ segmentEditId: undefined })
this.props.setShowCreateSegment(false)
}}
environmentId={this.props.environmentId}
projectId={this.props.projectId}
/>
)}
{visibleValues &&
!!visibleValues.length &&
!this.props.showCreateSegment && (
<div
style={isLoading ? { opacity: 0.5 } : null}
className='overflow-visible'
>
{!this.props.id && (
<div className='my-4'>
<InfoMessage className='mb-4 text-left faint'>
Prioritise a segment override by dragging it to the top of
the list.
<br />
Segment overrides will only apply when you identify via
the SDK.{' '}
<a
target='_blank'
href='https://docs.flagsmith.com/basic-features/managing-segments'
rel='noreferrer'
<Permission
level='project'
permission={'MANAGE_SEGMENTS'}
id={this.props.projectId}
>
Comment on lines +567 to +571
Copy link
Contributor

@zachaysan zachaysan Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this permission be MANAGE_SEGMENT_OVERRIDES?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the advice you left in this comment. #1676 (comment)

In addition to the above, please also hide the Create Feature-Specific Segment button if the user is missing the project permission MANAGE_SEGMENTS since they will not be able to create a segment if that permission is missing.

I thought it made sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just in case, this button creates a Feature-Specific segment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, of course. Thanks for double checking Novak!

{({ permission: manageSegments }) => {
return (
<div className='mt-2 mb-2'>
{!this.props.id &&
!this.props.disableCreate &&
!this.props.showCreateSegment &&
!this.props.readOnly && (
<Flex className='text-left'>
<SegmentSelect
disabled={!!isLimitReached}
projectId={this.props.projectId}
data-test='select-segment'
placeholder='Create a Segment Override...'
filter={filter}
value={this.state.selectedSegment}
onChange={(selectedSegment) =>
this.setState({ selectedSegment }, this.addItem)
}
/>
</Flex>
)}
{!this.props.showCreateSegment &&
!this.props.readOnly &&
!this.props.disableCreate && (
<div className='text-right'>
<Button
className='mt-2'
onClick={() => {
this.setState({ selectedSegment: null })
this.props.setShowCreateSegment(true)
}}
theme='outline'
disabled={
manageSegmentsEnabled
? !manageSegments || !!isLimitReached
: false
}
>
Check the Docs for more details
</a>
.
</InfoMessage>
<SegmentOverrideLimit
id={this.props.environmentId}
maxSegmentOverridesAllowed={ProjectStore.getMaxSegmentOverridesAllowed()}
Create Feature-Specific Segment
</Button>
</div>
)}
{this.props.showCreateSegment && !this.state.segmentEditId && (
<div className='create-segment-overrides'>
<CreateSegmentModal
onComplete={(segment) => {
this.props.setShowCreateSegment(false)
if (!this.state.selectedSegment) {
this.setState(
{
selectedSegment: {
label: segment.name,
value: segment.id,
},
},
this.addItem,
)
}
}}
onCancel={() => {
this.props.setShowCreateSegment(false)
}}
condensed
feature={this.props.feature}
environmentId={this.props.environmentId}
projectId={this.props.projectId}
/>
</div>
)}
{this.props.showCreateSegment && this.state.segmentEditId && (
<CreateSegmentModal
segment={this.state.segmentEditId}
isEdit
condensed
onComplete={() => {
this.setState({
segmentEditId: undefined,
})
this.props.setShowCreateSegment(false)
}}
onCancel={() => {
this.setState({ segmentEditId: undefined })
this.props.setShowCreateSegment(false)
}}
environmentId={this.props.environmentId}
projectId={this.props.projectId}
/>
)}
{visibleValues &&
!!visibleValues.length &&
!this.props.showCreateSegment && (
<div
style={isLoading ? { opacity: 0.5 } : null}
className='overflow-visible'
>
{!this.props.id && (
<div className='my-4'>
<InfoMessage className='mb-4 text-left faint'>
Prioritise a segment override by dragging it to the
top of the list.
<br />
Segment overrides will only apply when you identify
via the SDK.{' '}
<a
target='_blank'
href='https://docs.flagsmith.com/basic-features/managing-segments'
rel='noreferrer'
>
Check the Docs for more details
</a>
.
</InfoMessage>
<SegmentOverrideLimit
id={this.props.environmentId}
maxSegmentOverridesAllowed={ProjectStore.getMaxSegmentOverridesAllowed()}
/>
</div>
)}

{value && (
<>
<InnerComponent
disabled={isLoading || this.props.readOnly}
id={this.props.id}
name={this.props.name}
controlValue={this.props.controlValue}
multivariateOptions={multivariateOptions}
confirmRemove={this.confirmRemove}
setVariations={this.setVariations}
toggle={this.toggle}
setValue={this.setValue}
readOnly={this.props.readOnly}
showEditSegment={this.props.showEditSegment}
environmentId={this.props.environmentId}
projectId={this.props.projectId}
setShowCreateSegment={this.props.setShowCreateSegment}
items={value.map((v) => ({
...v,
}))}
setSegmentEditId={this.setSegmentEditId}
onSortEnd={this.onSortEnd}
projectFlag={this.props.projectFlag}
/>
<div className='text-left mt-4'>
<JSONReference
showNamesButton
title={'Segment Overrides'}
json={value}
/>
{value && (
<>
<InnerComponent
disabled={isLoading || this.props.readOnly}
id={this.props.id}
name={this.props.name}
controlValue={this.props.controlValue}
multivariateOptions={multivariateOptions}
confirmRemove={this.confirmRemove}
setVariations={this.setVariations}
toggle={this.toggle}
setValue={this.setValue}
readOnly={this.props.readOnly}
showEditSegment={this.props.showEditSegment}
environmentId={this.props.environmentId}
projectId={this.props.projectId}
setShowCreateSegment={
this.props.setShowCreateSegment
}
items={value.map((v) => ({
...v,
}))}
setSegmentEditId={this.setSegmentEditId}
onSortEnd={this.onSortEnd}
projectFlag={this.props.projectFlag}
/>
<div className='text-left mt-4'>
<JSONReference
showNamesButton
title={'Segment Overrides'}
json={value}
/>
</div>
</>
)}
</div>
</>
)}
)}
</div>
)}
</div>
)
}}
</Permission>
</div>
)
}
Expand Down
32 changes: 20 additions & 12 deletions frontend/web/components/modals/AssociatedSegmentOverrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class TheComponent extends Component {
id={this.props.id}
projectId={this.props.projectId}
environmentId={this.state.selectedEnv}
readOnly={this.props.readOnly}
/>
</div>
)
Expand Down Expand Up @@ -196,6 +197,7 @@ class TheComponent extends Component {
id={this.props.id}
projectId={this.props.projectId}
environmentId={v.env.api_key}
readOnly={this.props.readOnly}
/>
</div>
</div>
Expand Down Expand Up @@ -296,6 +298,7 @@ export default class SegmentOverridesInner extends Component {
originalSegmentOverrides,
projectFlag,
projectId,
readOnly,
segmentOverrides,
updateSegments,
} = this.props
Expand Down Expand Up @@ -366,11 +369,14 @@ export default class SegmentOverridesInner extends Component {
value={segmentOverride}
controlValue={projectFlag.feature_state_value}
onChange={updateSegments}
readOnly={readOnly}
/>
<div className='text-right'>
<Button disabled={this.state.isSaving} onClick={save}>
{this.state.isSaving ? 'Saving' : 'Save'}
</Button>
{!readOnly && (
<Button disabled={this.state.isSaving} onClick={save}>
{this.state.isSaving ? 'Saving' : 'Save'}
</Button>
)}
</div>
</div>
)
Expand Down Expand Up @@ -411,7 +417,7 @@ class SegmentOverridesInnerAdd extends Component {
}
}
render() {
const { environmentId, id, ignoreFlags, projectId } = this.props
const { environmentId, id, ignoreFlags, projectId, readOnly } = this.props
const addValue = (featureId, feature) => {
const env = ProjectStore.getEnvs().find((v) => v.name === environmentId)
const item = {
Expand Down Expand Up @@ -448,14 +454,16 @@ class SegmentOverridesInnerAdd extends Component {
{() => {
return (
<div className='mt-2'>
<FlagSelect
disabled={!!segmentOverrideLimitAlert}
onlyInclude={this.props.feature}
placeholder='Create a Segment Override...'
projectId={projectId}
ignore={ignoreFlags}
onChange={addValue}
/>
{!readOnly && (
<FlagSelect
disabled={!!segmentOverrideLimitAlert}
onlyInclude={this.props.feature}
placeholder='Create a Segment Override...'
projectId={projectId}
ignore={ignoreFlags}
onChange={addValue}
/>
)}
</div>
)
}}
Expand Down
Loading