/
DraftList.js
78 lines (72 loc) · 2.87 KB
/
DraftList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React, {Component} from "react";
import Model from "../models.js"
import { List, Card, Icon, Divider, Avatar } from "antd";
import { Link, NavLink } from "react-router-dom";
class DraftList extends Component {
constructor(props) {
super(props);
this.state = {
posts: []
}
}
componentWillMount() {
var self = this;
Model.loadDrafts(this.props.match.params.username).then(() => {
Model.drafts = Model.drafts.map(function(post) {
if (post.fields.user) {
post.editurl = "/writes/@"+post.fields.user.username + "/edit/" + self.fields.slug;
post.viewurl = "/writes/@"+post.fields.user.username + "/posts/" + self.fields.slug;
} else {
post.editurl = "/writes/@" + post.fields.session + "/edit/" + post.fields.slug;
post.viewurl = "/writes/@" + post.fields.session + "/posts/" + post.fields.slug;
}
return post;
});
self.setState({
posts: Model.drafts
})
})
}
deletePost(post) {
Model.deletePost(post.fields.slug)
var posts = this.state.posts.filter(p => {
return p.fields.slug != post.fields.slug
})
this.setState({posts: posts})
}
render() {
return (
<Card style={{minHeight: '100vh', border: '0'}}>
<Divider orientation="left">Drafts</Divider>
{console.log(this.state)}
{!this.state.posts.length &&
<div className="no-post-placeholder">
<p><Icon type="smile" style={{margin: '0 10px'}}/>You have no drafts yet. <a href="#">Start Writing!</a></p>
</div>
}
{!!this.state.posts.length &&
<List
itemLayout="horizontal"
dataSource={this.state.posts}
renderItem={item => (
<List.Item
actions={[
<Link to={item.editurl}>Edit</Link>,
<Link to={item.viewurl}>View</Link>,
<a onClick={() => {this.deletePost(item)}}>Delete</a>
]}
>
<List.Item.Meta
avatar={<Avatar>{item.fields.title[0]}</Avatar>}
title={<Link to={item.editurl}>{item.fields.title}</Link>}
description={new Date(item.fields.created_date).toDateString()}
/>
</List.Item>
)}
/>
}
</Card>
)
}
}
export default DraftList;