Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit 148431b

Browse files
committed
Implements Reordering system via Sortablejs
1 parent 84025e0 commit 148431b

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

velog-frontend/src/components/series/SeriesEditor/SeriesEditor.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// @flow
22
import React, { Component } from 'react';
3-
import { type SeriesData } from 'store/modules/series';
3+
import { type SeriesData, type SeriesPostData } from 'store/modules/series';
44
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+
59
import './SeriesEditor.scss';
610

711
type Props = {
@@ -10,15 +14,21 @@ type Props = {
1014

1115
type State = {
1216
name: string,
17+
tempPosts: SeriesPostData[],
1318
};
1419
class SeriesEditor extends Component<Props, State> {
20+
sortable: any = null;
21+
list = React.createRef();
22+
1523
state = {
1624
name: '',
25+
tempPosts: [],
1726
};
1827

1928
constructor(props: Props) {
2029
super(props);
2130
this.state.name = props.series.name;
31+
this.state.tempPosts = props.series.posts;
2232
}
2333

2434
onKeyPress = (e: KeyboardEvent) => {
@@ -33,7 +43,35 @@ class SeriesEditor extends Component<Props, State> {
3343
});
3444
};
3545

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+
3673
render() {
74+
const { series } = this.props;
3775
return (
3876
<div className="SeriesEditor">
3977
<TextareaAutosize
@@ -42,6 +80,18 @@ class SeriesEditor extends Component<Props, State> {
4280
value={this.state.name}
4381
onKeyPress={this.onKeyPress}
4482
/>
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>
4595
</div>
4696
);
4797
}
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
@import 'utils';
22

33
.SeriesEditor {
4+
color: $oc-gray-9;
45
.title {
56
resize: none !important;
67
width: 100%;
78
margin-top: 1.5rem;
8-
color: $oc-gray-9;
99
font-size: 2.5rem;
1010
font-weight: 800;
1111
border-bottom: 1px solid $oc-gray-2;
@@ -14,5 +14,41 @@
1414
border: 1px solid $oc-gray-3;
1515
outline: none;
1616
font-family: inherit;
17+
display: block;
18+
}
19+
.buttons-wrapper {
20+
margin-top: 0.5rem;
21+
display: flex;
22+
justify-content: flex-end;
23+
}
24+
.list {
25+
margin-top: 2rem;
26+
.item {
27+
background: $oc-gray-0;
28+
border: 1px solid $oc-gray-2;
29+
padding: 1rem;
30+
cursor: pointer;
31+
.post-title {
32+
font-size: 1.3125rem;
33+
font-weight: 600;
34+
}
35+
.date {
36+
margin-top: 0.5rem;
37+
color: $oc-gray-6;
38+
font-size: 0.875rem;
39+
}
40+
41+
&.chosen {
42+
border: 1px solid $oc-violet-6;
43+
}
44+
&.ghost {
45+
opacity: 0.4;
46+
}
47+
&.drag {
48+
}
49+
& + .item {
50+
margin-top: 0.5rem;
51+
}
52+
}
1753
}
1854
}

0 commit comments

Comments
 (0)