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

Commit 4941c2d

Browse files
committed
Merge branch 'sidebar-sort' into dev
2 parents 4b82eb0 + 0893027 commit 4941c2d

File tree

12 files changed

+104
-66
lines changed

12 files changed

+104
-66
lines changed

components/Tabber/BriefView.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
import React from 'react'
2-
import R from 'ramda'
32

43
import { Wrapper, TabItem } from './styles/brief_view'
54
import TabIcon from './TabIcon'
65

7-
import { uid, Trans } from '../../utils'
6+
import { uid, Trans, sortByIndex } from '../../utils'
87

9-
const BriefView = ({ source, active, onChange }) => {
10-
const sortedTabs = R.sort((a, b) => a.index - b.index, source)
11-
12-
return (
13-
<Wrapper>
14-
{sortedTabs.map(item => (
15-
<TabItem
16-
key={uid.gen()}
17-
active={item.raw === active}
18-
onClick={onChange.bind(this, item.raw)}
19-
>
20-
<TabIcon raw={item.raw} active={active} small />
21-
{item.alias ? item.alias : Trans(item.title)}
22-
</TabItem>
23-
))}
24-
</Wrapper>
25-
)
26-
}
8+
const BriefView = ({ source, active, onChange }) => (
9+
<Wrapper>
10+
{sortByIndex(source).map(item => (
11+
<TabItem
12+
key={uid.gen()}
13+
active={item.raw === active}
14+
onClick={onChange.bind(this, item.raw)}
15+
>
16+
<TabIcon raw={item.raw} active={active} small />
17+
{item.alias ? item.alias : Trans(item.title)}
18+
</TabItem>
19+
))}
20+
</Wrapper>
21+
)
2722

2823
export default BriefView

components/Tabber/NormalView.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
import React from 'react'
2-
import R from 'ramda'
32
import { Tabs } from 'antd'
43

54
import { LableWrapper } from './styles'
65
import TabIcon from './TabIcon'
7-
import { Trans } from '../../utils'
6+
import { Trans, sortByIndex } from '../../utils'
87

98
const { TabPane } = Tabs
109

11-
const NormalView = ({ source, active, onChange }) => {
12-
const sortedTabs = R.sort((a, b) => a.index - b.index, source)
13-
14-
return (
15-
<Tabs onChange={onChange} activeKey={active}>
16-
{sortedTabs.map(item => (
17-
<TabPane
18-
tab={
19-
<LableWrapper>
20-
<TabIcon raw={item.raw} active={active} />
21-
{item.alias ? item.alias : Trans(item.title)}
22-
</LableWrapper>
23-
}
24-
key={item.raw}
25-
/>
26-
))}
27-
</Tabs>
28-
)
29-
}
10+
const NormalView = ({ source, active, onChange }) => (
11+
<Tabs onChange={onChange} activeKey={active}>
12+
{sortByIndex(source).map(item => (
13+
<TabPane
14+
tab={
15+
<LableWrapper>
16+
<TabIcon raw={item.raw} active={active} />
17+
{item.alias ? item.alias : Trans(item.title)}
18+
</LableWrapper>
19+
}
20+
key={item.raw}
21+
/>
22+
))}
23+
</Tabs>
24+
)
3025

3126
export default NormalView

components/Tabber/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44

55
import React from 'react'
66
import PropTypes from 'prop-types'
7-
import R from 'ramda'
87

98
import NormalView from './NormalView'
109
import BriefView from './BriefView'
1110

1211
import { mapAlias } from './alias'
1312

14-
import { makeDebugger, THREAD, C11N } from '../../utils'
13+
import { makeDebugger, THREAD, C11N, sortByIndex } from '../../utils'
1514
/* eslint-disable no-unused-vars */
1615
const debug = makeDebugger('c:Tabber:index')
1716
/* eslint-enable no-unused-vars */
1817

1918
const Tabber = ({ source, active, onChange, layout, communityRaw }) => {
2019
const aliasSource = mapAlias(source, communityRaw)
21-
const sortedSource = R.sort((a, b) => a.index - b.index, aliasSource)
20+
const sortedSource = sortByIndex(aliasSource)
2221

2322
return (
2423
<React.Fragment>

containers/Sidebar/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ const debug = makeDebugger('C:Sidebar:index')
2020
/* eslint-enable no-unused-vars */
2121

2222
class SidebarContainer extends React.Component {
23-
constructor(props) {
24-
super(props)
25-
const { sidebar } = props
26-
logic.init(sidebar)
27-
}
28-
2923
componentDidMount() {
30-
logic.loadCommunities()
24+
const { sidebar } = this.props
25+
logic.init(sidebar)
26+
// logic.loadCommunities()
3127
}
3228

3329
render() {

containers/Sidebar/logic.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// import R from 'ramda'
1+
import R from 'ramda'
22
// const debug = makeDebugger('L:sidebar')
33
import { arrayMove } from 'react-sortable-hoc'
44

@@ -17,7 +17,6 @@ import {
1717
/* import { GRAPHQL_ENDPOINT } from '../../config' */
1818

1919
import S from './schema'
20-
2120
import SR71 from '../../utils/network/sr71'
2221

2322
const sr71$ = new SR71({
@@ -48,18 +47,38 @@ export function onCommunitySelect(community) {
4847
dispatchEvent(EVENT.COMMUNITY_CHANGE)
4948
}
5049

50+
const mapIndexed = R.addIndex(R.map)
51+
5152
export function onSortMenuEnd({ oldIndex, newIndex }) {
5253
const sortedCommunities = arrayMove(store.communitiesData, oldIndex, newIndex)
5354
// TODO: sync to server
55+
setC11N(sortedCommunities)
5456
store.onSortCommunities(sortedCommunities)
5557
}
5658

57-
export function loadCommunities() {
58-
/* const user = BStore.get('user') */
59+
const setC11N = sortedCommunities => {
60+
const { isLogin } = store
61+
if (!isLogin) return store.authWarning()
62+
63+
// TODO: check login
64+
sortedCommunities = R.reject(R.propEq('raw', 'home'), sortedCommunities)
65+
const sidebarCommunitiesIndex = mapIndexed(
66+
(c, index) => ({ community: c.raw, index }),
67+
sortedCommunities
68+
)
69+
70+
const { contentDivider } = store.accountInfo.customization
5971
const args = {
60-
filter: { page: 1, size: 30 },
72+
customization: {
73+
contentDivider,
74+
},
75+
sidebarCommunitiesIndex,
6176
}
62-
/* console.log('loadCommunities: ', GRAPHQL_ENDPOINT) */
77+
sr71$.mutate(S.setCustomization, args)
78+
}
79+
80+
export function loadCommunities() {
81+
const args = { filter: { page: 1, size: 30 } }
6382
sr71$.query(S.subscribedCommunities, args)
6483
}
6584

@@ -69,6 +88,10 @@ const DataSolver = [
6988
action: ({ subscribedCommunities }) =>
7089
store.loadCommunities(subscribedCommunities),
7190
},
91+
{
92+
match: asyncRes('setCustomization'),
93+
action: () => loadCommunities(),
94+
},
7295
{
7396
match: asyncRes(EVENT.LOGOUT),
7497
action: () => loadCommunities(),
@@ -101,9 +124,8 @@ const ErrSolver = [
101124
]
102125

103126
export function init(_store) {
104-
if (store) return false
105127
store = _store
106128

107-
if (sub$) sub$.unsubscribe()
129+
if (sub$) return false
108130
sub$ = sr71$.data().subscribe($solver(DataSolver, ErrSolver))
109131
}

containers/Sidebar/schema.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,26 @@ const subscribedCommunities = gql`
1212
}
1313
`
1414

15+
const setCustomization = gql`
16+
mutation(
17+
$userId: ID
18+
$customization: CustomizationInput!
19+
$sidebarCommunitiesIndex: [CommunityIndex]
20+
) {
21+
setCustomization(
22+
userId: $userId
23+
customization: $customization
24+
sidebarCommunitiesIndex: $sidebarCommunitiesIndex
25+
) {
26+
id
27+
}
28+
}
29+
`
30+
1531
const schema = {
1632
// communities,
1733
subscribedCommunities,
34+
setCustomization,
1835
}
1936

2037
export default schema

containers/Sidebar/store.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { types as t, getParent } from 'mobx-state-tree'
77
import R from 'ramda'
8-
import { makeDebugger, markStates, stripMobx } from '../../utils'
8+
import { makeDebugger, markStates, stripMobx, sortByIndex } from '../../utils'
99
/* import MenuItem from './MenuItemStore' */
1010

1111
/* eslint-disable no-unused-vars */
@@ -48,10 +48,15 @@ const SidebarStore = t
4848
},
4949
get communitiesData() {
5050
const { subscribedCommunities } = self.root.account
51-
return subscribedCommunities ? subscribedCommunities.entries : []
51+
return subscribedCommunities
52+
? sortByIndex(subscribedCommunities.entries)
53+
: []
5254
},
5355
}))
5456
.actions(self => ({
57+
authWarning(options) {
58+
self.root.authWarning(options)
59+
},
5560
markRoute(query) {
5661
self.root.markRoute(query)
5762
},

containers/schemas/fragments/base.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const community = `
22
id
33
title
44
raw
5+
index
56
desc
67
logo
78
`

stores/SharedModel/Community.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const Community = t.model('Community', {
1919
title: t.maybeNull(t.string),
2020
desc: t.optional(t.string, ''),
2121
raw: t.maybeNull(t.string),
22+
index: t.optional(t.number, 1000000),
2223
logo: t.maybeNull(t.string),
2324
categories: t.optional(t.array(SimpleCategory), []),
2425
contributesDigest: t.optional(t.array(t.number), []),

utils/functions.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ export const mapKeys = R.curry((fn, obj) => {
2424
})
2525

2626
export const sortByColor = source =>
27-
source.sort((t1, t2) => TAG_COLOR_ORDER[t1.color] - TAG_COLOR_ORDER[t2.color])
27+
R.sort(
28+
(t1, t2) => TAG_COLOR_ORDER[t1.color] - TAG_COLOR_ORDER[t2.color],
29+
source
30+
)
31+
32+
export const sortByIndex = source => R.sort((a, b) => a.index - b.index, source)
2833

2934
/* eslint-disable */
3035
const log = (...args) => data => {

0 commit comments

Comments
 (0)