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

Commit 51d15b6

Browse files
committed
chore: merge branch 'router' into dev
2 parents aa01021 + bcec01f commit 51d15b6

File tree

15 files changed

+191
-111
lines changed

15 files changed

+191
-111
lines changed

containers/Route/logic.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect } from 'react'
2-
import { Global, buildLog, getMainPath, getSubPath } from '@utils'
2+
import { Global, buildLog, parseURL } from '@utils'
33

44
/* eslint-disable-next-line */
55
const log = buildLog('L:Route')
@@ -20,8 +20,7 @@ export const init = (_store, routeObj) => {
2020

2121
store = _store
2222
// sync init router info
23-
const mainPath = getMainPath(routeObj)
24-
const subPath = getSubPath(routeObj)
23+
const { mainPath, subPath } = parseURL(routeObj)
2524
const { query } = routeObj
2625

2726
store.markState({ mainPath, subPath, query })
@@ -35,8 +34,7 @@ export const useInit = (_store, routeObj) => {
3534
useEffect(() => {
3635
store = _store
3736
// sync init router info
38-
const mainPath = getMainPath(routeObj)
39-
const subPath = getSubPath(routeObj)
37+
const { mainPath, subPath } = parseURL(routeObj)
4038
const { query } = routeObj
4139

4240
store.markState({ mainPath, subPath, query })

containers/Route/store.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import R from 'ramda'
88
// import Router from 'next/router'
99

1010
import { PAGE_SIZE } from '@config'
11-
import { Global, onClient, markStates, buildLog, serializeQuery } from '@utils'
11+
import {
12+
Global,
13+
isClientSide,
14+
markStates,
15+
buildLog,
16+
serializeQuery,
17+
} from '@utils'
1218

1319
/* eslint-disable-next-line */
1420
const log = buildLog('S:RouteStore')
@@ -23,6 +29,8 @@ const Query = t.model('Query', {
2329

2430
const RouteStore = t
2531
.model('RouteStore', {
32+
communityPath: t.optional(t.string, ''),
33+
threadPath: t.optional(t.string, ''),
2634
mainPath: t.optional(t.string, ''),
2735
subPath: t.optional(t.string, ''),
2836
query: t.optional(Query, {}),
@@ -32,13 +40,15 @@ const RouteStore = t
3240
return getParent(self)
3341
},
3442
get curRoute() {
35-
const { mainPath, subPath } = self
36-
return { mainPath, subPath }
43+
const { communityPath, threadPath, mainPath, subPath } = self
44+
return { communityPath, threadPath, mainPath, subPath }
3745
},
3846
}))
3947
.actions(self => ({
48+
// TODO: if current url is subdomain, then we should
49+
// reload to that page directly
4050
markRoute(query) {
41-
if (!onClient) return false
51+
if (!isClientSide) return false
4252
const { mainPath, subPath, page } = query
4353
query = R.pickBy(v => !R.isEmpty(v), query)
4454

pages/communities.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import R from 'ramda'
77

88
import { ROUTE } from '@constant'
99
import {
10+
parseURL,
1011
getJwtToken,
1112
makeGQClient,
1213
queryStringToJSON,
1314
nilOrEmpty,
14-
getSubPath,
1515
ssrAmbulance,
1616
parseTheme,
1717
} from '@utils'
@@ -45,7 +45,7 @@ async function fetchData(props, opt) {
4545
const token = realname ? getJwtToken(props) : null
4646
const gqClient = makeGQClient(token)
4747
const userHasLogin = nilOrEmpty(token) === false
48-
const subPath = getSubPath(props)
48+
const { subPath } = parseURL(props)
4949
const category = subPath !== '' ? subPath : 'pl'
5050

5151
const { asPath } = props

pages/community.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,12 @@ import NextSeo from 'next-seo'
66
import { PAGE_SIZE, SITE_URL } from '@config'
77
import initRootStore from '@stores/init'
88

9-
import AnalysisService from '@services/Analysis'
10-
import GlobalLayout from '@containers/GlobalLayout'
11-
import ThemeWrapper from '@containers/ThemeWrapper'
12-
import MultiLanguage from '@containers/MultiLanguage'
13-
import Sidebar from '@containers/Sidebar'
14-
import Preview from '@containers/Preview'
15-
import Doraemon from '@containers/Doraemon'
16-
import Route from '@containers/Route'
17-
import Header from '@containers/Header'
18-
import CommunityBanner from '@containers/CommunityBanner'
19-
import CommunityContent from '@containers/CommunityContent'
20-
import Footer from '@containers/Footer'
21-
import ErrorBox from '@containers/ErrorBox'
22-
import ErrorPage from '@components/ErrorPage'
23-
249
import {
10+
isServerSide,
2511
getJwtToken,
2612
makeGQClient,
2713
queryStringToJSON,
28-
getMainPath,
29-
getSubPath,
14+
parseURL,
3015
akaTranslate,
3116
extractThreadFromPath,
3217
buildLog,
@@ -40,6 +25,21 @@ import {
4025
parseTheme,
4126
} from '@utils'
4227

28+
import AnalysisService from '@services/Analysis'
29+
import GlobalLayout from '@containers/GlobalLayout'
30+
import ThemeWrapper from '@containers/ThemeWrapper'
31+
import MultiLanguage from '@containers/MultiLanguage'
32+
import Sidebar from '@containers/Sidebar'
33+
import Preview from '@containers/Preview'
34+
import Doraemon from '@containers/Doraemon'
35+
import Route from '@containers/Route'
36+
import Header from '@containers/Header'
37+
import CommunityBanner from '@containers/CommunityBanner'
38+
import CommunityContent from '@containers/CommunityContent'
39+
import Footer from '@containers/Footer'
40+
import ErrorBox from '@containers/ErrorBox'
41+
import ErrorPage from '@components/ErrorPage'
42+
4343
import { P } from '@schemas'
4444

4545
/* eslint-disable-next-line */
@@ -59,10 +59,8 @@ async function fetchData(props, opt) {
5959
const { asPath } = props
6060
// schema
6161

62-
// utils: filter, tags staff
63-
const mainPath = getMainPath(props)
64-
const community = akaTranslate(mainPath)
65-
const topic = getSubPath(props)
62+
const { communityPath, threadPath: topic } = parseURL(props)
63+
const community = akaTranslate(communityPath)
6664
const thread = extractThreadFromPath(props)
6765

6866
let filter = addTopicIfNeed(
@@ -107,8 +105,9 @@ async function fetchData(props, opt) {
107105

108106
export default class CommunityPage extends React.Component {
109107
static async getInitialProps(props) {
110-
const mainPath = getMainPath(props)
111-
const subPath = getSubPath(props)
108+
if (!isServerSide) return {}
109+
110+
const { communityPath, threadPath } = parseURL(props)
112111
const thread = extractThreadFromPath(props)
113112

114113
let resp
@@ -120,7 +119,7 @@ export default class CommunityPage extends React.Component {
120119
} else {
121120
return {
122121
statusCode: 404,
123-
target: mainPath,
122+
target: communityPath,
124123
viewing: { community: {} },
125124
route: {},
126125
}
@@ -157,7 +156,12 @@ export default class CommunityPage extends React.Component {
157156
repo: {},
158157
user: {},
159158
},
160-
route: { mainPath: community.raw, subPath },
159+
route: {
160+
communityPath: community.raw,
161+
mainPath: community.raw,
162+
threadPath,
163+
subPath: threadPath,
164+
},
161165
tagsBar: { tags: partialTags },
162166
},
163167
contentsThread
@@ -180,7 +184,7 @@ export default class CommunityPage extends React.Component {
180184
viewing: { community },
181185
route,
182186
} = this.props
183-
const { mainPath, subPath } = route
187+
const { communityPath, threadPath } = route
184188

185189
const seoTitle =
186190
community.raw === 'home'
@@ -201,7 +205,7 @@ export default class CommunityPage extends React.Component {
201205
<React.Fragment>
202206
<NextSeo
203207
config={{
204-
url: `${SITE_URL}/${mainPath}/${subPath}`,
208+
url: `${SITE_URL}/${communityPath}/${threadPath}`,
205209
title: seoTitle,
206210
description: `${community.desc}`,
207211
}}

pages/home/posts.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import {
2525
getJwtToken,
2626
makeGQClient,
2727
queryStringToJSON,
28-
getMainPath,
29-
getSubPath,
28+
parseURL,
3029
akaTranslate,
3130
extractThreadFromPath,
3231
buildLog,
@@ -60,9 +59,8 @@ async function fetchData(props, opt) {
6059
// schema
6160

6261
// utils: filter, tags staff
63-
const mainPath = getMainPath(props)
64-
const community = akaTranslate(mainPath)
65-
const topic = getSubPath(props)
62+
const { communityPath, subPath: topic } = parseURL(props)
63+
const community = akaTranslate(communityPath)
6664
const thread = extractThreadFromPath(props)
6765

6866
let filter = addTopicIfNeed(
@@ -107,8 +105,7 @@ async function fetchData(props, opt) {
107105

108106
export default class HomePage extends React.Component {
109107
static async getInitialProps(props) {
110-
const mainPath = getMainPath(props)
111-
const subPath = getSubPath(props)
108+
const { communityPath, threadPath } = parseURL(props)
112109
const thread = extractThreadFromPath(props)
113110

114111
let resp
@@ -120,7 +117,7 @@ export default class HomePage extends React.Component {
120117
} else {
121118
return {
122119
statusCode: 404,
123-
target: mainPath,
120+
target: communityPath,
124121
viewing: { community: {} },
125122
route: {},
126123
}
@@ -157,7 +154,12 @@ export default class HomePage extends React.Component {
157154
repo: {},
158155
user: {},
159156
},
160-
route: { mainPath: community.raw, subPath },
157+
route: {
158+
communityPath: community.raw,
159+
mainPath: community.raw,
160+
threadPath,
161+
subPath: threadPath,
162+
},
161163
tagsBar: { tags: partialTags },
162164
},
163165
contentsThread
@@ -180,7 +182,7 @@ export default class HomePage extends React.Component {
180182
viewing: { community },
181183
route,
182184
} = this.props
183-
const { mainPath, subPath } = route
185+
const { communityPath, subPath } = route
184186

185187
const seoTitle =
186188
community.raw === 'home'
@@ -201,7 +203,7 @@ export default class HomePage extends React.Component {
201203
<React.Fragment>
202204
<NextSeo
203205
config={{
204-
url: `${SITE_URL}/${mainPath}/${subPath}`,
206+
url: `${SITE_URL}/${communityPath}/${subPath}`,
205207
title: seoTitle,
206208
description: `${community.desc}`,
207209
}}

pages/index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Provider } from 'mobx-react'
33
import R from 'ramda'
44

55
import { ROUTE } from '@constant'
6-
import { buildLog, getMainPath } from '@utils'
6+
import { buildLog, parseURL, isServerSide } from '@utils'
77
import AnalysisService from '@services/Analysis'
88

99
import GlobalLayout from '@containers/GlobalLayout'
@@ -37,16 +37,13 @@ global.Intl = require('intl')
3737
*/
3838
export default class PageCommunity extends React.Component {
3939
static async getInitialProps(props) {
40-
const isServer = typeof window === 'undefined'
41-
console.log('page:index isServer: ', isServer)
40+
if (!isServerSide) return {}
4241

43-
if (!isServer) return {}
42+
const { communityPath, threadPath } = parseURL(props)
4443

45-
const mainPath = getMainPath(props)
46-
const subPath = getMainPath(props)
4744
const hideSidebar =
48-
R.contains(mainPath, [ROUTE.USER]) ||
49-
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])
5047

5148
return {
5249
hideSidebar,

pages/job.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import { TYPE, ROUTE, THREAD } from '@constant'
88
import {
99
getJwtToken,
1010
makeGQClient,
11-
getMainPath,
12-
getSubPath,
13-
getThirdPath,
11+
parseURL,
1412
nilOrEmpty,
1513
ssrAmbulance,
1614
parseTheme,
@@ -45,7 +43,7 @@ async function fetchData(props) {
4543
const userHasLogin = nilOrEmpty(token) === false
4644

4745
// schema
48-
const id = getThirdPath(props)
46+
const { thridPath: id } = parseURL(props)
4947

5048
// query data
5149
const sessionState = gqClient.request(P.sessionState)
@@ -74,17 +72,18 @@ async function fetchData(props) {
7472
export default class JobPage extends React.Component {
7573
static async getInitialProps(props) {
7674
let resp
75+
const { communityPath, threadPath } = parseURL(props)
76+
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: getSubPath(props) }
83+
return { statusCode: 404, target: threadPath }
8484
}
8585
}
8686

87-
const mainPath = getMainPath(props)
8887
const { sessionState, pagedComments, subscribedCommunities, job } = resp
8988

9089
return {
@@ -97,7 +96,12 @@ export default class JobPage extends React.Component {
9796
isValidSession: sessionState.isValid,
9897
userSubscribedCommunities: subscribedCommunities,
9998
},
100-
route: { mainPath, subPath: ROUTE.JOB },
99+
route: {
100+
communityPath,
101+
mainPath: communityPath,
102+
threadPath: ROUTE.JOB,
103+
subPath: ROUTE.JOB,
104+
},
101105
viewing: {
102106
job,
103107
activeThread: THREAD.JOB,

0 commit comments

Comments
 (0)