Skip to content

hw3 done #31

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

Open
wants to merge 1 commit 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
17,473 changes: 17,473 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import React, { Component } from 'react'
import ArticleList from './components/article-list'
import Filters from './components/filters'
import UserForm from './components/user-form'
import Counter from './components/counter'

class App extends Component {
render() {
return (
<div>
<h1>Article App</h1>
<UserForm />
<Counter />
<Filters articles={[]} />
<ArticleList />
</div>
Expand Down
16 changes: 15 additions & 1 deletion src/ac/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DELETE_ARTICLE, INCREMENT } from '../constants'
import { DELETE_ARTICLE, INCREMENT, SELECT_ARTICLES, SET_RANGE } from '../constants'

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

export function selectArticles(payload) {
return {
type: SELECT_ARTICLES,
payload
}
}

export function setRange(payload) {
return {
type: SET_RANGE,
payload
}
}
17 changes: 16 additions & 1 deletion src/components/article-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ ArticleList.propTypes = {
openItemId: PropTypes.string
}

function filterArticles(articles, { range, selected }) {
let result = []
if (selected && selected.length) {
result = articles.filter((article) => selected.some((item) => article.id === item.value))
} else {
result = articles
}

if (range && range.from && range.to) {
result = result.filter((article) => article.date >= range.from && article.date <= range.to)
}

return result
}

export default connect((state) => ({
articles: state.articles
articles: filterArticles(state.articles, state.articlesFilter)
}))(accordion(ArticleList))
48 changes: 48 additions & 0 deletions src/components/comment-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { useState } from 'react'

function commentForm() {
Copy link
Owner

Choose a reason for hiding this comment

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

Названия компонент с большой буквы должны быть

const [author, setAuthor] = useState('')
const [comment, setComment] = useState('')
const [validationError, setValidationError] = useState('')
Copy link
Owner

Choose a reason for hiding this comment

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

Зачем отдельный стейт под ошибки?


function authorInputHandler({ target }) {
const { value } = target

setAuthor(value)
}

function commentInputHandler({ target }) {
const { value } = target

setComment(value)
}

function validateData(event) {
event.preventDefault()
setValidationError('')

if (comment.length < 5) {
setValidationError(`${validationError}Comment must be longer than 5 characters`)
}

if (author.length < 5) {
setValidationError(`${validationError}; author must be longer than 5 characters`)
}
}

return (
<section>
<h2>New comment</h2>
<form action="/">
<input type="text" onInput={authorInputHandler} placeholder="Author name" />
<div>
<textarea placeholder="Comment text" onInput={commentInputHandler} cols="30" rows="10" />
</div>
<div style={{ color: 'red' }}>{validationError}</div>
<button onClick={validateData}>submit</button>
</form>
</section>
)
}

export default commentForm
23 changes: 17 additions & 6 deletions src/components/filters/date-range.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import React, { useState } from 'react'
import React from 'react'
import { connect } from 'react-redux'
import DayPicker, { DateUtils } from 'react-day-picker'

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

function DateRange() {
const [state, setState] = useState({ from: null, to: null })
import { setRange } from '../../ac/index'

const handleDayClick = (day) => setState(DateUtils.addDayToRange(day, state))
function DateRange({ range, setRange }) {
const { from, to } = range

const handleDayClick = (day) => setRange(DateUtils.addDayToRange(day, range))

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

return (
Expand All @@ -22,4 +24,13 @@ function DateRange() {
)
}

export default DateRange
const mapStateToProps = (state) => {
return {
range: state.articlesFilter.range
}
}

export default connect(
mapStateToProps,
{ setRange }
)(DateRange)
23 changes: 18 additions & 5 deletions src/components/filters/select.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import React, { useState } from 'react'
import React from 'react'
import { connect } from 'react-redux'
import Select from 'react-select'

function SelectFilter({ articles }) {
const [selected, setSelection] = useState(null)
import { selectArticles } from '../../ac/index'

function SelectFilter({ articles, selectArticles, selected }) {
const options = articles.map((article) => ({
label: article.title,
value: article.id
}))

return <Select options={options} value={selected} onChange={setSelection} isMulti />
return <Select options={options} value={selected} onChange={selectArticles} isMulti />
}

export default SelectFilter
const mapStateToProps = (state) => ({
articles: state.articles,
selected: state.articlesFilter.selected
})

const mapDispatchToProps = {
selectArticles
}

export default connect(
mapStateToProps,
mapDispatchToProps
)(SelectFilter)
3 changes: 3 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const INCREMENT = 'INCREMENT'

export const DELETE_ARTICLE = 'DELETE_ARTICLE'

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

const initialState = {
range: {
from: null,
to: null
},
selected: null
}

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

switch (type) {
case SELECT_ARTICLES: {
return {
...filtersState,
selected: payload
}
}

case SET_RANGE: {
return {
...filtersState,
range: payload
}
}
default:
return filtersState
}
}
3 changes: 2 additions & 1 deletion src/reducer/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export default (articlesState = articles, action) => {
const { type, payload } = action

switch (type) {
case DELETE_ARTICLE:
case DELETE_ARTICLE: {
return articlesState.filter((article) => article.id !== payload.id)
}

default:
return articlesState
Expand Down
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 './counter'
import articles from './articles'
import articlesFilter from './articles-filter'

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