1- import React , { useState } from 'react' ;
1+ import React , { useState , useEffect } from 'react' ;
22import { DragDropContext , Droppable , Draggable } from 'react-beautiful-dnd' ;
33import Typography from '@mui/material/Typography' ;
44import Chip from '@mui/material/Chip' ;
@@ -7,7 +7,7 @@ import palette from '../styles/oep-theme/palette.js';
77import variables from '../styles/oep-theme/variables.js' ;
88import StudyKeywords from './scenarioBundleUtilityComponents/StudyDescriptors' ;
99import handleOpenURL from './scenarioBundleUtilityComponents/handleOnClickTableIRI.jsx' ;
10- import HtmlTooltip from '../styles/oep-theme/components/tooltipStyles'
10+ import HtmlTooltip from '../styles/oep-theme/components/tooltipStyles' ;
1111
1212const reorder = ( list , startIndex , endIndex ) => {
1313 const result = Array . from ( list ) ;
@@ -21,7 +21,7 @@ const aspectStyle = {
2121 padding : variables . spacing [ 3 ] ,
2222 color : palette . text . primary ,
2323 fontSize : variables . fontSize . sm ,
24- lineHeight : variables . lineHeight . sm
24+ lineHeight : variables . lineHeight . sm ,
2525} ;
2626
2727const getItemStyle = ( isDragging , draggableStyle , index ) => ( {
@@ -38,78 +38,105 @@ const getItemStyle = (isDragging, draggableStyle, index) => ({
3838 ...draggableStyle ,
3939} ) ;
4040
41- const getListStyle = isDraggingOver => ( {
41+ const getListStyle = ( isDraggingOver ) => ( {
4242 background : isDraggingOver ? 'white' : 'white' ,
4343 display : 'flex' ,
4444 overflow : 'auto' ,
4545 width : '100%' ,
4646 minHeight : '20rem' ,
4747} ) ;
4848
49- export default function ComparisonBoardItems ( props ) {
49+ export default function ComparisonBoardItems ( props ) {
5050 const { elements, c_aspects } = props ;
51- const [ state , setState ] = useState ( { items : elements } ) ;
5251
53- function onDragEnd ( result ) {
54- if ( ! result . destination ) {
55- return ;
56- }
57- if ( result . destination . index === result . source . index ) {
58- return ;
59- }
60- const newItems = reorder (
61- state . items ,
62- result . source . index ,
63- result . destination . index
64- ) ;
65- setState ( {
66- items : newItems ,
67- } ) ;
68- }
52+ const [ state , setState ] = useState ( { items : elements } ) ;
53+ const [ mounted , setMounted ] = useState ( false ) ;
6954
70- const label = { inputProps : { 'aria-label' : 'Checkbox demo' } } ;
55+ useEffect ( ( ) => {
56+ setState ( { items : elements } ) ;
57+ } , [ elements ] ) ;
58+
59+ useEffect ( ( ) => {
60+ const timer = setTimeout ( ( ) => setMounted ( true ) , 30 ) ; // allow DOM to stabilize
61+ return ( ) => clearTimeout ( timer ) ;
62+ } , [ ] ) ;
63+
64+ const onDragEnd = ( result ) => {
65+ if ( ! result . destination || result . destination . index === result . source . index ) return ;
66+
67+ const newItems = reorder ( state . items , result . source . index , result . destination . index ) ;
68+ setState ( { items : newItems } ) ;
69+ } ;
70+
71+ if ( ! mounted || ! state . items ?. length ) return null ;
7172
7273 return (
7374 < div style = { { overflow : 'auto' , marginBottom : variables . spacing [ 6 ] } } >
7475 < DragDropContext onDragEnd = { onDragEnd } >
75- < Droppable
76- droppableId = "droppable"
77- direction = "horizontal"
78- >
79- { ( provided , snapshot ) => (
76+ < Droppable droppableId = "droppable-scenarios" direction = "horizontal" >
77+ { ( provided , snapshot ) => (
8078 < div
8179 ref = { provided . innerRef }
82- style = { getListStyle ( snapshot . isDraggingOver ) }
8380 { ...provided . droppableProps }
81+ style = { getListStyle ( snapshot . isDraggingOver ) }
8482 >
85- { state . items . map ( ( item , index ) => (
86- < Draggable key = { item . data . uid } draggableId = { item . data . uid } index = { index } >
87- { ( provided , snapshot ) => (
88- < div
89- ref = { provided . innerRef }
90- { ...provided . draggableProps }
91- { ...provided . dragHandleProps }
92- style = { getItemStyle (
93- snapshot . isDragging ,
94- provided . draggableProps . style ,
95- index
96- )
97- }
98- >
99- < div style = { { display : 'flex' , flexDirection : 'column' , alignItems : 'center' , justifyContent :'center' , height : '4rem' , marginBottom : variables . spacing [ 3 ] , backgroundColor : index === 0 ? palette . background . highlight : palette . background . lighter , color : index === 0 ? palette . primary . contrastText : palette . text . primary } } >
100-
101- < Typography variant = "h6" >
102- { index === 0 ? < b > { item . acronym } </ b > : item . acronym }
103- </ Typography >
104- < Typography variant = "caption" >
105- { index === 0 ? 'Base scenario' : '' }
106- </ Typography >
107- </ div >
83+ { state . items . map ( ( item , index ) => {
84+ const uid = String ( item ?. data ?. uid ) ;
85+ if ( ! uid ) return null ;
10886
109- < div style = { { height : '60vh' , overflow : 'auto' , } } >
87+ return (
88+ < Draggable key = { uid } draggableId = { uid } index = { index } >
89+ { ( provided , snapshot ) => (
90+ < div
91+ ref = { provided . innerRef }
92+ { ...provided . draggableProps }
93+ { ...provided . dragHandleProps }
94+ style = { getItemStyle (
95+ snapshot . isDragging ,
96+ provided . draggableProps . style ,
97+ index
98+ ) }
99+ >
100+ { /* --- DRAGGABLE CONTENT HERE --- */ }
101+ < div
102+ style = { {
103+ display : 'flex' ,
104+ flexDirection : 'column' ,
105+ alignItems : 'center' ,
106+ justifyContent : 'center' ,
107+ height : '4rem' ,
108+ marginBottom : variables . spacing [ 3 ] ,
109+ backgroundColor :
110+ index === 0
111+ ? palette . background . highlight
112+ : palette . background . lighter ,
113+ color :
114+ index === 0
115+ ? palette . primary . contrastText
116+ : palette . text . primary ,
117+ } }
118+ >
119+ < Typography variant = "h6" >
120+ { index === 0 ? < b > { item . acronym } </ b > : item . acronym }
121+ </ Typography >
122+ < Typography variant = "caption" >
123+ { index === 0 ? 'Base scenario' : '' }
124+ </ Typography >
125+ </ div >
110126
127+ < div style = { { height : '60vh' , overflow : 'auto' } } >
128+ { c_aspects . includes ( 'Study name' ) && (
129+ < div style = { aspectStyle } >
130+ < Typography variant = "subtitle2" gutterBottom >
131+ < b > Study name:</ b >
132+ </ Typography >
133+ < Typography variant = "body2" >
134+ { item . data . study_label }
135+ </ Typography >
136+ </ div >
137+ ) }
111138
112- { c_aspects . includes ( "Study name" ) && < div style = { aspectStyle } >
139+ { c_aspects . includes ( "Study name" ) && < div style = { aspectStyle } >
113140 < Typography variant = "subtitle2" gutterBottom component = "div" >
114141 < b > Study name:</ b >
115142 </ Typography >
@@ -277,13 +304,13 @@ export default function ComparisonBoardItems (props) {
277304 </ HtmlTooltip >
278305 ) ) }
279306 </ div > }
280-
281- </ div >
282-
283- </ div >
284- ) }
285- </ Draggable >
286- ) ) }
307+ </ div >
308+ </ div >
309+ ) }
310+ </ Draggable >
311+ ) ;
312+ } ) }
313+ { provided . placeholder }
287314 </ div >
288315 ) }
289316 </ Droppable >
0 commit comments