1- // import R from 'ramda'
1+ import R from 'ramda'
22// const debug = makeDebugger('L:sidebar')
33import { arrayMove } from 'react-sortable-hoc'
44
@@ -17,7 +17,6 @@ import {
1717/* import { GRAPHQL_ENDPOINT } from '../../config' */
1818
1919import S from './schema'
20-
2120import SR71 from '../../utils/network/sr71'
2221
2322const 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+
5152export 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
103126export 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}
0 commit comments