Skip to content

Commit

Permalink
add simple selector
Browse files Browse the repository at this point in the history
  • Loading branch information
romabelka committed Oct 22, 2018
1 parent e1b9cdb commit b14d600
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"react-redux": "^5.0.7",
"react-scripts": "2.0.4",
"react-select": "^2.1.0",
"redux": "^4.0.1"
"redux": "^4.0.1",
"reselect": "^4.0.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
18 changes: 4 additions & 14 deletions src/components/article-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import Article from './article'
import accordionDecorator from '../decorators/accordion'
import { filtratedArticlesSelector } from '../selectors'

export class ArticleList extends Component {
static propTypes = {
Expand All @@ -15,6 +16,7 @@ export class ArticleList extends Component {
}

render() {
console.log('---', 'rendering article list')
return <ul>{this.items}</ul>
}

Expand All @@ -38,20 +40,8 @@ export class ArticleList extends Component {
}

export default connect((state) => {
const {
selected,
dateRange: { from, to }
} = state.filters

const filtratedArticles = state.articles.filter((article) => {
const published = Date.parse(article.date)
return (
(!selected.length ||
selected.find((selected) => selected.value === article.id)) &&
(!from || !to || (published > from && published < to))
)
})
console.log('---', 'article list connect')
return {
articles: filtratedArticles
articles: filtratedArticlesSelector(state)
}
})(accordionDecorator(ArticleList))
24 changes: 24 additions & 0 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createSelector } from 'reselect'

export const selectionSelector = (state) => state.filters.selected
export const dateRangeSelector = (state) => state.filters.dateRange
export const articleListSelector = (state) => state.articles

export const filtratedArticlesSelector = createSelector(
selectionSelector,
dateRangeSelector,
articleListSelector,
(selected, dateRange, articles) => {
console.log('---', 'article list selector')
const { from, to } = dateRange

return articles.filter((article) => {
const published = Date.parse(article.date)
return (
(!selected.length ||
selected.find((selected) => selected.value === article.id)) &&
(!from || !to || (published > from && published < to))
)
})
}
)

0 comments on commit b14d600

Please sign in to comment.