22
33import  React ,  {  PropTypes  }  from  'react' ; 
44import  ItemTypes  from  './ItemTypes' ; 
5- import  {  DragSource ,  DropTarget ,  ObservePolyfill  }  from  'react-dnd' ; 
6- 
7- class  CardDragSource  extends  DragSource  { 
8-   beginDrag ( )  { 
9-     return  { 
10-       item : { 
11-         id : this . component . props . id 
12-       } 
13-     } ; 
14-   } 
15- } 
16- 
17- class  CardDropTarget  extends  DropTarget  { 
18-   over ( monitor )  { 
19-     const  item  =  monitor . getItem ( ) ; 
20-     this . component . props . moveCard ( item . id ,  this . component . props . id ) ; 
21-   } 
22- } 
5+ import  {  configureDragDrop  }  from  'react-dnd' ; 
236
247const  style  =  { 
258  border : '1px dashed gray' , 
@@ -28,48 +11,55 @@ const style = {
2811  margin : '0.5rem' 
2912} ; 
3013
31- const  Card  =  React . createClass ( { 
32-   contextTypes : { 
33-     dragDrop : PropTypes . object . isRequired 
34-   } , 
35- 
36-   mixins : [ ObservePolyfill ( { 
37-     constructor ( )  { 
38-       this . dragSource  =  new  CardDragSource ( this ) ; 
39-       //this.dropTarget = new CardDropTarget(this); 
40-     } , 
14+ const  propTypes  =  { 
15+   id : PropTypes . any . isRequired , 
16+   text : PropTypes . string . isRequired , 
17+   isDragging : PropTypes . bool . isRequired , 
18+   overlappingCardId : PropTypes . bool . isRequired , 
19+   moveCard : PropTypes . func . isRequired , 
20+   connectDragDrop : PropTypes . func . isRequired 
21+ } ; 
4122
42-     observe ( )  { 
43-       return  { 
44-         dragSource : this . dragSource . connectTo ( this . context . dragDrop ,  ItemTypes . CARD ) , 
45-         //dropTarget: this.dropTarget.connectTo(this.context.dragDrop, ItemTypes.CARD) 
46-       } ; 
23+ class  Card  { 
24+   componentWillReceiveProps ( nextProps )  { 
25+     if  ( ! this . props . overlappingCardId  &&  nextProps . overlappingCardId )  { 
26+       const  {  id,  overlappingCardId }  =  nextProps ; 
27+       if  ( overlappingCardId  !==  id )  { 
28+         this . props . moveCard ( overlappingCardId ,  id ) ; 
29+       } 
4730    } 
48-   } ) ] , 
49- 
50-   propTypes : { 
51-     id : PropTypes . any . isRequired , 
52-     text : PropTypes . string . isRequired , 
53-     moveCard : PropTypes . func . isRequired 
54-   } , 
31+   } 
5532
5633  render ( )  { 
57-     const  {  text }  =  this . props ; 
58- 
59-     // TODO: refs totally suck here. 
60-     // Need to rethink this. 
61- 
62-     const  {  isDragging,  ref : sourceRef  }  =  this . state . data . dragSource ; 
63-     //const { ref: targetRef } = this.state.data.dropTarget; 
34+     const  {  text,  isDragging,  connectDragDrop }  =  this . props ; 
6435    const  opacity  =  isDragging  ? 0  : 1 ; 
6536
6637    return  ( 
67-       < div  ref = { sourceRef } 
38+       < div  ref = { connectDragDrop } 
6839           style = { {  ...style ,  opacity } } > 
6940        { text } 
7041      </ div > 
7142    ) ; 
7243  } 
73- } ) ; 
44+ } 
7445
75- export  default  Card ; 
46+ export  default  configureDragDrop ( Card ,  { 
47+   getHandlers ( props ,  sourceFor ,  targetFor )  { 
48+     return  { 
49+       cardSource : sourceFor ( ItemTypes . CARD ,  { 
50+         beginDrag ( props )  { 
51+           return  {  id : props . id  } ; 
52+         } 
53+       } ) , 
54+       cardTarget : targetFor ( ItemTypes . CARD ) 
55+     } ; 
56+   } , 
57+ 
58+   getProps ( connect ,  monitor ,  handlers )  { 
59+     return  { 
60+       isDragging : monitor . isDragging ( handlers . cardSource ) , 
61+       overlappingCardId : monitor . isOver ( handlers . cardTarget )  &&  monitor . getItem ( ) . id  ||  null , 
62+       connectDragDrop : connect ( handlers . cardSource ,  handlers . cardTarget ) 
63+     } ; 
64+   } 
65+ } ) ; 
0 commit comments