-
Notifications
You must be signed in to change notification settings - Fork 581
/
index.js
65 lines (58 loc) · 1.65 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
import React from 'react'
import { compose, withStateHandlers, withHandlers } from 'recompose'
import { Q } from '@nozbe/watermelondb'
import withObservables from '@nozbe/with-observables'
import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider'
import ListItem from 'components/ListItem'
import style from './style'
const RawBlogItem = ({ blog, to, onClick, isActive }) => (
<ListItem title={blog.name}
countObservable={blog.posts.observeCount()}
to={to}
isActive={isActive}
onClick={onClick} />
)
const BlogItem = compose(
withObservables(['blog'], ({ blog }) => ({
blog: blog.observe(),
})),
withHandlers({
onClick: ({ onClick, blog, showPostList }) => e => {
onClick(e, blog.id)
showPostList()
},
}),
)(RawBlogItem)
const BlogList = ({ blogs, setActiveItem, activeItem, showPostList }) => (
<div className={style.root}>
{blogs.map(blog => (
<BlogItem blog={blog}
showPostList={showPostList}
key={blog.id}
to={`/blog/${blog.id}`}
isActive={blog.id === activeItem}
onClick={setActiveItem} />
))}
{!blogs.length && <span className={style.placeholder}>Click “Generate x records” above!</span>}
</div>
)
const enhance = compose(
withObservables(['search'], ({ database, search }) => ({
blogs: database.collections
.get('blogs')
.query(Q.where('name', Q.like(`%${Q.sanitizeLikeString(search)}%`))),
})),
withStateHandlers(
{
activeItem: null,
},
{
setActiveItem: () => (e, postId) => {
return {
activeItem: postId,
}
},
},
),
)
export default withDatabase(enhance(BlogList))