-
Notifications
You must be signed in to change notification settings - Fork 17
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
Hw03 #35
base: master
Are you sure you want to change the base?
Hw03 #35
Changes from all commits
eec9a02
daffab9
f5deffe
25cda0e
effc359
6557897
37bea1c
f439dc6
080018e
843d182
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,39 @@ export class ArticleList extends Component { | |
|
||
const ArticleListWithAccordion = accordionDecorator(ArticleList) | ||
|
||
export default connect((state) => ({ | ||
articles: state.articles | ||
}))(ArticleListWithAccordion) | ||
function mapStateToProps(state) { | ||
const articles = state.articles | ||
const filters = state.filters | ||
|
||
const idFilter = filters.ids.map((item) => item.value) | ||
console.log(' --- ids: ' + idFilter) | ||
|
||
const dateFilterFrom = | ||
filters.dates.from && new Date(filters.dates.from).getTime() | ||
const dateFilterTo = filters.dates.to && new Date(filters.dates.to).getTime() | ||
console.log( | ||
' --- date from: ' + filters.dates.from + ' date to: ' + filters.dates.to | ||
) | ||
|
||
const noDateFilter = !dateFilterFrom && !dateFilterTo | ||
|
||
const oneDate = | ||
dateFilterFrom && !dateFilterTo | ||
? new Date(dateFilterFrom).setHours(0, 0, 0, 0) | ||
: null | ||
|
||
return { | ||
articles: articles.filter((article) => { | ||
const idCheck = !idFilter.length || idFilter.includes(article.id) | ||
const date = article.date && new Date(article.date).getTime() | ||
const oneDateCheck = | ||
oneDate && new Date(date).setHours(0, 0, 0, 0) === oneDate | ||
const dateRangeCheck = date >= dateFilterFrom && date <= dateFilterTo | ||
|
||
return idCheck && (noDateFilter || oneDateCheck || dateRangeCheck) | ||
}), | ||
filters: state.filters | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а зачем там filters? |
||
} | ||
} | ||
|
||
export default connect(mapStateToProps)(ArticleListWithAccordion) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import React, { Component } from 'react' | ||
import { connect } from 'react-redux' | ||
import DateRange from './date-range' | ||
import SelectFilter from './select' | ||
|
||
|
@@ -8,11 +9,19 @@ class Filters extends Component { | |
render() { | ||
return ( | ||
<div> | ||
<SelectFilter articles={this.props.articles} /> | ||
<DateRange /> | ||
<SelectFilter | ||
articles={this.props.articles} | ||
selected={this.props.filters.id} | ||
/> | ||
<DateRange filter={this.props.filters.dates} /> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default Filters | ||
const mapStateToProps = (state) => ({ | ||
articles: state.articles, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. удобнее уровнем ниже достать There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. удобнее уровнем ниже достать |
||
filters: state.filters | ||
}) | ||
|
||
export default connect(mapStateToProps)(Filters) |
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 FILTER_ID = 'FILTER_ID' | ||
|
||
export const FILTER_DATE = 'FILTER_DATE' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { FILTER_ID, FILTER_DATE } from '../constants' | ||
|
||
const defFilterState = { | ||
ids: [], | ||
dates: { | ||
from: null, | ||
to: null | ||
} | ||
} | ||
|
||
export default (filtersState = defFilterState, action) => { | ||
const { type, payload } = action | ||
|
||
switch (type) { | ||
case FILTER_ID: | ||
return { | ||
...filtersState, | ||
ids: payload.values ? payload.values : defFilterState.ids | ||
} | ||
|
||
case FILTER_DATE: | ||
const range = payload.range || {} | ||
return { | ||
...filtersState, | ||
dates: { | ||
from: range.from ? range.from : defFilterState.dates.from, | ||
to: range.to ? range.to : defFilterState.dates.to | ||
} | ||
} | ||
|
||
default: | ||
// return articlesState | ||
return filtersState | ||
} | ||
} |
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 filters from './filters' | ||
|
||
export default combineReducers({ | ||
counter: counterReducer, | ||
articles | ||
articles, | ||
filters | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а зачем там filters?