Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit8643688

Browse files
committed
refactor(router): use parseURL instead of parse one-by-one
rm old getMainPath, getSubPath, getThridPath
1 parentaa01021 commit8643688

File tree

11 files changed

+135
-84
lines changed

11 files changed

+135
-84
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
constlog=buildLog('L:Route')
@@ -20,8 +20,7 @@ export const init = (_store, routeObj) => {
2020

2121
store=_store
2222
// sync init router info
23-
constmainPath=getMainPath(routeObj)
24-
constsubPath=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-
constmainPath=getMainPath(routeObj)
39-
constsubPath=getSubPath(routeObj)
37+
const{ mainPath, subPath}=parseURL(routeObj)
4038
const{ query}=routeObj
4139

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

‎pages/community.js‎

Lines changed: 21 additions & 22 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
importinitRootStorefrom'@stores/init'
88

9-
importAnalysisServicefrom'@services/Analysis'
10-
importGlobalLayoutfrom'@containers/GlobalLayout'
11-
importThemeWrapperfrom'@containers/ThemeWrapper'
12-
importMultiLanguagefrom'@containers/MultiLanguage'
13-
importSidebarfrom'@containers/Sidebar'
14-
importPreviewfrom'@containers/Preview'
15-
importDoraemonfrom'@containers/Doraemon'
16-
importRoutefrom'@containers/Route'
17-
importHeaderfrom'@containers/Header'
18-
importCommunityBannerfrom'@containers/CommunityBanner'
19-
importCommunityContentfrom'@containers/CommunityContent'
20-
importFooterfrom'@containers/Footer'
21-
importErrorBoxfrom'@containers/ErrorBox'
22-
importErrorPagefrom'@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+
importAnalysisServicefrom'@services/Analysis'
29+
importGlobalLayoutfrom'@containers/GlobalLayout'
30+
importThemeWrapperfrom'@containers/ThemeWrapper'
31+
importMultiLanguagefrom'@containers/MultiLanguage'
32+
importSidebarfrom'@containers/Sidebar'
33+
importPreviewfrom'@containers/Preview'
34+
importDoraemonfrom'@containers/Doraemon'
35+
importRoutefrom'@containers/Route'
36+
importHeaderfrom'@containers/Header'
37+
importCommunityBannerfrom'@containers/CommunityBanner'
38+
importCommunityContentfrom'@containers/CommunityContent'
39+
importFooterfrom'@containers/Footer'
40+
importErrorBoxfrom'@containers/ErrorBox'
41+
importErrorPagefrom'@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-
constmainPath=getMainPath(props)
62+
const{ mainPath,subPath:topic}=parseURL(props)
6463
constcommunity=akaTranslate(mainPath)
65-
consttopic=getSubPath(props)
6664
constthread=extractThreadFromPath(props)
6765

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

108106
exportdefaultclassCommunityPageextendsReact.Component{
109107
staticasyncgetInitialProps(props){
110-
constmainPath=getMainPath(props)
111-
constsubPath=getSubPath(props)
108+
if(!isServerSide())return{}
109+
110+
const{ mainPath, subPath}=parseURL(props)
112111
constthread=extractThreadFromPath(props)
113112

114113
letresp

‎pages/home/posts.js‎

Lines changed: 3 additions & 6 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-
constmainPath=getMainPath(props)
62+
const{mainPath,subPath:topic}=parseURL(props)
6463
constcommunity=akaTranslate(mainPath)
65-
consttopic=getSubPath(props)
6664
constthread=extractThreadFromPath(props)
6765

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

108106
exportdefaultclassHomePageextendsReact.Component{
109107
staticasyncgetInitialProps(props){
110-
constmainPath=getMainPath(props)
111-
constsubPath=getSubPath(props)
108+
const{ mainPath, subPath}=parseURL(props)
112109
constthread=extractThreadFromPath(props)
113110

114111
letresp

‎pages/index.js‎

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

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

99
importGlobalLayoutfrom'@containers/GlobalLayout'
@@ -37,13 +37,9 @@ global.Intl = require('intl')
3737
*/
3838
exportdefaultclassPageCommunityextendsReact.Component{
3939
staticasyncgetInitialProps(props){
40-
constisServer=typeofwindow==='undefined'
41-
console.log('page:index isServer: ',isServer)
40+
if(!isServerSide())return{}
4241

43-
if(!isServer)return{}
44-
45-
constmainPath=getMainPath(props)
46-
constsubPath=getMainPath(props)
42+
const{ mainPath, subPath}=parseURL(props)
4743
consthideSidebar=
4844
R.contains(mainPath,[ROUTE.USER])||
4945
R.contains(subPath,[ROUTE.POST,ROUTE.REPO,ROUTE.VIDEO,ROUTE.JOB])

‎pages/job.js‎

Lines changed: 5 additions & 6 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
constuserHasLogin=nilOrEmpty(token)===false
4644

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

5048
// query data
5149
constsessionState=gqClient.request(P.sessionState)
@@ -74,17 +72,18 @@ async function fetchData(props) {
7472
exportdefaultclassJobPageextendsReact.Component{
7573
staticasyncgetInitialProps(props){
7674
letresp
75+
const{ mainPath, subPath}=parseURL(props)
76+
7777
try{
7878
resp=awaitfetchData(props)
7979
}catch({response:{ errors}}){
8080
if(ssrAmbulance.hasLoginError(errors)){
8181
resp=awaitfetchData(props,{realname:false})
8282
}else{
83-
return{statusCode:404,target:getSubPath(props)}
83+
return{statusCode:404,target:subPath}
8484
}
8585
}
8686

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

9089
return{

‎pages/post.js‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import {
99
getJwtToken,
1010
nilOrEmpty,
1111
makeGQClient,
12-
getMainPath,
13-
getSubPath,
14-
getThirdPath,
12+
parseURL,
1513
ssrAmbulance,
1614
parseTheme,
1715
}from'@utils'
@@ -46,7 +44,7 @@ async function fetchData(props, opt) {
4644
constuserHasLogin=nilOrEmpty(token)===false
4745

4846
// schema
49-
constid=getThirdPath(props)
47+
const{thridPath:id}=parseURL(props)
5048

5149
// query data
5250
constsessionState=gqClient.request(P.sessionState)
@@ -75,22 +73,22 @@ async function fetchData(props, opt) {
7573

7674
exportdefaultclassPostPageextendsReact.Component{
7775
staticasyncgetInitialProps(props){
76+
const{ mainPath, subPath}=parseURL(props)
7877
letresp
7978
try{
8079
resp=awaitfetchData(props)
8180
}catch({response:{ errors}}){
8281
if(ssrAmbulance.hasLoginError(errors)){
8382
resp=awaitfetchData(props,{realname:false})
8483
}else{
85-
return{statusCode:404,target:getSubPath(props)}
84+
return{statusCode:404,target:subPath}
8685
}
8786
}
8887

89-
constmainPath=getMainPath(props)
9088
const{ sessionState, post, pagedComments, subscribedCommunities}=resp
9189

9290
if(!R.contains(mainPath,R.pluck('raw',post.communities))){
93-
return{statusCode:404,target:getSubPath(props)}
91+
return{statusCode:404,target:subPath}
9492
}
9593

9694
return{

‎pages/repo.js‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import {
99
getJwtToken,
1010
nilOrEmpty,
1111
makeGQClient,
12-
getMainPath,
13-
getSubPath,
14-
getThirdPath,
12+
parseURL,
1513
ssrAmbulance,
1614
parseTheme,
1715
}from'@utils'
@@ -45,7 +43,7 @@ async function fetchData(props, opt) {
4543
constgqClient=makeGQClient(token)
4644
constuserHasLogin=nilOrEmpty(token)===false
4745

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

5048
constsessionState=gqClient.request(P.sessionState)
5149
constrepo=gqClient.request(P.repo,{ id})
@@ -72,22 +70,22 @@ async function fetchData(props, opt) {
7270

7371
exportdefaultclassRepoPageextendsReact.Component{
7472
staticasyncgetInitialProps(props){
73+
const{ mainPath, subPath}=parseURL(props)
7574
letresp
7675
try{
7776
resp=awaitfetchData(props)
7877
}catch({response:{ errors}}){
7978
if(ssrAmbulance.hasLoginError(errors)){
8079
resp=awaitfetchData(props,{realname:false})
8180
}else{
82-
return{statusCode:404,target:getSubPath(props)}
81+
return{statusCode:404,target:subPath}
8382
}
8483
}
8584

86-
constmainPath=getMainPath(props)
8785
const{ sessionState, repo, pagedComments, subscribedCommunities}=resp
8886

8987
if(!R.contains(mainPath,R.pluck('raw',repo.communities))){
90-
return{statusCode:404,target:getSubPath(props)}
88+
return{statusCode:404,target:subPath}
9189
}
9290

9391
return{

‎pages/video.js‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import {
99
getJwtToken,
1010
nilOrEmpty,
1111
makeGQClient,
12-
getMainPath,
13-
getSubPath,
14-
getThirdPath,
12+
parseURL,
1513
ssrAmbulance,
1614
parseTheme,
1715
}from'@utils'
@@ -42,7 +40,7 @@ async function fetchData(props) {
4240
constgqClient=makeGQClient(token)
4341
constuserHasLogin=nilOrEmpty(token)===false
4442

45-
constid=getThirdPath(props)
43+
const{thridPath:id}=parseURL(props)
4644

4745
// query data
4846
constsessionState=gqClient.request(P.sessionState)
@@ -70,22 +68,22 @@ async function fetchData(props) {
7068

7169
exportdefaultclassVideoPageextendsReact.Component{
7270
staticasyncgetInitialProps(props){
71+
const{ mainPath, subPath}=parseURL(props)
7372
letresp
7473
try{
7574
resp=awaitfetchData(props)
7675
}catch({response:{ errors}}){
7776
if(ssrAmbulance.hasLoginError(errors)){
7877
resp=awaitfetchData(props,{realname:false})
7978
}else{
80-
return{statusCode:404,target:getSubPath(props)}
79+
return{statusCode:404,target:subPath}
8180
}
8281
}
8382

84-
constmainPath=getMainPath(props)
8583
const{ sessionState, video, pagedComments, subscribedCommunities}=resp
8684

8785
if(!R.contains(mainPath,R.pluck('raw',video.communities))){
88-
return{statusCode:404,target:getSubPath(props)}
86+
return{statusCode:404,target:subPath}
8987
}
9088

9189
return{

‎utils/index.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ export {
5454
// export { default as githubApi } from './github_api'
5555

5656
export{
57-
getMainPath,
58-
getSubPath,
59-
getThirdPath,
57+
parseURL,
6058
akaTranslate,
6159
getParameterByName,
6260
getQueryFromUrl,
@@ -82,6 +80,7 @@ export {
8280
}from'./mobx_helper'
8381

8482
export{
83+
isServerSide,
8584
getJwtToken,
8685
ssrPagedSchema,
8786
ssrPagedFilter,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp