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

Commit 9eada2e

Browse files
committed
refactor(router): add communityPath & threadPath concept
1 parent 3fd64d3 commit 9eada2e

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

containers/Route/store.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const Query = t.model('Query', {
2323

2424
const RouteStore = t
2525
.model('RouteStore', {
26+
communityPath: t.optional(t.string, ''),
27+
threadPath: t.optional(t.string, ''),
2628
mainPath: t.optional(t.string, ''),
2729
subPath: t.optional(t.string, ''),
2830
query: t.optional(Query, {}),
@@ -32,11 +34,13 @@ const RouteStore = t
3234
return getParent(self)
3335
},
3436
get curRoute() {
35-
const { mainPath, subPath } = self
36-
return { mainPath, subPath }
37+
const { communityPath, threadPath, mainPath, subPath } = self
38+
return { communityPath, threadPath, mainPath, subPath }
3739
},
3840
}))
3941
.actions(self => ({
42+
// TODO: if current url is subdomain, then we should
43+
// reload to that page directly
4044
markRoute(query) {
4145
if (!onClient) return false
4246
const { mainPath, subPath, page } = query

pages/home/posts.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ async function fetchData(props, opt) {
5959
// schema
6060

6161
// utils: filter, tags staff
62-
const { mainPath, subPath: topic } = parseURL(props)
63-
const community = akaTranslate(mainPath)
62+
const { communityPath, subPath: topic } = parseURL(props)
63+
const community = akaTranslate(communityPath)
6464
const thread = extractThreadFromPath(props)
6565

6666
let filter = addTopicIfNeed(
@@ -105,7 +105,7 @@ async function fetchData(props, opt) {
105105

106106
export default class HomePage extends React.Component {
107107
static async getInitialProps(props) {
108-
const { mainPath, subPath } = parseURL(props)
108+
const { communityPath, threadPath } = parseURL(props)
109109
const thread = extractThreadFromPath(props)
110110

111111
let resp
@@ -117,7 +117,7 @@ export default class HomePage extends React.Component {
117117
} else {
118118
return {
119119
statusCode: 404,
120-
target: mainPath,
120+
target: communityPath,
121121
viewing: { community: {} },
122122
route: {},
123123
}
@@ -154,7 +154,12 @@ export default class HomePage extends React.Component {
154154
repo: {},
155155
user: {},
156156
},
157-
route: { mainPath: community.raw, subPath },
157+
route: {
158+
communityPath: community.raw,
159+
mainPath: community.raw,
160+
threadPath,
161+
subPath: threadPath,
162+
},
158163
tagsBar: { tags: partialTags },
159164
},
160165
contentsThread
@@ -177,7 +182,7 @@ export default class HomePage extends React.Component {
177182
viewing: { community },
178183
route,
179184
} = this.props
180-
const { mainPath, subPath } = route
185+
const { communityPath, subPath } = route
181186

182187
const seoTitle =
183188
community.raw === 'home'
@@ -198,7 +203,7 @@ export default class HomePage extends React.Component {
198203
<React.Fragment>
199204
<NextSeo
200205
config={{
201-
url: `${SITE_URL}/${mainPath}/${subPath}`,
206+
url: `${SITE_URL}/${communityPath}/${subPath}`,
202207
title: seoTitle,
203208
description: `${community.desc}`,
204209
}}

pages/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ global.Intl = require('intl')
3737
*/
3838
export default class PageCommunity extends React.Component {
3939
static async getInitialProps(props) {
40-
if (!isServerSide()) return {}
40+
if (!isServerSide) return {}
41+
42+
const { communityPath, threadPath } = parseURL(props)
4143

42-
const { mainPath, subPath } = parseURL(props)
4344
const hideSidebar =
44-
R.contains(mainPath, [ROUTE.USER]) ||
45-
R.contains(subPath, [ROUTE.POST, ROUTE.REPO, ROUTE.VIDEO, ROUTE.JOB])
45+
R.contains(communityPath, [ROUTE.USER]) ||
46+
R.contains(threadPath, [ROUTE.POST, ROUTE.REPO, ROUTE.VIDEO, ROUTE.JOB])
4647

4748
return {
4849
hideSidebar,

pages/job.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ async function fetchData(props) {
7272
export default class JobPage extends React.Component {
7373
static async getInitialProps(props) {
7474
let resp
75-
const { mainPath, subPath } = parseURL(props)
75+
const { communityPath, threadPath } = parseURL(props)
7676

7777
try {
7878
resp = await fetchData(props)
7979
} catch ({ response: { errors } }) {
8080
if (ssrAmbulance.hasLoginError(errors)) {
8181
resp = await fetchData(props, { realname: false })
8282
} else {
83-
return { statusCode: 404, target: subPath }
83+
return { statusCode: 404, target: threadPath }
8484
}
8585
}
8686

@@ -96,7 +96,12 @@ export default class JobPage extends React.Component {
9696
isValidSession: sessionState.isValid,
9797
userSubscribedCommunities: subscribedCommunities,
9898
},
99-
route: { mainPath, subPath: ROUTE.JOB },
99+
route: {
100+
communityPath,
101+
mainPath: communityPath,
102+
threadPath: ROUTE.JOB,
103+
subPath: ROUTE.JOB,
104+
},
100105
viewing: {
101106
job,
102107
activeThread: THREAD.JOB,

pages/video.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async function fetchData(props) {
6868

6969
export default class VideoPage extends React.Component {
7070
static async getInitialProps(props) {
71-
const { mainPath, subPath } = parseURL(props)
71+
const { communityPath, subPath } = parseURL(props)
7272
let resp
7373
try {
7474
resp = await fetchData(props)
@@ -82,7 +82,7 @@ export default class VideoPage extends React.Component {
8282

8383
const { sessionState, video, pagedComments, subscribedCommunities } = resp
8484

85-
if (!R.contains(mainPath, R.pluck('raw', video.communities))) {
85+
if (!R.contains(communityPath, R.pluck('raw', video.communities))) {
8686
return { statusCode: 404, target: subPath }
8787
}
8888

@@ -96,7 +96,7 @@ export default class VideoPage extends React.Component {
9696
isValidSession: sessionState.isValid,
9797
userSubscribedCommunities: subscribedCommunities,
9898
},
99-
route: { mainPath, subPath: ROUTE.VIDEO },
99+
route: { communityPath, mainPath: communityPath, subPath: ROUTE.VIDEO },
100100
viewing: {
101101
video,
102102
activeThread: THREAD.VIDEO,
@@ -121,7 +121,7 @@ export default class VideoPage extends React.Component {
121121
viewing: { video },
122122
route,
123123
} = this.props
124-
const { mainPath } = route
124+
const { communityPath } = route
125125

126126
return (
127127
<Provider store={this.store}>
@@ -132,7 +132,7 @@ export default class VideoPage extends React.Component {
132132
) : (
133133
<React.Fragment>
134134
<BlogJsonLd
135-
url={`${SITE_URL}/${mainPath}/video/${video.id}`}
135+
url={`${SITE_URL}/${communityPath}/video/${video.id}`}
136136
title={`${video.title}`}
137137
datePublished={`${video.insertedAt}`}
138138
dateModified={`${video.updatedAt}`}

0 commit comments

Comments
 (0)