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

Ht3 #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Ht3 #32

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
6,926 changes: 3,463 additions & 3,463 deletions package-lock.json

Large diffs are not rendered by default.

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,
SET_FILTER_SELECT,
SET_FILTER_DATE_RANGE
} from '../constants'

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

export function setSelect(selected) {
return {
type: SET_FILTER_SELECT,
payload: { selected }
}
}

export function setDateRange(dateRange) {
return {
type: SET_FILTER_DATE_RANGE,
payload: { dateRange }
}
}
2 changes: 1 addition & 1 deletion src/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class App extends Component {
<div>
<UserForm />
<Counter />
<Filters articles={[]} />
<Filters />
<ArticleList />
</div>
)
Expand Down
24 changes: 23 additions & 1 deletion src/components/article-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ export class ArticleList extends Component {

const ArticleListWithAccordion = accordionDecorator(ArticleList)

const filterArticle = (state) => {
return state.articles.filter((article) => {
const { selected, dateRange } = state.filter

if (
selected &&
selected.length &&
selected.map((item) => item.value).indexOf(article.id) === -1
) {
return false
}

const articleDate = new Date(article.date)

if (dateRange.from && articleDate <= dateRange.from) {
return false
}

return !(dateRange.to && articleDate >= dateRange.to)
})
}

export default connect((state) => ({
articles: state.articles
articles: filterArticle(state)
}))(ArticleListWithAccordion)
26 changes: 19 additions & 7 deletions src/components/filters/date-range.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import React, { Component } from 'react'
import DayPicker, { DateUtils } from 'react-day-picker'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'

import { setDateRange } from '../../ac'

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

class DateRange extends Component {
state = {
from: null,
to: null
static propTypes = {
dateRange: PropTypes.shape({
from: PropTypes.instanceOf(Date),
to: PropTypes.instanceOf(Date)
})
}

handleDayClick = (day) =>
this.setState(DateUtils.addDayToRange(day, this.state))
handleDayClick = (day) => {
this.props.setDateRange(DateUtils.addDayToRange(day, this.props.dateRange))
}

render() {
const { from, to } = this.state
const { from, to } = this.props.dateRange
const selectedRange =
from && to && `${from.toDateString()} - ${to.toDateString()}`
return (
Expand All @@ -28,4 +35,9 @@ class DateRange extends Component {
}
}

export default DateRange
export default connect(
(state) => ({
dateRange: state.filter.dateRange
}),
{ setDateRange }
)(DateRange)
2 changes: 1 addition & 1 deletion src/components/filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Filters extends Component {
render() {
return (
<div>
<SelectFilter articles={this.props.articles} />
<SelectFilter />
<DateRange />
</div>
)
Expand Down
26 changes: 21 additions & 5 deletions src/components/filters/select.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import React, { Component } from 'react'
import Select from 'react-select'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { setSelect } from '../../ac'

class SelectFilter extends Component {
state = {
selected: null
static propTypes = {
selected: PropTypes.arrayOf(
PropTypes.shape({
label: PropTypes.string.required,
value: PropTypes.string.required
})
)
}

handleChange = (selected) => this.setState({ selected })
handleChange = (selected) => {
this.props.setSelect(selected)
}

get options() {
return this.props.articles.map((article) => ({
Expand All @@ -19,12 +29,18 @@ 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(
(state) => ({
articles: state.articles,
selected: state.filter.selected
}),
{ setSelect }
)(SelectFilter)
4 changes: 4 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const INCREMENT = 'INCREMENT'

export const DELETE_ARTICLE = 'DELETE_ARTICLE'

export const SET_FILTER_SELECT = 'SET_FILTER_SELECT'

export const SET_FILTER_DATE_RANGE = 'SET_FILTER_DATE_RANGE'
24 changes: 24 additions & 0 deletions src/reducer/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { SET_FILTER_SELECT, SET_FILTER_DATE_RANGE } from '../constants'

const initialFilter = {
selected: null,
dateRange: {
from: null,
to: null
}
}

export default (filterState = initialFilter, action) => {
const { type, payload } = action

switch (type) {
case SET_FILTER_SELECT:
return { ...filterState, selected: payload.selected }

case SET_FILTER_DATE_RANGE:
return { ...filterState, dateRange: payload.dateRange }

default:
return filterState
}
}
4 changes: 3 additions & 1 deletion 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 articles from './articles'
import filter from './filter'

export default combineReducers({
counter: counterReducer,
articles
articles,
filter
})