1- import React , { Fragment , Component } from 'react' ;
1+ import React , { Component } from 'react' ;
22import classnames from 'classnames' ;
33import { observer , inject } from 'mobx-react' ;
44import _ from 'lodash' ;
55import { translate } from 'react-i18next' ;
66
7- import { Icon , Notification } from 'components/Base' ;
7+ import { Icon } from 'components/Base' ;
88import Layout , { TitleBanner } from 'components/Layout' ;
99import AppList from 'components/AppList' ;
1010import Loading from 'components/Loading' ;
1111import InfiniteScroll from 'components/InfiniteScroll' ;
1212
13- import { getScrollTop , getScrollBottom } from 'utils' ;
13+ import { getScrollTop } from 'utils' ;
1414import { getUrlParam } from 'utils/url' ;
1515
1616import styles from './index.scss' ;
@@ -29,36 +29,42 @@ export default class Home extends Component {
2929 static isHome = true ;
3030
3131 state = {
32- pageLoading : true
32+ pageLoading : true ,
33+ cate : ''
3334 } ;
3435
3536 get searchWord ( ) {
36- return getUrlParam ( 'q' ) ;
37+ return this . props . search || getUrlParam ( 'q' ) ;
38+ }
39+
40+ get category ( ) {
41+ return this . state . cate || getUrlParam ( 'cate' ) ;
3742 }
3843
3944 async componentDidMount ( ) {
40- const {
41- rootStore, appStore, categoryStore, match
42- } = this . props ;
43- const { category } = match . params ;
45+ const { rootStore, appStore, categoryStore } = this . props ;
46+
47+ rootStore . setNavFix ( Boolean ( this . category || this . searchWord ) ) ;
4448
45- rootStore . setNavFix ( Boolean ( category || this . searchWord ) ) ;
46- if ( ! ( category || this . searchWord ) ) {
49+ if ( ! ( this . category || this . searchWord ) ) {
4750 this . threshold = this . getThreshold ( ) ;
48- window . onscroll = _ . throttle ( this . handleScroll , 100 ) ;
51+ window . onscroll = _ . debounce ( this . handleScroll , 100 ) ;
4952 }
5053 window . scroll ( { top : 0 } ) ;
5154
52- await categoryStore . fetchAll ( ) ;
55+ await categoryStore . fetchAll ( { noLimit : true } ) ;
5356 await appStore . fetchStoreAppsCount ( ) ;
5457
5558 // always fetch active apps
56- appStore . selectStatus = 'active' ;
59+ Object . assign ( appStore , {
60+ selectStatus : 'active' ,
61+ currentPage : 1
62+ } ) ;
5763
58- if ( this . searchWord || category ) {
64+ if ( this . searchWord || ( this . category && this . category !== cateLatest ) ) {
5965 await appStore . fetchAll ( {
6066 search_word : this . searchWord ,
61- category_id : category
67+ category_id : this . category
6268 } ) ;
6369 } else {
6470 await appStore . fetchActiveApps ( ) ;
@@ -69,30 +75,34 @@ export default class Home extends Component {
6975 } ) ;
7076 }
7177
72- async componentDidUpdate ( prevProps ) {
73- const { match , appStore, search } = this . props ;
74- const { category } = match . params ;
78+ async componentDidUpdate ( prevProps , prevState ) {
79+ const { appStore, search } = this . props ;
80+ const { cate } = this . state ;
7581
76- if ( prevProps . match . params . category !== category ) {
77- if ( category === cateLatest ) {
78- await appStore . fetchActiveApps ( {
79- search_word : this . searchWord
80- } ) ;
82+ appStore . showActiveApps = this . category === cateLatest ;
83+
84+ if ( prevState . cate !== cate ) {
85+ appStore . currentPage = 1 ;
86+ // reset search wd
87+ appStore . searchWord = '' ;
88+ if ( cate === cateLatest ) {
89+ await appStore . fetchActiveApps ( ) ;
8190 } else {
82- // reset search wd
83- appStore . searchWord = '' ;
8491 await appStore . fetchAll ( {
85- category_id : category
92+ category_id : cate
8693 } ) ;
8794 }
8895 }
8996
9097 if ( prevProps . search !== search ) {
9198 appStore . searchWord = search ;
92- if ( search ) {
93- await appStore . fetchAll ( ) ;
94- } else {
99+ appStore . currentPage = 1 ;
100+
101+ if ( ! search ) {
102+ this . setState ( { cate : cateLatest } ) ;
95103 await appStore . fetchActiveApps ( ) ;
104+ } else {
105+ await appStore . fetchAll ( ) ;
96106 }
97107 }
98108 }
@@ -115,7 +125,7 @@ export default class Home extends Component {
115125 }
116126
117127 handleScroll = async ( ) => {
118- const { rootStore, appStore , categoryStore } = this . props ;
128+ const { rootStore } = this . props ;
119129 const { fixNav } = rootStore ;
120130 if ( this . threshold <= 0 ) {
121131 return ;
@@ -128,20 +138,22 @@ export default class Home extends Component {
128138 } else if ( ! needFixNav && fixNav ) {
129139 rootStore . setNavFix ( false ) ;
130140 }
131-
132- // todo
133141 } ;
134142
135- handleClickCate = cate_id => {
136- this . props . history . push ( `/cat/${ cate_id } ` ) ;
143+ handleClickCate = cate => {
144+ const { rootStore, history } = this . props ;
145+
146+ this . setState ( {
147+ cate
148+ } ) ;
149+ rootStore . setNavFix ( true ) ;
150+ rootStore . setSearchWord ( ) ;
151+ history . push ( `/?cate=${ cate } ` ) ;
137152 } ;
138153
139154 renderCateMenu ( ) {
140- const {
141- match, categoryStore, rootStore, t
142- } = this . props ;
155+ const { categoryStore, rootStore, t } = this . props ;
143156 const { categories } = categoryStore ;
144- const { category } = match . params ;
145157
146158 return (
147159 < div
@@ -155,7 +167,7 @@ export default class Home extends Component {
155167 < li
156168 key = { cateLatest }
157169 className = { classnames ( styles . item , {
158- [ styles . active ] : category === cateLatest
170+ [ styles . active ] : this . category === cateLatest
159171 } ) }
160172 onClick = { ( ) => this . handleClickCate ( cateLatest ) }
161173 >
@@ -172,7 +184,7 @@ export default class Home extends Component {
172184 < li
173185 key = { category_id }
174186 className = { classnames ( styles . item , {
175- [ styles . active ] : category === category_id
187+ [ styles . active ] : this . category === category_id
176188 } ) }
177189 onClick = { ( ) => this . handleClickCate ( category_id ) }
178190 >
@@ -193,7 +205,7 @@ export default class Home extends Component {
193205
194206 render ( ) {
195207 const {
196- rootStore, appStore, categoryStore, match , t
208+ rootStore, appStore, categoryStore, t
197209 } = this . props ;
198210 const { pageLoading } = this . state ;
199211 const { fixNav } = rootStore ;
@@ -203,13 +215,12 @@ export default class Home extends Component {
203215 isLoading,
204216 hasMore,
205217 currentPage,
206- loadMoreHomeApps ,
218+ loadMore ,
207219 countStoreApps
208220 } = appStore ;
209221 const { categories } = categoryStore ;
210- const { category } = match . params ;
211222 const categoryTitle = _ . get (
212- _ . find ( categories , { category_id : category } ) ,
223+ _ . find ( categories , { category_id : this . category } ) ,
213224 'name' ,
214225 ''
215226 ) ;
@@ -231,9 +242,9 @@ export default class Home extends Component {
231242 < InfiniteScroll
232243 className = { styles . apps }
233244 pageStart = { currentPage }
234- loadMore = { loadMoreHomeApps }
245+ loadMore = { loadMore }
235246 isLoading = { isLoading }
236- hasMore = { Boolean ( category || this . searchWord ) && hasMore }
247+ hasMore = { Boolean ( this . category || this . searchWord ) && hasMore }
237248 >
238249 < AppList
239250 apps = { apps }
0 commit comments