Skip to content

Commit 0d4d1eb

Browse files
committed
put query state into route store
1 parent a00b013 commit 0d4d1eb

File tree

8 files changed

+156
-65
lines changed

8 files changed

+156
-65
lines changed

containers/CommunitiesContent/IndexContent.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,31 +121,29 @@ const columns = [
121121
dataIndex: '',
122122
align: 'center',
123123
key: 'operation',
124-
render: (text, record) => {
125-
return (
126-
<OperationWrapper>
127-
<Button
128-
size="small"
129-
type="primary"
130-
ghost
131-
onClick={logic.onEdit.bind(this, record)}
132-
>
133-
编辑
124+
render: (text, record) => (
125+
<OperationWrapper>
126+
<Button
127+
size="small"
128+
type="primary"
129+
ghost
130+
onClick={logic.onEdit.bind(this, record)}
131+
>
132+
编辑
133+
</Button>
134+
<Space right="10px" />
135+
<Popconfirm
136+
title="确定删除 ??"
137+
okText="Y"
138+
cancelText="N"
139+
onConfirm={logic.onDelete.bind(this, record)}
140+
>
141+
<Button size="small" type="red" ghost>
142+
删除
134143
</Button>
135-
<Space right="10px" />
136-
<Popconfirm
137-
title="确定删除 ??"
138-
okText="Y"
139-
cancelText="N"
140-
onConfirm={logic.onDelete.bind(this, record)}
141-
>
142-
<Button size="small" type="red" ghost>
143-
删除
144-
</Button>
145-
</Popconfirm>
146-
</OperationWrapper>
147-
)
148-
},
144+
</Popconfirm>
145+
</OperationWrapper>
146+
),
149147
},
150148
]
151149

containers/CommunitiesContent/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import React from 'react'
88
import { inject, observer } from 'mobx-react'
99
// import Link from 'next/link'
10+
/* import { withRouter } from 'next/router' */
1011

1112
import { makeDebugger, storePlug, ROUTE } from '../../utils'
1213

containers/CommunitiesContent/logic.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import R from 'ramda'
2+
/* import Router from 'next/router' */
23

34
import {
45
asyncRes,
@@ -36,7 +37,10 @@ export function loadCommunities(page = 1) {
3637
communitiesLoading: true,
3738
})
3839
scrollIntoEle(TYPE.APP_HEADER_ID)
40+
3941
sr71$.query(S.pagedCommunities, args)
42+
43+
communitiesContent.markQuery({ page, size })
4044
}
4145

4246
export function loadPosts(page = 1) {
@@ -275,6 +279,4 @@ export function init(selectedStore) {
275279
communitiesContent = selectedStore
276280
debug(communitiesContent)
277281
sr71$.data().subscribe($solver(DataSolver, ErrSolver))
278-
279-
// loadCommunities()
280282
}

containers/Route/logic.js

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,109 @@
11
import R from 'ramda'
22

3-
import { makeDebugger, isEmptyNil } from '../../utils'
3+
import { makeDebugger /* isEmptyNil, getParameterByName */ } from '../../utils'
44

55
/* eslint-disable no-unused-vars */
66
const debug = makeDebugger('L:Route')
77
/* eslint-enable no-unused-vars */
88

99
let route = null
1010

11-
const getAsPathList = R.compose(
11+
/*
12+
const getAsPathList = R.compose(
13+
R.reject(R.isEmpty),
14+
R.split('/'),
15+
R.prop('asPath')
16+
)
17+
18+
const getQueryMain = routeObj => {
19+
if (R.isEmpty(routeObj)) return ''
20+
21+
if (isEmptyNil(routeObj.query) && routeObj.pathname !== '/') {
22+
return routeObj.pathname.slice(1)
23+
} else if (isEmptyNil(routeObj.query) && routeObj.pathname === '/') {
24+
const asPathList = getAsPathList(routeObj)
25+
return R.head(asPathList)
26+
}
27+
28+
return routeObj.query.main
29+
}
30+
31+
const getQuerySub = routeObj => {
32+
const asPathList = getAsPathList(routeObj)
33+
return R.last(asPathList)
34+
}
35+
*/
36+
37+
const asPathForMain = R.compose(
38+
R.head,
39+
R.split('?'),
40+
R.head,
1241
R.reject(R.isEmpty),
1342
R.split('/'),
1443
R.prop('asPath')
1544
)
1645

17-
const getQueryMain = routeObj => {
18-
if (R.isEmpty(routeObj)) return ''
46+
const INDEX = ''
47+
const getMainPath = routeObj => {
48+
if (R.isEmpty(routeObj)) return INDEX
49+
const { asPath } = routeObj
50+
if (asPath === '/') return INDEX
1951

20-
if (isEmptyNil(routeObj.query) && routeObj.pathname !== '/') {
21-
return routeObj.pathname.slice(1)
22-
} else if (isEmptyNil(routeObj.query) && routeObj.pathname === '/') {
23-
const asPathList = getAsPathList(routeObj)
24-
return R.head(asPathList)
25-
}
52+
/* const test = splitAsPathTest('/communities/posts/?page=1&size=20') */
53+
/* console.log('test --> ', test) */
2654

27-
return routeObj.query.main
55+
/* const asPathList = splitAsPath(routeObj) */
56+
/* console.log('asPathList -- > ', asPathList) */
57+
return asPathForMain(routeObj)
2858
}
2959

30-
const getQuerySub = routeObj => {
31-
const asPathList = getAsPathList(routeObj)
32-
return R.last(asPathList)
60+
const asPathForSub = R.compose(
61+
R.reject(R.isEmpty),
62+
R.split('/'),
63+
R.head,
64+
R.reject(R.contains('=')),
65+
R.reject(R.isEmpty),
66+
R.split('?'),
67+
R.prop('asPath')
68+
)
69+
70+
const getSubPath = routeObj => {
71+
if (R.isEmpty(routeObj)) return INDEX
72+
73+
const { asPath } = routeObj
74+
if (asPath === '/') return INDEX
75+
76+
const asPathList = asPathForSub(routeObj)
77+
/* console.log('asPath jjj -> ', asPathList) */
78+
/* const asPathList = asPathForSub('/communities') */
79+
80+
/* const asPathList = asPathForSub('/communities/posts?page=2&size=20') */
81+
/* const asPathListtest = asPathForSub('/communities/posts/?page=2&size=20') */
82+
/* const asPathListtest = fuckmetest('/communities') */
83+
/* const asPathListtest = fuckmetest('/communities/') */
84+
/* console.log('asPathListtest jjj -> ', asPathListtest) */
85+
86+
return asPathList.length > 1 ? asPathList[1] : asPathList[0]
3387
}
3488

3589
export function syncRoute(routeObj) {
36-
/* console.log('syncRoute routeObj: ', routeObj) */
37-
/*
38-
console.log('syncRoute query: ', routeObj.query)
39-
console.log('syncRoute pathname: ', routeObj.pathname)
40-
console.log('syncRoute asPath: ', routeObj.asPath)
41-
console.log('syncRoute route: ', routeObj.route)
90+
/* const mainQuery = getQueryMain(routeObj) */
91+
const mainQuery = getMainPath(routeObj)
92+
/* console.log('mainQuery ## ', mainQuery) */
4293

43-
console.log(' ---------- ')
44-
console.log('### getQueryMain: ', getQueryMain(routeObj))
45-
console.log('### getQuerySub: ', getQuerySub(routeObj))
46-
*/
94+
const subQuery = getSubPath(routeObj)
95+
/* const subQuery = getSubPath(routeObj) */
96+
/* const subPath = getSubPath(routeObj) */
97+
/* console.log('subPath final --> ', subPath) */
4798

48-
const mainQuery = getQueryMain(routeObj)
49-
const subQuery = getQuerySub(routeObj)
99+
const { query } = routeObj
50100

101+
// TODO: mainQuery -> mainPath
102+
// subQuery -> subPath
51103
route.markState({
52104
mainQuery,
53105
subQuery,
106+
query,
54107
})
55108
}
56109

containers/Sidebar/logic.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ export function pin() {
3434
export function extendMenuBar(communityRaw) {
3535
switch (communityRaw) {
3636
case ROUTE.COMMUNITIES: {
37-
return Router.push(`/${ROUTE.COMMUNITIES}`, `/${communityRaw}/`)
37+
/* return Router.push(`/${ROUTE.COMMUNITIES}`, `/${communityRaw}/`) */
38+
return Router.push(
39+
{
40+
pathname: `/${ROUTE.COMMUNITIES}`,
41+
asPath: `/${communityRaw}/`,
42+
}
43+
/* `/${ROUTE.COMMUNITIES}`, `/${communityRaw}/` */
44+
)
3845
}
3946
case ROUTE.USERS: {
4047
return Router.push(`/${ROUTE.USERS}`, `/${communityRaw}/`)

server.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,9 @@ const communityQuery = route('/:main/:sub?')
5555

5656
app.prepare().then(() => {
5757
createServer((req, res) => {
58-
/*
59-
req.url = req.url.replace(/\/$/, '')
60-
if (req.url === '') {
61-
req.url = '/'
62-
}
63-
handle(req, res)
64-
*/
65-
66-
const { pathname } = parse(req.url)
58+
const urlParts = parse(req.url, true)
59+
/* const { pathname } = parse(req.url) */
60+
const { pathname, query } = urlParts
6761

6862
const accept = accepts(req)
6963
const locale = accept.language(supportLanguages) // 'zh'
@@ -72,11 +66,14 @@ app.prepare().then(() => {
7266
res.setHeader('Content-Type', 'application/json;charset=utf-8')
7367
return res.end(JSON.stringify(getMessages(localeQuery(pathname).lang)))
7468
} else if (communitiesQuery(pathname)) {
75-
return app.render(req, res, '/communities')
69+
/* console.log('===========================================') */
70+
/* console.log('req.params: ', parse(req.url, true)) */
71+
/* console.log('===========================================') */
72+
return app.render(req, res, '/communities', query)
7673
} else if (usersQuery(pathname)) {
77-
return app.render(req, res, '/users')
74+
return app.render(req, res, '/users', query)
7875
} else if (communityQuery(pathname)) {
79-
return app.render(req, res, '/')
76+
return app.render(req, res, '/', query)
8077
}
8178

8279
// now index page go this way

stores/CommunitiesContentStore/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ const CommunitiesContentStore = t
6060
},
6161
}))
6262
.actions(self => ({
63+
markQuery(query) {
64+
self.root.route.markQuery(query)
65+
},
6366
markState(sobj) {
6467
markStates(sobj, self)
6568
},

stores/RouteStore/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@
44
*/
55

66
import { types as t } from 'mobx-state-tree'
7+
import R from 'ramda'
8+
import Router from 'next/router'
79

810
import { markStates, makeDebugger } from '../../utils'
911
/* eslint-disable no-unused-vars */
1012
const debug = makeDebugger('S:RouteStore')
1113
/* eslint-enable no-unused-vars */
1214

15+
const Query = t.model('Query', {
16+
page: t.optional(t.string, '1'),
17+
size: t.optional(t.string, '20'), // TODO: use config
18+
// sort .... [when, ...]
19+
// view ... [chart, list ...]
20+
})
21+
1322
const RouteStore = t
1423
.model('RouteStore', {
1524
mainQuery: t.optional(t.string, ''),
1625
subQuery: t.optional(t.string, ''),
26+
query: t.optional(Query, {}),
1727
})
1828
.views(self => ({
1929
get curPath() {
@@ -25,6 +35,26 @@ const RouteStore = t
2535
},
2636
}))
2737
.actions(self => ({
38+
markQuery(query = {}) {
39+
query = R.mapObjIndexed(v => String(v), query)
40+
const { page } = query
41+
42+
// page = 1 is default
43+
if (page && page === '1') {
44+
query = R.omit(['page', 'size'], query)
45+
}
46+
47+
/* debug('final query => ', query) */
48+
if (typeof window !== 'undefined') {
49+
return Router.push({
50+
pathname: `/${self.mainQuery}`,
51+
asPath: `/${self.subQuery}`,
52+
query,
53+
shallow: true,
54+
})
55+
}
56+
},
57+
2858
markState(sobj) {
2959
markStates(sobj, self)
3060
},

0 commit comments

Comments
 (0)