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

Commit 5dd58f3

Browse files
committed
refactor(ssr): badic community page workflow
1 parent 6f604a1 commit 5dd58f3

File tree

9 files changed

+171
-17
lines changed

9 files changed

+171
-17
lines changed

containers/CommunitiesContent/styles/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import styled from 'styled-components'
33
import { Img } from '../../../components'
44
import { theme } from '../../../utils'
55

6-
export const Wrapper = styled.div``
7-
6+
export const Wrapper = styled.div`
7+
min-height: 800px;
8+
`
89
export const CommunityIcon = styled(Img)`
910
fill: ${theme('banner.title')};
1011
width: 30px;

containers/CommunityContent/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const renderChildContent = (route, store, restProps) => {
2626
// return <IndexContent data={pagedCommunitiesData} restProps={restProps} />
2727
// return <PostsContent data={pagedPostsData} restProps={restProps} />
2828

29+
debug('current fucking: ', pagedPostsData)
30+
2931
switch (route.subPath) {
3032
case ROUTE.TAGS: {
3133
return <TagsContent data={pagedTagsData} restProps={restProps} />

containers/CommunityContent/store.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,33 @@
66
import { types as t, getParent } from 'mobx-state-tree'
77
// import R from 'ramda'
88

9-
import { PagedPosts, PagedTags } from '../../stores/SharedModel'
9+
import {
10+
PagedPosts,
11+
PagedJobs,
12+
PagedRepos,
13+
PagedVideos,
14+
PagedTags,
15+
PagedThreads,
16+
PagedCategories,
17+
emptyPagiData,
18+
} from '../../stores/SharedModel'
19+
1020
import { markStates, makeDebugger, stripMobx } from '../../utils'
1121
/* eslint-disable no-unused-vars */
1222
const debug = makeDebugger('S:CommunityContentStore')
1323
/* eslint-enable no-unused-vars */
1424

1525
const CommunityContentStore = t
1626
.model('CommunityContentStore', {
17-
pagedPosts: t.maybeNull(PagedPosts),
18-
pagedTags: t.maybeNull(PagedTags),
27+
pagedPosts: t.optional(PagedPosts, emptyPagiData),
28+
pagedJobs: t.optional(PagedJobs, emptyPagiData),
29+
pagedRepos: t.optional(PagedRepos, emptyPagiData),
30+
pagedVideos: t.optional(PagedVideos, emptyPagiData),
31+
32+
pagedCategories: t.optional(PagedCategories, emptyPagiData),
33+
pagedTags: t.optional(PagedTags, emptyPagiData),
34+
pagedThreads: t.optional(PagedThreads, emptyPagiData),
35+
1936
postsLoading: t.optional(t.boolean, false),
2037
tagsLoading: t.optional(t.boolean, false),
2138
})

containers/CommunityContent/styles/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,24 @@ export const CommunityIcon = styled(Img)`
66
width: 30px;
77
height: 30px;
88
`
9-
export const Wrapper = styled.div``
10-
9+
export const Wrapper = styled.div`
10+
min-height: 800px;
11+
`
1112
export const OperationWrapper = styled.div`
1213
display: flex;
1314
justify-content: center;
1415
`
15-
1616
export const ColorCell = styled.div`
1717
display: flex;
1818
align-items: center;
1919
justify-content: center;
2020
`
21-
2221
export const ColorDot = styled.div`
2322
width: 10px;
2423
height: 10px;
2524
background: ${props => props.color};
2625
border-radius: 100%;
2726
`
28-
2927
export const ColorTitle = styled.div`
3028
margin-left: 5px;
3129
`

containers/UsersContent/styles/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import styled from 'styled-components'
22

3-
export const Wrapper = styled.div``
4-
3+
export const Wrapper = styled.div`
4+
min-height: 800px;
5+
`
56
export const OperationWrapper = styled.div`
67
display: flex;
78
justify-content: center;

pages/community.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import React from 'react'
2+
import { Provider } from 'mobx-react'
3+
import R from 'ramda'
4+
5+
import GAWraper from '../components/GAWraper'
6+
import initRootStore from '../stores/init'
7+
import ThemeWrapper from '../containers/ThemeWrapper'
8+
import MultiLanguage from '../containers/MultiLanguage'
9+
import Sidebar from '../containers/Sidebar'
10+
import Preview from '../containers/Preview'
11+
import Doraemon from '../containers/Doraemon'
12+
import Route from '../containers/Route'
13+
import BodyLayout from '../containers/BodyLayout'
14+
import Header from '../containers/Header'
15+
import CommunityBanner from '../containers/CommunityBanner'
16+
import CommunityContent from '../containers/CommunityContent'
17+
18+
import {
19+
makeGQClient,
20+
getMainPath,
21+
getSubPath,
22+
BStore,
23+
ssrPagedSchema,
24+
ssrPagedContents,
25+
} from '../utils'
26+
import Footer from '../components/Footer'
27+
28+
// try to fix safari bug
29+
// see https://github.com/yahoo/react-intl/issues/422
30+
global.Intl = require('intl')
31+
32+
/*
33+
this function is only needed in dev-mode, it's a bug
34+
in production, the query for sub-path will go direactly to sub-route file
35+
*/
36+
async function fetchData(props) {
37+
const token = BStore.cookie.from_req(props.req, 'jwtToken')
38+
const gqClient = makeGQClient(token)
39+
40+
const subpath = getSubPath(props)
41+
console.log('subpath --> ', subpath)
42+
43+
// const sessionState = gqClient.request(P.sessionState)
44+
const pagedContents = gqClient.request(ssrPagedSchema(subpath), {
45+
filter: { page: 1, size: 30 },
46+
})
47+
48+
return {
49+
...(await pagedContents),
50+
}
51+
}
52+
53+
/* filter: mergeRouteQuery(query), */
54+
export default class Index extends React.Component {
55+
static async getInitialProps(props) {
56+
console.log('## community ## index page ..')
57+
58+
const subPath = getSubPath(props)
59+
const mainPath = getMainPath(props)
60+
61+
let resp
62+
try {
63+
resp = await fetchData(props)
64+
} catch ({ response: { errors } }) {
65+
/*
66+
if (ssrAmbulance.hasLoginError(errors)) {
67+
resp = await fetchData(props, { realname: false })
68+
} else {
69+
return { statusCode: 404, target: subPath }
70+
}
71+
*/
72+
return { statusCode: 404, target: subPath }
73+
}
74+
75+
// const { pagedCommunities } = resp
76+
const pagedContents = ssrPagedContents(mainPath, subPath, resp)
77+
78+
return R.merge({ route: { mainPath, subPath } }, pagedContents)
79+
}
80+
81+
constructor(props) {
82+
super(props)
83+
this.store = initRootStore({ ...props })
84+
}
85+
86+
render() {
87+
return (
88+
<Provider store={this.store}>
89+
<GAWraper>
90+
<ThemeWrapper>
91+
<Route />
92+
<MultiLanguage>
93+
<Sidebar />
94+
<Preview />
95+
<Doraemon />
96+
<BodyLayout>
97+
<Header />
98+
<CommunityBanner />
99+
<CommunityContent />
100+
<Footer />
101+
</BodyLayout>
102+
</MultiLanguage>
103+
</ThemeWrapper>
104+
</GAWraper>
105+
</Provider>
106+
)
107+
}
108+
}

server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ app.prepare().then(() => {
4646
return renderAndCache(req, res, '/users', req.query)
4747
})
4848

49-
server.get('/:main/:sub?', (req, res) => {
49+
server.get('/:community/:thread', (req, res) => {
5050
// console.log('match me user')
5151
// return app.render(req, res, '/user', req.query)
52-
return renderAndCache(req, res, '/', req.query)
52+
return renderAndCache(req, res, '/community', req.query)
5353
})
5454

5555
server.get('*', (req, res) => handle(req, res))

utils/constants.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,11 @@ export const ROUTE = {
139139
// id is used for UI when item is active
140140
// communities CURD
141141
COMMUNITIES: 'communities',
142+
COMMUNITY: 'community',
142143
// communities categories CURD
143-
CATEGORIES: 'categories',
144144
TAGS: 'tags',
145145
THREADS: 'threads',
146146

147-
COMMUNITY: 'community',
148-
149147
USERS: 'users',
150148
SENIOR: 'senior',
151149
// valid part

utils/ssr_helper.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@ export const ssrPagedSchema = subpath => {
2929
}
3030

3131
export const ssrPagedContents = (mainPath, subPath, resp) => {
32+
switch (mainPath) {
33+
case ROUTE.COMMUNITIES: {
34+
return ssrCommunitiesContents(subPath, resp)
35+
}
36+
37+
default: {
38+
return ssrCommunityContents(subPath, resp)
39+
}
40+
}
41+
}
42+
43+
// communityContent
44+
const ssrCommunityContents = (subPath, resp) => {
45+
switch (subPath) {
46+
case ROUTE.POSTS: {
47+
return {
48+
communityContent: { pagedPosts: resp.pagedPosts },
49+
}
50+
}
51+
52+
default: {
53+
return {
54+
communityContent: { pagedPosts: resp.pagedPosts },
55+
}
56+
}
57+
}
58+
}
59+
60+
const ssrCommunitiesContents = (subPath, resp) => {
3261
switch (subPath) {
3362
case ROUTE.CATEGORIES: {
3463
return {

0 commit comments

Comments
 (0)