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 #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

ht3 #30

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,802 changes: 3,408 additions & 3,394 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/ac/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { INCREMENT, DELETE_ARTICLE } from '../constants'
import { INCREMENT, DELETE_ARTICLE, FILTER_SELECT } from '../constants'

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

export function filterSelect(select) {
return {
type: FILTER_SELECT,
payload: { select }
}
}
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
4 changes: 3 additions & 1 deletion src/components/article-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ export class ArticleList extends Component {
const ArticleListWithAccordion = accordionDecorator(ArticleList)

export default connect((state) => ({
articles: state.articles
articles: state.articles.articlesVisable.length
? state.articles.articlesVisable
: state.articles.articlesState
}))(ArticleListWithAccordion)
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
20 changes: 14 additions & 6 deletions src/components/filters/select.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { Component } from 'react'
import Select from 'react-select'
import { connect } from 'react-redux'
import { filterSelect } from '../../ac'

class SelectFilter extends Component {
state = {
selected: null
handleChange = (selected) => {
this.props.filterSelect({ selected })
}

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

get options() {
return this.props.articles.map((article) => ({
label: article.title,
Expand All @@ -19,12 +19,20 @@ 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.articlesState,
selected: state.articles.articlesFilter.length
? state.articles.articlesFilter
: null
}),
{ filterSelect }
)(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_SELECT = 'FILTER_SELECT'
46 changes: 41 additions & 5 deletions src/reducer/articles.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
import defaultArticles from '../fixtures'
import { DELETE_ARTICLE } from '../constants'
import { DELETE_ARTICLE, FILTER_SELECT } from '../constants'

export default (articlesState = defaultArticles, action) => {
export default (
Copy link
Owner

Choose a reason for hiding this comment

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

Лучше храни отдельно статьи и отдельно значения фильтров. Саму фильтрацию можешь потом сделать в коннекте, при необходимости. Старайся хранить минимальное состояние в сторе. Все, что можно посчитать - считай там, где это нужно

articleObj = {
articlesState: defaultArticles,
articlesVisable: {},
articlesFilter: {}
Copy link
Owner

Choose a reason for hiding this comment

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

Лучше храни отдельно статьи и отдельно значения фильтров. Саму фильтрацию можешь потом сделать в коннекте, при необходимости. Старайся хранить минимальное состояние в сторе. Все, что можно посчитать - считай там, где это нужно

},
action
) => {
const { type, payload } = action

switch (type) {
case DELETE_ARTICLE:
return articlesState.filter((article) => article.id !== payload.id)
case DELETE_ARTICLE: {
let item_article = articleObj.articlesState.filter(
(article) => article.id !== payload.id
)
let item_visable = articleObj.articlesVisable.filter(
(article) => article.id !== payload.id
)
let item_filter = articleObj.articlesFilter.filter(
(article) => article.value !== payload.id
)
articleObj = {
...articleObj,
articlesState: item_article,
articlesVisable: item_visable,
articlesFilter: item_filter
}
return articleObj
}

case FILTER_SELECT: {
let item_id = new Set(payload.select.selected.map((item) => item.value))
let item_visable = articleObj.articlesState.filter((article) =>
item_id.has(article.id)
)
articleObj = {
...articleObj,
articlesVisable: item_visable,
articlesFilter: payload.select.selected
}
return articleObj
}

default:
return articlesState
return articleObj
}
}