1
1
// @flow
2
2
import React , { Component } from 'react' ;
3
- import { type SeriesData } from 'store/modules/series' ;
3
+ import { type SeriesData , type SeriesPostData } from 'store/modules/series' ;
4
4
import TextareaAutosize from 'react-autosize-textarea' ;
5
+ import Button from 'components/common/Button' ;
6
+ import { fromNow } from 'lib/common' ;
7
+ import Sortable from 'sortablejs' ;
8
+
5
9
import './SeriesEditor.scss' ;
6
10
7
11
type Props = {
@@ -10,15 +14,21 @@ type Props = {
10
14
11
15
type State = {
12
16
name : string ,
17
+ tempPosts : SeriesPostData [ ] ,
13
18
} ;
14
19
class SeriesEditor extends Component < Props , State > {
20
+ sortable : any = null ;
21
+ list = React . createRef ( ) ;
22
+
15
23
state = {
16
24
name : '' ,
25
+ tempPosts : [ ] ,
17
26
} ;
18
27
19
28
constructor ( props : Props ) {
20
29
super ( props ) ;
21
30
this . state . name = props . series . name ;
31
+ this . state . tempPosts = props . series . posts ;
22
32
}
23
33
24
34
onKeyPress = ( e : KeyboardEvent ) = > {
@@ -33,7 +43,35 @@ class SeriesEditor extends Component<Props, State> {
33
43
} ) ;
34
44
} ;
35
45
46
+ reorder = ( oldIndex : number , newIndex : number ) => {
47
+ const nextPosts = [ ...this . state . tempPosts ] ;
48
+ const temp = nextPosts [ oldIndex ] ;
49
+ nextPosts . splice ( oldIndex , 1 ) ;
50
+ nextPosts . splice ( newIndex , 0 , temp ) ;
51
+ this . setState ( {
52
+ tempPosts : nextPosts ,
53
+ } ) ;
54
+ } ;
55
+
56
+ initialize = ( ) => {
57
+ this . sortable = Sortable . create ( this . list . current , {
58
+ animation : 150 ,
59
+ chosenClass : 'chosen' ,
60
+ ghostClass : 'ghost' ,
61
+ dragClass : 'drag' ,
62
+ onUpdate : ( e : any ) => {
63
+ const { oldIndex, newIndex } = e ;
64
+ this . reorder ( oldIndex , newIndex ) ;
65
+ } ,
66
+ } ) ;
67
+ window . sortable = this . sortable ;
68
+ } ;
69
+ componentDidMount ( ) {
70
+ this . initialize ( ) ;
71
+ }
72
+
36
73
render ( ) {
74
+ const { series } = this . props ;
37
75
return (
38
76
< div className = "SeriesEditor" >
39
77
< TextareaAutosize
@@ -42,6 +80,18 @@ class SeriesEditor extends Component<Props, State> {
42
80
value = { this . state . name }
43
81
onKeyPress = { this . onKeyPress }
44
82
/>
83
+ < div className = "buttons-wrapper" >
84
+ < Button cancel > 취소</ Button >
85
+ < Button > 적용</ Button >
86
+ </ div >
87
+ < div className = "list" ref = { this . list } >
88
+ { this . state . tempPosts . map ( post => (
89
+ < div className = "item" key = { post . id } >
90
+ < div className = "post-title" > { post . title } </ div >
91
+ < div className = "post-date" > { fromNow ( post . released_at ) } </ div >
92
+ </ div >
93
+ ) ) }
94
+ </ div >
45
95
</ div >
46
96
) ;
47
97
}
0 commit comments