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

Commit cd9b386

Browse files
committed
refactor(SSR): basic workflow with helpers
1 parent 8ad7cc2 commit cd9b386

File tree

7 files changed

+129
-41
lines changed

7 files changed

+129
-41
lines changed

containers/CommunitiesBanner/index.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,25 @@ import { BannerContainer } from './styles'
2020
const debug = makeDebugger('C:CommunitiesBanner')
2121
/* eslint-enable no-unused-vars */
2222

23-
const ChildBanner = ({ curRoute, totalCount, restProps }) => {
23+
const ChildBanner = ({
24+
curRoute,
25+
totalCount,
26+
tagsTotalCount,
27+
categoriesTotalCount,
28+
threadsTotalCount,
29+
postsTotalCount,
30+
restProps,
31+
}) => {
2432
const {
2533
// communities
2634
filteredTotalCount,
2735
// tags
28-
tagsTotalCount,
2936
filterdTagsCount,
3037
// threads
31-
threadsTotalCount,
3238
filterdThreadsCount,
3339
// categories
34-
categoriesTotalCount,
3540
filterdCategoriesCount,
3641
// posts
37-
postsTotalCount,
3842
filteredPostsCount,
3943
// jobs
4044
jobsTotalCount,
@@ -109,16 +113,24 @@ class CommunitiesBannerContainer extends React.Component {
109113

110114
render() {
111115
const { communitiesBanner } = this.props
112-
const { curRoute, totalCount } = communitiesBanner
113-
114-
// console.log('totalCount --> ', communitiesBanner.totalCount)
115-
console.log('the fucking totalCount: ', totalCount)
116+
const {
117+
curRoute,
118+
totalCount,
119+
tagsTotalCount,
120+
categoriesTotalCount,
121+
threadsTotalCount,
122+
postsTotalCount,
123+
} = communitiesBanner
116124

117125
return (
118126
<BannerContainer>
119127
<ChildBanner
120128
curRoute={curRoute}
121129
totalCount={totalCount}
130+
categoriesTotalCount={categoriesTotalCount}
131+
threadsTotalCount={threadsTotalCount}
132+
tagsTotalCount={tagsTotalCount}
133+
postsTotalCount={postsTotalCount}
122134
restProps={stripMobx(communitiesBanner)}
123135
/>
124136
</BannerContainer>

containers/CommunitiesBanner/store.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@ const CommunitiesBannerStore = t
1717
// totalCount: t.optional(t.number, 0),
1818
filteredTotalCount: t.maybeNull(t.number),
1919
// categories
20-
categoriesTotalCount: t.optional(t.number, 0),
2120
filterdCategoriesCount: t.maybeNull(t.number),
2221
// tags
23-
tagsTotalCount: t.optional(t.number, 0),
2422
filterdTagsCount: t.maybeNull(t.number),
2523
// threads
26-
threadsTotalCount: t.optional(t.number, 0),
2724
filterdThreadsCount: t.maybeNull(t.number),
2825
// posts
29-
postsTotalCount: t.optional(t.number, 0),
26+
// postsTotalCount: t.optional(t.number, 0),
3027
filteredPostsCount: t.maybeNull(t.number),
3128
// jobs
3229
jobsTotalCount: t.optional(t.number, 0),
@@ -39,6 +36,18 @@ const CommunitiesBannerStore = t
3936
get totalCount() {
4037
return self.root.communitiesContent.pagedCommunities.totalCount
4138
},
39+
get categoriesTotalCount() {
40+
return self.root.communitiesContent.pagedCategories.totalCount
41+
},
42+
get tagsTotalCount() {
43+
return self.root.communitiesContent.pagedTags.totalCount
44+
},
45+
get threadsTotalCount() {
46+
return self.root.communitiesContent.pagedThreads.totalCount
47+
},
48+
get postsTotalCount() {
49+
return self.root.communitiesContent.pagedPosts.totalCount
50+
},
4251
get curRoute() {
4352
return self.root.curRoute
4453
},

containers/CommunitiesContent/schema.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
import gql from 'graphql-tag'
2-
import { P, F } from '../schemas'
2+
import { P } from '../schemas'
33

44
const pagedCommunities = gql`
55
${P.pagedCommunities}
66
`
77
const pagedCategories = gql`
8-
query($filter: PagedFilter!) {
9-
pagedCategories(filter: $filter) {
10-
entries {
11-
id
12-
title
13-
raw
14-
index
15-
communities {
16-
id
17-
logo
18-
title
19-
}
20-
author {
21-
${F.author}
22-
}
23-
insertedAt
24-
updatedAt
25-
}
26-
${F.pagedCounts}
27-
}
28-
}
8+
${P.pagedCategories}
299
`
3010
const pagedTags = gql`
3111
${P.pagedTags}

containers/schemas/pages/misc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ export const pagedCategories = `
1616
title
1717
raw
1818
index
19+
communities {
20+
${F.community}
21+
}
22+
author {
23+
${F.author}
24+
}
25+
insertedAt
26+
updatedAt
1927
}
2028
${F.pagedCounts}
2129
}

pages/communities.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import { Provider } from 'mobx-react'
3+
import R from 'ramda'
34

45
import GAWraper from '../components/GAWraper'
56
import initRootStore from '../stores/init'
@@ -14,8 +15,6 @@ import Header from '../containers/Header'
1415
import CommunitiesBanner from '../containers/CommunitiesBanner'
1516
import CommunitiesContent from '../containers/CommunitiesContent'
1617

17-
import { P } from '../containers/schemas'
18-
1918
import {
2019
makeGQClient,
2120
// Global,
@@ -24,6 +23,8 @@ import {
2423
getMainPath,
2524
getSubPath,
2625
BStore,
26+
ssrPagedSchema,
27+
ssrPagedContents,
2728
} from '../utils'
2829
import Footer from '../components/Footer'
2930

@@ -45,12 +46,12 @@ async function fetchData(props) {
4546
const subpath = getSubPath(props)
4647
console.log('subpath --> ', subpath)
4748

48-
const pagedCommunities = gqClient.request(P.pagedCommunities, {
49+
const pagedContents = gqClient.request(ssrPagedSchema(subpath), {
4950
filter: { page: 1, size: 30 },
5051
})
5152

5253
return {
53-
...(await pagedCommunities),
54+
...(await pagedContents),
5455
}
5556
}
5657

@@ -64,13 +65,37 @@ export default class Index extends React.Component {
6465
const subPath = getSubPath(props)
6566
const mainPath = getMainPath(props)
6667

67-
const { pagedCommunities } = await fetchData(props)
68+
let resp
69+
try {
70+
resp = await fetchData(props)
71+
} catch ({ response: { errors } }) {
72+
/*
73+
if (ssrAmbulance.hasLoginError(errors)) {
74+
resp = await fetchData(props, { realname: false })
75+
} else {
76+
return { statusCode: 404, target: subPath }
77+
}
78+
*/
79+
return { statusCode: 404, target: subPath }
80+
}
81+
82+
const { pagedCommunities } = resp
83+
const pagedContents = ssrPagedContents(mainPath, subPath, resp)
6884

85+
return R.merge(
86+
{
87+
route: { mainPath, subPath },
88+
communities: pagedCommunities,
89+
},
90+
pagedContents
91+
)
92+
/*
6993
return {
7094
route: { mainPath, subPath },
7195
communities: pagedCommunities,
7296
communitiesContent: { pagedCommunities },
7397
}
98+
*/
7499
}
75100

76101
constructor(props) {

utils/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ export {
104104
} from './themes'
105105

106106
export { default as fakeUsers } from './fake_user'
107-
108107
export { default as SOCIAL_LISTS } from './social_lists'
109108

109+
export { ssrPagedSchema, ssrPagedContents } from './ssr_helper'
110+
110111
// helpers
111112
export { default as cs } from './common_styles'
112113
export { default as animate } from './animations'

utils/ssr_helper.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// import R from 'ramda'
2+
import { P } from '../containers/schemas'
3+
import { ROUTE } from './constants'
4+
5+
export const ssrPagedSchema = subpath => {
6+
switch (subpath) {
7+
case ROUTE.CATEGORIES: {
8+
return P.pagedCategories
9+
}
10+
case ROUTE.TAGS: {
11+
return P.pagedTags
12+
}
13+
case ROUTE.THREADS: {
14+
return P.pagedThreads
15+
}
16+
case ROUTE.POSTS: {
17+
return P.pagedPosts
18+
}
19+
default: {
20+
return P.pagedCommunities
21+
}
22+
}
23+
}
24+
25+
export const ssrPagedContents = (mainPath, subPath, resp) => {
26+
switch (subPath) {
27+
case ROUTE.CATEGORIES: {
28+
return {
29+
communitiesContent: { pagedCategories: resp.pagedCategories },
30+
}
31+
}
32+
case ROUTE.TAGS: {
33+
return {
34+
communitiesContent: { pagedTags: resp.pagedTags },
35+
}
36+
}
37+
case ROUTE.THREADS: {
38+
return {
39+
communitiesContent: { pagedThreads: resp.pagedThreads },
40+
}
41+
}
42+
case ROUTE.POSTS: {
43+
return {
44+
communitiesContent: { pagedPosts: resp.pagedPosts },
45+
}
46+
}
47+
default: {
48+
return {
49+
communitiesContent: { pagedCommunities: resp.pagedCommunities },
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)