@@ -12,6 +12,9 @@ import api from 'boost/api'
1212import auth from 'boost/auth'
1313import io from 'boost/socket'
1414
15+ const TEXT_FILTER = 'TEXT_FILTER'
16+ const FOLDER_FILTER = 'FOLDER_FILTER'
17+
1518class HomePage extends React . Component {
1619 componentDidMount ( ) {
1720 const { dispatch } = this . props
@@ -53,7 +56,7 @@ class HomePage extends React.Component {
5356 < div className = 'HomePage' >
5457 < UserNavigator users = { users } />
5558 < ArticleNavigator dispatch = { dispatch } activeUser = { activeUser } status = { status } />
56- < ArticleTopBar />
59+ < ArticleTopBar dispatch = { dispatch } status = { status } />
5760 < ArticleList dispatch = { dispatch } articles = { articles } status = { status } activeArticle = { activeArticle } />
5861 < ArticleDetail dispatch = { dispatch } activeUser = { activeUser } activeArticle = { activeArticle } status = { status } />
5962 </ div >
@@ -72,12 +75,42 @@ function remap (state) {
7275 let activeUser = findWhere ( users , { id : parseInt ( status . userId , 10 ) } )
7376 if ( activeUser == null ) activeUser = users [ 0 ]
7477
78+ // Fetch articles
7579 let articles = state . articles [ 'team-' + activeUser . id ]
7680 if ( articles == null ) articles = [ ]
7781 articles . sort ( ( a , b ) => {
7882 return new Date ( b . updatedAt ) - new Date ( a . updatedAt )
7983 } )
8084
85+ // Filter articles
86+ let filters = status . search . split ( ' ' ) . map ( key => key . trim ( ) ) . filter ( key => key . length > 0 ) . map ( key => {
87+ if ( key . match ( / ^ i n : .+ $ / ) ) {
88+ return { type : FOLDER_FILTER , value : key . match ( / ^ i n : ( .+ ) $ / ) [ 1 ] }
89+ }
90+ return { type : TEXT_FILTER , value : key }
91+ } )
92+ let folderFilters = filters . filter ( filter => filter . type === FOLDER_FILTER )
93+ let textFilters = filters . filter ( filter => filter . type === TEXT_FILTER )
94+
95+ let targetFolders = activeUser . Folders . filter ( folder => {
96+ return findWhere ( folderFilters , { value : folder . name } )
97+ } )
98+ status . targetFolders = targetFolders
99+
100+ if ( targetFolders . length > 0 ) {
101+ articles = articles . filter ( article => {
102+ return findWhere ( targetFolders , { id : article . FolderId } )
103+ } )
104+ }
105+ if ( textFilters . length > 0 ) {
106+ articles = textFilters . reduce ( ( articles , textFilter ) => {
107+ return articles . filter ( article => {
108+ return article . title . match ( new RegExp ( textFilter . value , 'i' ) ) || article . content . match ( new RegExp ( textFilter . value , 'i' ) )
109+ } )
110+ } , articles )
111+ }
112+
113+ // Grab active article
81114 let activeArticle = findWhere ( articles , { key : status . articleKey } )
82115 if ( activeArticle == null ) activeArticle = articles [ 0 ]
83116
@@ -120,7 +153,7 @@ function remap (state) {
120153 articles,
121154 activeArticle
122155 }
123- console . log ( props )
156+
124157 return props
125158}
126159
@@ -131,8 +164,7 @@ HomePage.propTypes = {
131164 userId : PropTypes . string
132165 } ) ,
133166 status : PropTypes . shape ( {
134- userId : PropTypes . string ,
135- folderId : PropTypes . number
167+ userId : PropTypes . string
136168 } ) ,
137169 articles : PropTypes . array ,
138170 activeArticle : PropTypes . shape ( ) ,
0 commit comments