@@ -14,6 +14,7 @@ import CloseIcon from '@mui/icons-material/Close'
1414import MoreHorizIcon from '@mui/icons-material/MoreHoriz'
1515import CodesWithSystems from 'components/Hierarchy/CodesWithSystems'
1616import { SearchOutlined } from '@mui/icons-material'
17+ import { isEqual , sortBy } from 'lodash'
1718
1819type ExecutiveUnitsProps = {
1920 value : Hierarchy < ScopeElement > [ ]
@@ -25,16 +26,14 @@ type ExecutiveUnitsProps = {
2526
2627const ExecutiveUnits = ( { value, sourceType, disabled = false , onChange, label } : ExecutiveUnitsProps ) => {
2728 const [ population , setPopulation ] = useState < Hierarchy < ScopeElement > [ ] > ( [ ] )
28- const [ selectedPopulation , setSelectedPopulation ] = useState < Hierarchy < ScopeElement > [ ] > ( [ ] )
2929 const [ confirmedPopulation , setConfirmedPopulation ] = useState < Hierarchy < ScopeElement > [ ] > ( value )
3030 const [ loading , setLoading ] = useState ( disabled ? LoadingStatus . SUCCESS : LoadingStatus . FETCHING )
3131 const [ open , setOpen ] = useState ( false )
3232 const [ isExtended , setIsExtended ] = useState ( false )
3333
3434 const handleDelete = ( node : Hierarchy < ScopeElement > ) => {
35- const newSelectedPopulation = selectedPopulation . filter ( ( item ) => item . id !== node . id )
36- setSelectedPopulation ( newSelectedPopulation )
37- setConfirmedPopulation ( newSelectedPopulation )
35+ const newSelectedPopulation = value . filter ( ( item ) => item . id !== node . id )
36+ onChange ( newSelectedPopulation )
3837 }
3938
4039 useEffect ( ( ) => {
@@ -46,10 +45,6 @@ const ExecutiveUnits = ({ value, sourceType, disabled = false, onChange, label }
4645 if ( ! disabled ) handleFetchPopulation ( )
4746 } , [ ] )
4847
49- useEffect ( ( ) => {
50- onChange ( confirmedPopulation )
51- } , [ confirmedPopulation ] )
52-
5348 return (
5449 < InputWrapper >
5550 < Grid item container alignContent = "center" alignItems = { 'center' } >
@@ -75,44 +70,48 @@ const ExecutiveUnits = ({ value, sourceType, disabled = false, onChange, label }
7570 padding = "9px 3px 9px 12px"
7671 >
7772 < Grid container alignItems = "center" item xs = { 10 } >
78- { ! confirmedPopulation . length && < FormLabel component = "legend" > Sélectionner une unité exécutrice</ FormLabel > }
79- < CodesWithSystems
80- disabled = { disabled }
81- codes = { confirmedPopulation }
82- isExtended = { isExtended }
83- onDelete = { handleDelete }
84- />
73+ { ! value . length && < FormLabel component = "legend" > Sélectionner une unité exécutrice</ FormLabel > }
74+ < CodesWithSystems disabled = { disabled } codes = { value } isExtended = { isExtended } onDelete = { handleDelete } />
8575 </ Grid >
8676 < Grid item xs = { 2 } container justifyContent = "flex-end" >
87- { confirmedPopulation . length > 0 && isExtended && (
77+ { value . length > 0 && isExtended && (
8878 < IconButton size = "small" sx = { { color : '#5BC5F2' } } onClick = { ( ) => setIsExtended ( false ) } >
8979 < CloseIcon />
9080 </ IconButton >
9181 ) }
92- { confirmedPopulation . length > 0 && ! isExtended && (
82+ { value . length > 0 && ! isExtended && (
9383 < IconButton size = "small" sx = { { color : '#5BC5F2' } } onClick = { ( ) => setIsExtended ( true ) } >
9484 < MoreHorizIcon />
9585 </ IconButton >
9686 ) }
97- < IconButton sx = { { color : '#5BC5F2' } } size = "small" onClick = { ( ) => setOpen ( true ) } disabled = { disabled } >
87+ < IconButton
88+ sx = { { color : '#5BC5F2' } }
89+ size = "small"
90+ onClick = { ( ) => {
91+ setOpen ( true )
92+ setIsExtended ( false )
93+ } }
94+ disabled = { disabled }
95+ >
9896 { loading === LoadingStatus . FETCHING && < CircularProgress size = { 24 } /> }
9997 { loading === LoadingStatus . SUCCESS && < SearchOutlined /> }
10098 </ IconButton >
10199 </ Grid >
102100 </ Grid >
103101 < Panel
102+ mandatory = { isEqual ( sortBy ( confirmedPopulation ) , sortBy ( value ) ) }
104103 title = "Sélectionner une unité exécutrice"
105104 open = { open }
106105 onConfirm = { ( ) => {
107- setConfirmedPopulation ( selectedPopulation )
106+ onChange ( confirmedPopulation )
108107 setOpen ( false )
109108 } }
110109 onClose = { ( ) => setOpen ( false ) }
111110 >
112111 < ScopeTree
113112 baseTree = { population }
114- selectedNodes = { confirmedPopulation }
115- onSelect = { setSelectedPopulation }
113+ selectedNodes = { value }
114+ onSelect = { setConfirmedPopulation }
116115 sourceType = { sourceType }
117116 />
118117 </ Panel >
0 commit comments