1919import React , { useState , useEffect } from 'react' ;
2020import { styled } from '@mui/material/styles' ;
2121import PropTypes from 'prop-types' ;
22- import TextField from '@mui/material/TextField' ;
2322import { FormattedMessage } from 'react-intl' ;
2423import Autocomplete from '@mui/material/Autocomplete' ;
2524import CheckBoxIcon from '@mui/icons-material/CheckBox' ;
2625import CheckBoxOutlineBlankIcon from '@mui/icons-material/CheckBoxOutlineBlank' ;
27- import Checkbox from '@mui/material/Checkbox' ;
28- import Box from '@mui/material/Box' ;
29- import Tooltip from '@mui/material/Tooltip' ;
3026import HelpOutline from '@mui/icons-material/HelpOutline' ;
3127import API from 'AppData/api' ;
28+ import {
29+ RadioGroup ,
30+ FormControlLabel ,
31+ FormLabel ,
32+ Radio ,
33+ TextField ,
34+ Checkbox ,
35+ Tooltip ,
36+ Box ,
37+ } from "@mui/material" ;
3238
3339const PREFIX = 'SharedOrganizations' ;
3440
@@ -39,9 +45,8 @@ const classes = {
3945
4046const StyledBox = styled ( Box ) ( ( { theme } ) => ( {
4147 [ `& .${ classes . tooltip } ` ] : {
42- position : 'absolute' ,
43- right : theme . spacing ( - 4 ) ,
4448 top : theme . spacing ( 1 ) ,
49+ marginLeft : theme . spacing ( 1 ) ,
4550 } ,
4651
4752 [ `& .${ classes . listItemText } ` ] : {
@@ -62,90 +67,114 @@ const checkedIcon = <CheckBoxIcon fontSize='small' />;
6267function SharedOrganizations ( props ) {
6368 const [ organizations , setOrganizations ] = useState ( { } ) ;
6469 const { api, configDispatcher } = props ;
70+ const [ selectionMode , setSelectionMode ] = useState ( "all" ) ;
6571
6672 useEffect ( ( ) => {
6773 API . getOrganizations ( ) . then ( ( response ) => setOrganizations ( response . body ) ) ;
74+ if ( api . visibleOrganizations . includes ( "all" ) ) {
75+ setSelectionMode ( "all" ) ;
76+ } else if ( api . visibleOrganizations . length === 0 ) {
77+ setSelectionMode ( "none" ) ;
78+ } else {
79+ setSelectionMode ( "select" ) ;
80+ }
6881 } , [ ] ) ;
6982
7083 if ( organizations && ! organizations . list ) {
7184 return null ;
7285 } else if ( organizations && organizations . list ) {
73- const allOption = { organizationId : "all" , displayName : "All Organizations" } ;
74- const optionsList = [ allOption , ...organizations . list ] ;
75- const handleChange = ( event , newValue ) => {
76- if ( newValue . some ( ( org ) => org . organizationId === "all" ) ) {
86+ const optionsList = organizations . list ;
87+ const handleRadioChange = ( event ) => {
88+ const { value } = event . target ;
89+ setSelectionMode ( value ) ;
90+ if ( value === "all" ) {
7791 configDispatcher ( { action : "visibleOrganizations" , value : [ "all" ] } ) ;
78- } else if ( newValue . length === 0 ) {
92+ } else if ( value === "none" ) {
7993 configDispatcher ( { action : "visibleOrganizations" , value : [ ] } ) ;
80- } else {
81- configDispatcher ( {
82- action : "visibleOrganizations" ,
83- value : newValue . map ( ( org ) => org . organizationId ) ,
84- } ) ;
8594 }
8695 } ;
96+ const handleDropdownChange = ( event , newValue ) => {
97+ configDispatcher ( {
98+ action : "visibleOrganizations" ,
99+ value : newValue . map ( ( org ) => org . organizationId ) ,
100+ } ) ;
101+ } ;
102+
87103 return (
88- < StyledBox style = { { position : 'relative' , marginTop : 10 } } >
89- < Autocomplete
90- multiple
91- fullWidth
92- limitTags = { 5 }
93- id = 'SharedOrganizations-autocomplete'
94- options = { optionsList }
95- noOptionsText = 'No Organizations selected'
96- disableCloseOnSelect
97- getOptionLabel = { ( option ) => option . displayName }
98- isOptionEqualToValue = { ( option , value ) => option . organizationId === value . organizationId }
99- value = {
100- api . visibleOrganizations . includes ( "all" )
101- ? [ allOption ]
102- : organizations . list . filter ( ( org ) => api . visibleOrganizations . includes ( org . organizationId ) )
103- }
104- onChange = { handleChange }
105- renderOption = { ( optionProps , option , { selected } ) => (
106- < li { ...optionProps } >
107- < Checkbox
108- key = { option . organizationId }
109- icon = { icon }
110- checkedIcon = { checkedIcon }
111- style = { { marginRight : 8 } }
112- checked = { selected }
113- />
114- { option . displayName }
115- </ li >
116- ) }
117- renderInput = { ( params ) => (
118- < TextField
119- { ...params }
120- fullWidth
121- label = 'Shared Organizations'
122- placeholder = 'Add Organizations'
123- helperText = 'Select organizations for sharing the API'
124- margin = 'normal'
125- variant = 'outlined'
104+ < StyledBox style = { { position : 'relative' } } >
105+ < Box display = 'flex' alignItems = 'center' >
106+ < FormLabel component = 'legend' >
107+ < FormattedMessage
108+ id = 'Apis.Details.Configuration.components.Shared.Organizations.label'
109+ defaultMessage = 'Share API with Organizations'
126110 />
127- ) }
128- />
129- < Tooltip
130- title = { (
131- < >
132- < p >
133- < FormattedMessage
134- id = 'Shared.organizations.dropdown.tooltip'
135- defaultMessage = { 'Allow to share API with other organizations.'
136- + ' There has to be pre-defined organizations in the'
137- + ' environment in order to share the API.' }
111+ </ FormLabel >
112+ < Tooltip
113+ title = { (
114+ < >
115+ < p >
116+ < FormattedMessage
117+ id = 'Apis.Details.Configuration.components.Shared.organizations.dropdown.tooltip'
118+ defaultMessage = { 'Allow to share API with other organizations.'
119+ + ' There has to be pre-defined organizations in the'
120+ + ' environment in order to share the API.' }
121+ />
122+ </ p >
123+ </ >
124+ ) }
125+ aria-label = 'Shared Organizations'
126+ placement = 'right-end'
127+ interactive
128+ className = { classes . tooltip }
129+ >
130+ < HelpOutline />
131+ </ Tooltip >
132+ </ Box >
133+ < RadioGroup value = { selectionMode } onChange = { handleRadioChange } row >
134+ < FormControlLabel value = 'all' control = { < Radio /> } label = 'All' />
135+ < FormControlLabel value = 'none' control = { < Radio /> } label = 'None' />
136+ < FormControlLabel value = 'select' control = { < Radio /> } label = 'Select' />
137+ </ RadioGroup >
138+ { selectionMode === "select" && (
139+ < Autocomplete
140+ multiple
141+ fullWidth
142+ limitTags = { 5 }
143+ id = 'SharedOrganizations-autocomplete'
144+ options = { optionsList }
145+ noOptionsText = 'No organizations registered'
146+ disableCloseOnSelect
147+ getOptionLabel = { ( option ) => option . displayName }
148+ isOptionEqualToValue = { ( option , value ) => option . organizationId === value . organizationId }
149+ value = { organizations . list . filter ( ( org ) =>
150+ api . visibleOrganizations . includes ( org . organizationId )
151+ ) }
152+ onChange = { handleDropdownChange }
153+ renderOption = { ( optionProps , option , { selected } ) => (
154+ < li { ...optionProps } >
155+ < Checkbox
156+ key = { option . organizationId }
157+ icon = { icon }
158+ checkedIcon = { checkedIcon }
159+ style = { { marginRight : 8 } }
160+ checked = { selected }
138161 />
139- </ p >
140- </ >
141- ) }
142- aria-label = 'Shared Organizations'
143- placement = 'right-end'
144- interactive
145- className = { classes . tooltip }
146- >
147- < HelpOutline />
148- </ Tooltip >
162+ { option . displayName }
163+ </ li >
164+ ) }
165+ renderInput = { ( params ) => (
166+ < TextField
167+ { ...params }
168+ fullWidth
169+ label = 'Shared Organizations'
170+ placeholder = 'Add Organizations'
171+ helperText = 'Select organizations for sharing the API'
172+ margin = 'normal'
173+ variant = 'outlined'
174+ />
175+ ) }
176+ />
177+ ) }
149178 </ StyledBox >
150179 ) ;
151180 }
0 commit comments