Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 2bab977

Browse files
committed
feat(UserPublishedComments): with different thread & refactor
1 parent 77616b9 commit 2bab977

File tree

24 files changed

+837
-27
lines changed

24 files changed

+837
-27
lines changed

containers/UserContent/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React from 'react'
88
import { inject, observer } from 'mobx-react'
99

1010
import UserPublished from '../UserPublished'
11+
import UserPublishedComments from '../UserPublishedComments'
1112
import UserBilling from '../UserBilling'
1213
import UserSettings from '../UserSettings'
1314
import UserStared from '../UserStared'
@@ -26,7 +27,7 @@ import * as logic from './logic'
2627
const debug = makeDebugger('C:UserContent')
2728
/* eslint-enable no-unused-vars */
2829

29-
const fakeThreads = [
30+
const taberThreads = [
3031
{
3132
title: '发布',
3233
raw: 'publish',
@@ -56,7 +57,7 @@ const fakeThreads = [
5657
const TabberContent = ({ active }) => {
5758
switch (active) {
5859
case USER_THREAD.COMMENTS: {
59-
return <h2>COMMENTS</h2>
60+
return <UserPublishedComments />
6061
}
6162
case USER_THREAD.FAVORITES: {
6263
return <UserFavorited />
@@ -91,7 +92,7 @@ class UserContentContainer extends React.Component {
9192
<MainWrapper>
9293
<TabberWrapper className="tabs-with-bottom">
9394
<Tabber
94-
source={fakeThreads}
95+
source={taberThreads}
9596
onChange={logic.tabChange}
9697
active={activeThread}
9798
/>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import React from 'react'
2+
import TimeAgo from 'timeago-react'
3+
4+
import { MarkDownRender, CommentLoading, EmptyLabel } from '../../components'
5+
import {
6+
Wrapper,
7+
CommentBlock,
8+
CommentDivider,
9+
CommentBox,
10+
CommentHeader,
11+
Avatar,
12+
AvatarInfo,
13+
Nickname,
14+
When,
15+
CommentBody,
16+
} from './styles/comments_to_post'
17+
18+
import Parent from './Parent'
19+
20+
import { uid, TYPE, Trans } from '../../utils'
21+
22+
const CommentsToContent = ({ data, thread, curView }) => {
23+
const { entries } = data
24+
25+
switch (curView) {
26+
case TYPE.RESULT: {
27+
return (
28+
<Wrapper>
29+
{entries.map(comment => (
30+
<div key={uid.gen()}>
31+
<CommentBlock>
32+
<Parent data={comment} thread={thread} />
33+
<CommentBox>
34+
<CommentHeader>
35+
<Avatar src={comment.author.avatar} />
36+
<AvatarInfo>
37+
<Nickname>{comment.author.nickname}</Nickname>
38+
<When>
39+
评论于:
40+
<TimeAgo datetime={comment.updatedAt} locale="zh_CN" />
41+
</When>
42+
</AvatarInfo>
43+
</CommentHeader>
44+
<CommentBody>
45+
<MarkDownRender body={comment.body} />
46+
</CommentBody>
47+
</CommentBox>
48+
</CommentBlock>
49+
<CommentDivider />
50+
</div>
51+
))}
52+
</Wrapper>
53+
)
54+
}
55+
case TYPE.RESULT_EMPTY: {
56+
return (
57+
<React.Fragment>
58+
<EmptyLabel text={`未找到评论的${Trans(thread)}信息`} size="large" />
59+
</React.Fragment>
60+
)
61+
}
62+
default: {
63+
return <CommentLoading />
64+
}
65+
}
66+
}
67+
68+
export default CommentsToContent
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react'
2+
3+
import { Popover } from '../../components'
4+
5+
import { Wrapper, Logo, PopoverInfo } from './styles/community_list'
6+
import { uid } from '../../utils'
7+
8+
const CommunityList = ({ items }) => (
9+
<Wrapper>
10+
{items.map(community => (
11+
<Popover
12+
key={uid.gen()}
13+
placement="bottom"
14+
trigger="hover"
15+
content={<PopoverInfo>{community.title}</PopoverInfo>}
16+
>
17+
<div>
18+
<Logo src={community.logo} />
19+
</div>
20+
</Popover>
21+
))}
22+
</Wrapper>
23+
)
24+
25+
export default CommunityList
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react'
2+
3+
import { Wrapper, Title } from './styles/parent'
4+
import CommunityList from './CommunityList'
5+
6+
import { THREAD } from '../../utils'
7+
8+
const Parent = ({ thread, data }) => {
9+
switch (thread) {
10+
case THREAD.JOB: {
11+
return (
12+
<Wrapper>
13+
<Title>{data.job.title}</Title>
14+
<CommunityList items={data.job.communities} />
15+
</Wrapper>
16+
)
17+
}
18+
case THREAD.REPO: {
19+
return (
20+
<Wrapper>
21+
<Title>{data.repo.title}</Title>
22+
<CommunityList items={data.repo.communities} />
23+
</Wrapper>
24+
)
25+
}
26+
case THREAD.VIDEO: {
27+
return (
28+
<Wrapper>
29+
<Title>{data.video.title}</Title>
30+
<CommunityList items={data.video.communities} />
31+
</Wrapper>
32+
)
33+
}
34+
default: {
35+
return (
36+
<Wrapper>
37+
<Title>{data.post.title}</Title>
38+
<CommunityList items={data.post.communities} />
39+
</Wrapper>
40+
)
41+
}
42+
}
43+
}
44+
45+
export default Parent
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
*
3+
* UserPublishedComments
4+
*
5+
*/
6+
7+
import React from 'react'
8+
import { inject, observer } from 'mobx-react'
9+
10+
import { ThreadSelector } from '../../components'
11+
import { ThreadWrapper } from './styles'
12+
13+
import CommentsToContent from './CommentsToContent'
14+
15+
import { makeDebugger, storePlug } from '../../utils'
16+
17+
import * as logic from './logic'
18+
/* eslint-disable no-unused-vars */
19+
const debug = makeDebugger('C:UserPublishedComments')
20+
/* eslint-enable no-unused-vars */
21+
22+
class UserPublishedCommentsContainer extends React.Component {
23+
componentWillMount() {
24+
const { userPublishedComments } = this.props
25+
logic.init(userPublishedComments)
26+
}
27+
28+
render() {
29+
const { userPublishedComments } = this.props
30+
const { curThread, curView, pagedCommentsData } = userPublishedComments
31+
32+
const { totalCount } = pagedCommentsData
33+
// debug('pagedCommentsData-->: ', pagedCommentsData)
34+
35+
return (
36+
<div>
37+
<ThreadWrapper>
38+
<ThreadSelector
39+
active={curThread}
40+
onSelect={logic.onThreadChange}
41+
totalCount={totalCount}
42+
lookLike="box"
43+
/>
44+
</ThreadWrapper>
45+
<CommentsToContent
46+
thread={curThread}
47+
curView={curView}
48+
data={pagedCommentsData}
49+
/>
50+
</div>
51+
)
52+
}
53+
}
54+
55+
export default inject(storePlug('userPublishedComments'))(
56+
observer(UserPublishedCommentsContainer)
57+
)
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// import R from 'ramda'
2+
3+
import {
4+
makeDebugger,
5+
$solver,
6+
asyncRes,
7+
asyncErr,
8+
TYPE,
9+
THREAD,
10+
ERR,
11+
pagedFilter,
12+
} from '../../utils'
13+
14+
import SR71 from '../../utils/network/sr71'
15+
import S from './schema'
16+
17+
const sr71$ = new SR71()
18+
let sub$ = null
19+
20+
/* eslint-disable no-unused-vars */
21+
const debug = makeDebugger('L:UserPublishedComments')
22+
/* eslint-enable no-unused-vars */
23+
24+
let store = null
25+
26+
const beforeQuery = page => {
27+
store.markState({ curView: TYPE.LOADING })
28+
// args
29+
return {
30+
userId: store.viewingUser.id,
31+
filter: pagedFilter(page),
32+
}
33+
}
34+
35+
export function loadPostComments(page = 1) {
36+
const args = beforeQuery(page)
37+
sr71$.query(S.publishedPostComments, args)
38+
}
39+
40+
export function loadJobComments(page = 1) {
41+
const args = beforeQuery(page)
42+
sr71$.query(S.publishedJobComments, args)
43+
}
44+
45+
export function loadVideoComments(page = 1) {
46+
const args = beforeQuery(page)
47+
sr71$.query(S.publishedVideoComments, args)
48+
}
49+
50+
export function loadRepoComments(page = 1) {
51+
const args = beforeQuery(page)
52+
sr71$.query(S.publishedRepoComments, args)
53+
}
54+
55+
export function onThreadChange(curThread) {
56+
// TODO: markRoute
57+
store.markState({ curThread })
58+
// reload()
59+
switch (store.curThread) {
60+
case THREAD.JOB: {
61+
return loadJobComments()
62+
}
63+
case THREAD.VIDEO: {
64+
return loadVideoComments()
65+
}
66+
case THREAD.REPO: {
67+
return loadRepoComments()
68+
}
69+
default: {
70+
return loadPostComments()
71+
}
72+
}
73+
}
74+
75+
// ###############################
76+
// Data & Error handlers
77+
// ###############################
78+
79+
const DataSolver = [
80+
{
81+
match: asyncRes('publishedPostComments'),
82+
action: ({ publishedPostComments }) =>
83+
store.markPagedData(publishedPostComments),
84+
},
85+
{
86+
match: asyncRes('publishedJobComments'),
87+
action: ({ publishedJobComments }) =>
88+
store.markPagedData(publishedJobComments),
89+
},
90+
{
91+
match: asyncRes('publishedVideoComments'),
92+
action: ({ publishedVideoComments }) =>
93+
store.markPagedData(publishedVideoComments),
94+
},
95+
{
96+
match: asyncRes('publishedRepoComments'),
97+
action: ({ publishedRepoComments }) => {
98+
debug('get publishedRepoComments: ', publishedRepoComments)
99+
store.markPagedData(publishedRepoComments)
100+
},
101+
},
102+
]
103+
104+
const ErrSolver = [
105+
{
106+
match: asyncErr(ERR.CRAPHQL),
107+
action: ({ details }) => {
108+
debug('ERR.CRAPHQL -->', details)
109+
},
110+
},
111+
{
112+
match: asyncErr(ERR.TIMEOUT),
113+
action: ({ details }) => {
114+
debug('ERR.TIMEOUT -->', details)
115+
},
116+
},
117+
{
118+
match: asyncErr(ERR.NETWORK),
119+
action: ({ details }) => {
120+
debug('ERR.NETWORK -->', details)
121+
},
122+
},
123+
]
124+
125+
export function init(_store) {
126+
if (store) {
127+
return loadPostComments()
128+
}
129+
store = _store
130+
131+
debug(store)
132+
if (sub$) sub$.unsubscribe()
133+
sub$ = sr71$.data().subscribe($solver(DataSolver, ErrSolver))
134+
135+
loadPostComments()
136+
}

0 commit comments

Comments
 (0)