Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done HT3 #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/ac/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { INCREMENT, DELETE_ARTICLE } from '../constants'
import {
INCREMENT,
DELETE_ARTICLE,
FILTER_ARTICLE,
FILTER_DATE
} from '../constants'

export function increment() {
return {
Expand All @@ -12,3 +17,17 @@ export function deleteArticle(id) {
payload: { id }
}
}

export function filterArticle(payload) {
return {
type: FILTER_ARTICLE,
payload
}
}

export function filterDate(date) {
return {
type: FILTER_DATE,
payload: { date }
}
}
9 changes: 5 additions & 4 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 getArticles from '../selectors'

export class ArticleList extends Component {
static propTypes = {
Expand All @@ -20,6 +21,7 @@ export class ArticleList extends Component {

get items() {
const { articles, openItemId, toggleOpenItem } = this.props

return articles.map((article) => (
<li key={article.id} className="test--article-list__item">
<Article
Expand All @@ -33,12 +35,11 @@ export class ArticleList extends Component {

componentDidMount() {
const { fetchData } = this.props

fetchData && fetchData()
}
}

const ArticleListWithAccordion = accordionDecorator(ArticleList)

export default connect((state) => ({
articles: state.articles
}))(ArticleListWithAccordion)
articles: getArticles(state)
}))(accordionDecorator(ArticleList))
19 changes: 11 additions & 8 deletions src/components/filters/date-range.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import React, { Component } from 'react'
import DayPicker, { DateUtils } from 'react-day-picker'
import { filterDate } from '../../ac'
import { getDate } from '../../selectors'
import { connect } from 'react-redux'

import 'react-day-picker/lib/style.css'

class DateRange extends Component {
state = {
from: null,
to: null
}

handleDayClick = (day) =>
this.setState(DateUtils.addDayToRange(day, this.state))
this.props.filterDate(DateUtils.addDayToRange(day, this.props.date))

render() {
const { from, to } = this.state
const { from, to } = this.props.date

const selectedRange =
from && to && `${from.toDateString()} - ${to.toDateString()}`

return (
<div className="date-range">
<DayPicker
Expand All @@ -28,4 +28,7 @@ class DateRange extends Component {
}
}

export default DateRange
export default connect(
(state) => ({ date: getDate(state) }),
{ filterDate }
)(DateRange)
13 changes: 9 additions & 4 deletions src/components/filters/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import React, { Component } from 'react'
import DateRange from './date-range'
import SelectFilter from './select'
import { connect } from 'react-redux'

class Filters extends Component {
static propTypes = {}

render() {
return (
<div>
<SelectFilter articles={this.props.articles} />
<SelectFilter
articles={this.props.articles}
selected={this.props.filters.entities}
/>
<DateRange />
</div>
)
}
}

export default Filters
export default connect((state) => ({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Удобнее было на уровень ниже доставать

articles: state.articles,
filters: state.filters
}))(Filters)
26 changes: 19 additions & 7 deletions src/components/filters/select.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import React, { Component } from 'react'
import Select from 'react-select'
import { connect } from 'react-redux'
import { filterArticle } from '../../ac'

class SelectFilter extends Component {
state = {
selected: null
}
handleChange = (selected) => {
const { filterArticle } = this.props
const filtersArr = []

selected.map((item) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Этого совсем не понял

return filtersArr.push(item)
})

handleChange = (selected) => this.setState({ selected })
return filterArticle(filtersArr)
}

get options() {
return this.props.articles.map((article) => ({
const { articles } = this.props

return articles.map((article) => ({
label: article.title,
value: article.id
}))
Expand All @@ -19,12 +28,15 @@ class SelectFilter extends Component {
return (
<Select
options={this.options}
value={this.state.selected}
value={this.props.selected}
onChange={this.handleChange}
isMulti
/>
)
}
}

export default SelectFilter
export default connect(
null,
{ filterArticle }
)(SelectFilter)
2 changes: 2 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const INCREMENT = 'INCREMENT'

export const DELETE_ARTICLE = 'DELETE_ARTICLE'
export const FILTER_ARTICLE = 'FILTER_ARTICLE'
export const FILTER_DATE = 'FILTER_DATE'
2 changes: 1 addition & 1 deletion src/fixtures.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default [
{
id: '56c782f18990ecf954f6e027',
date: '2016-06-09T15:03:23.000Z',
date: '2018-10-22T15:03:23.000Z',
title: 'Commodo qui incididunt',
text:
'Commodo qui incididunt ex ut ea nulla et eu aliquip duis. Aute deserunt excepteur ullamco fugiat sunt aliquip exercitation do sint incididunt. Amet consectetur sint irure reprehenderit fugiat amet mollit. In commodo mollit ullamco cillum pariatur eiusmod cillum aute mollit. Culpa non sint eiusmod ad dolor velit dolore voluptate do adipisicing. Cupidatat sint est magna officia qui magna eu elit qui excepteur fugiat duis ex labore.\n\nAliquip veniam ad reprehenderit mollit exercitation id enim ut exercitation. Esse irure ipsum minim laborum reprehenderit irure ut. Tempor excepteur nisi nulla nostrud amet id cillum. Sint velit sint officia aliqua sint quis deserunt.\n\nAliquip dolor cillum deserunt enim nulla dolor amet irure cupidatat commodo laboris id aliqua. Labore aliqua adipisicing Lorem id adipisicing. Ad cupidatat et do anim ex commodo elit magna ad consequat. Nostrud sit eu laborum ut consequat fugiat aute culpa. Lorem tempor quis sunt ad consequat excepteur est. Enim voluptate cillum Lorem ex fugiat ea qui. Irure aute magna dolore eiusmod minim non ad anim dolore sint et.',
Expand Down
39 changes: 39 additions & 0 deletions src/reducer/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { FILTER_ARTICLE, DELETE_ARTICLE, FILTER_DATE } from '../constants'
const initialState = {
entities: [],
date: {
from: null,
to: null
}
}

export default (filtersState = initialState, action) => {
const { type, payload } = action

switch (type) {
case FILTER_ARTICLE:
return {
...filtersState,
entities: payload ? payload : initialState
}
case DELETE_ARTICLE:
return {
...filtersState,
entities: filtersState.entities.filter(
(item) => item.value !== payload.id
)
}
case FILTER_DATE:
const date = payload.date

return {
...filtersState,
date: {
from: date.from,
to: date.to
}
}
default:
return filtersState
}
}
8 changes: 5 additions & 3 deletions src/reducer/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { combineReducers } from 'redux'
import counterReducer from './couter'
import counter from './couter'
import articles from './articles'
import filters from './filters'

export default combineReducers({
counter: counterReducer,
articles
counter,
articles,
filters
})
1 change: 1 addition & 0 deletions src/selectors/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getDate = (state) => state.filters.date
32 changes: 32 additions & 0 deletions src/selectors/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default (state) => {
const { articles, filters } = state
let result

const filterDate = date => {
const { from } = filters.date
const { to } = filters.date
let _date

_date = new Date(date)

return (
(from ? _date >= filters.date.from : true) &&
(to ? _date <= filters.date.to : true)
)
}

if (!filters.entities.length && (!filters.date.from && !filters.date.to))
return articles

if (!filters.entities.length) {
result = articles.filter(({ date }) => filterDate(date))
} else {
const entities = filters.entities

result = articles
.filter(({ id }) => entities.some(({ value }) => id === value))
.filter(({ date }) => filterDate(date))
}

return result
}
3 changes: 3 additions & 0 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default } from './filters'

export { getDate } from './date'
12 changes: 7 additions & 5 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createStore } from 'redux'
import { createStore, compose } from 'redux'
import reducer from '../reducer'

const store = createStore(reducer)

//dev only, no need in prod!
window.store = store
const store = createStore(
reducer,
compose(
window.devToolsExtension ? window.__REDUX_DEVTOOLS_EXTENSION__() : (f) => f
)
)

export default store