/
index.js
103 lines (93 loc) 路 2.56 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React from 'react'
import { compose, withPropsOnChange, withHandlers, withStateHandlers } from 'recompose'
import withObservables from '@nozbe/with-observables'
import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider'
import Button from 'components/Button'
import ListItem from 'components/ListItem'
import style from './style'
const RowNastyCommentsItem = ({ blog, to, isActive, onClick }) => (
<ListItem title="Nasty comments"
countObservable={blog.nastyComments.observeCount()}
to={to}
isActive={isActive}
onClick={onClick} />
)
const RawPostItem = ({ post, to, onClick, isActive }) => (
<ListItem title={post.title}
countObservable={post.comments.observeCount()}
to={to}
isActive={isActive}
onClick={onClick} />
)
const PostItem = compose(
withObservables(['post'], ({ post }) => ({
post: post.observe(),
})),
withHandlers({
onClick: ({ onClick, post, showMain }) => e => {
onClick(e, post.id)
showMain()
},
}),
)(RawPostItem)
const NastyCommentsItem = compose(
withHandlers({
onClick: ({ onClick, blog, showMain }) => e => {
onClick(e, blog.id)
showMain()
},
}),
)(RowNastyCommentsItem)
const Blog = props => {
const { blog, posts, moderate, setActiveItem, activeItem, showMain } = props
return (
<div className={style.root}>
<div className={style.postLength}>
<span>Posts: {posts.length}</span>
<Button title="Moderate" onClick={moderate} />
</div>
<NastyCommentsItem blog={blog}
to={`/blog/${blog.id}/nasty/${blog.id}`}
isActive={blog.id === activeItem}
onClick={setActiveItem}
showMain={showMain}
/>
{posts.map(post => (
<PostItem post={post}
key={post.id}
to={`/blog/${blog.id}/post/${post.id}`}
isActive={post.id === activeItem}
onClick={setActiveItem}
showMain={showMain}
/>
))}
</div>
)
}
const enhance = compose(
withPropsOnChange(['match'], ({ match }) => ({
blogId: match.params.blogId,
})),
withObservables(['id'], ({ blogId, database }) => ({
blog: database.collections.get('blogs').findAndObserve(blogId),
})),
withObservables(['blog'], ({ blog }) => ({
posts: blog.posts.observe(),
})),
withStateHandlers(
{
activeItem: null,
},
{
setActiveItem: () => (e, postId) => ({
activeItem: postId,
}),
},
),
withHandlers({
moderate: props => async () => {
await props.blog.moderateAll()
},
}),
)
export default withDatabase(enhance(Blog))