Skip to content
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
18 changes: 17 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, CHANGE_DATE_RANGE, CHANGE_SELECTION} from '../constants'
import { INCREMENT, DELETE_ARTICLE, CHANGE_DATE_RANGE, CHANGE_SELECTION, ADD_NEW_COMMENT, ADD_NEW_ID_COMMENT_TO_ARCTICLE } from '../constants'

export function increment() {
return {
Expand Down Expand Up @@ -26,3 +26,19 @@ export function changeSelection(selected) {
payload: { selected }
}
}


export function addNewComment(id, comment) {
return {
type: ADD_NEW_COMMENT,
payload: { id, comment }
}

}

export function addIdNewCommentToArticle(idarcticle) {
return {
type: ADD_NEW_ID_COMMENT_TO_ARCTICLE,
payload: { idarcticle }
}
}
46 changes: 35 additions & 11 deletions src/components/Article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import CommentList from '../CommentList'
import {deleteArticle} from '../../AC'
import './style.css'

import {createArticleSelector} from '../../selectors'

class Article extends PureComponent {
static propTypes = {
article: PropTypes.shape({
title: PropTypes.string.isRequired,
text: PropTypes.string,
comments: PropTypes.array
}).isRequired,
isOpen: PropTypes.bool,
toggleOpen: PropTypes.func
}
// static propTypes = {
// article: PropTypes.shape({
// title: PropTypes.string.isRequired,
// text: PropTypes.string,
// comments: PropTypes.array
// }).isRequired,
// isOpen: PropTypes.bool,
// toggleOpen: PropTypes.func
// }

constructor(props) {
super(props)
Expand All @@ -33,7 +35,7 @@ class Article extends PureComponent {
const body = isOpen && (
<div>
<section>{article.text}</section>
<CommentList comments = {article.comments} ref = {this.setCommentsRef} key = {this.state.count}/>
<CommentList idarticle = {article.id} comments = {article.comments} ref = {this.setCommentsRef} key = {this.state.count}/>
</div>
)
return (
Expand Down Expand Up @@ -84,6 +86,28 @@ class Article extends PureComponent {
}

}
const createMapStateToProps = (state, ownProps) =>{

const articleSelector = createArticleSelector()

return ( state, ownProps ) => {
return {
article: articleSelector(state , ownProps)
}
}


}

// const createMapStateToProps = () => {
// const commentSelector = createCommentSelector()
// return (state, ownProps) => {
// return {
// comment: commentSelector(state, ownProps)
// }
// }
// }


export default connect(null, { deleteArticle })(Article)
// export default connect(createMapStateToProps)(Article)
export default connect(createMapStateToProps, { deleteArticle })(Article)
21 changes: 15 additions & 6 deletions src/components/ArticleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ import {filtratedArticlesSelector} from '../selectors'

class ArticleList extends Accordion {
render() {
console.log('---', 'rerendering article list')




console.log('---', 'rerendering article list' )
const {articles} = this.props
console.log(articles)
if (!articles.length) return <h3>No Articles</h3>
const articleElements = articles.map((article) => <li key={article.id}>
<Article article={article}
const articleElements = Object.values(articles).map((article) => <li key={article.id}>
Copy link
Owner

Choose a reason for hiding this comment

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

Лучше селектор, который достанет из в виде массива


<Article
// article={article} /// это точно наверно не надо
id={article.id}
Copy link
Owner

Choose a reason for hiding this comment

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

не очень зачем это

isOpen={article.id === this.state.openItemId}
toggleOpen={this.toggleOpenItemMemoized(article.id)}
/>

</li>)
return (
<ul>
Expand All @@ -29,9 +38,9 @@ ArticleList.defaultProps = {
articles: []
}

ArticleList.propTypes = {
articles: PropTypes.array.isRequired
}
// ArticleList.propTypes = {
// articles: PropTypes.array.isRequired
// }

export default connect(state => {
console.log('---', 'connect updated')
Expand Down
16 changes: 15 additions & 1 deletion src/components/CommentForm/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React, { Component } from 'react'
import './style.css'

import {connect} from 'react-redux'
import {addNewComment, addIdNewCommentToArticle} from '../../AC'



class CommentForm extends Component {
static propTypes = {
};
Expand Down Expand Up @@ -30,6 +35,14 @@ class CommentForm extends Component {
user: '',
text: ''
})
const {addNewComment, addIdNewCommentToArticle , idarticle } = this.props // подключим нашу функцию из пропса конеекта

const id = "" // сделаем его пустым и подменим его в миделваре

addNewComment(id, { user: this.state.user, text: this.state.text } )
// console.log("idarticle" + idarticle )
addIdNewCommentToArticle( idarticle )

}

isValidForm = () => ['user', 'text'].every(this.isValidField)
Expand Down Expand Up @@ -58,4 +71,5 @@ const limits = {
}
}

export default CommentForm
// подключим к стору (редаксу)
export default connect(null, { addNewComment , addIdNewCommentToArticle })(CommentForm)
21 changes: 13 additions & 8 deletions src/components/CommentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,35 @@ class CommentList extends Component {
}

render() {
const {isOpen, toggleOpen} = this.props
const text = isOpen ? 'hide comments' : 'show comments'
const {isOpen, toggleOpen, idarticle } = this.props
const text = isOpen ? 'hide comments' : 'show comments'

return (
<div>
<button onClick={toggleOpen}>{text}</button>
{this.getBody()}
{this.getBody(idarticle)}
</div>
)
}

getBody() {
getBody(idarticle) {
const {comments, isOpen} = this.props
if (!isOpen) return null

const body = comments.length ? (
<ul>
{comments.map(id => <li key = {id}><Comment id = {id} /></li>)}
{comments.map(id => <li key = {id}><Comment id = {id} /></li>)}
</ul>
) : <h3>No comments yet</h3>

return (
<div>


// console.log("idarticle------------" + idarticle)
return (

<div>
{body}
<CommentForm />
<CommentForm idarticle = { idarticle} />
</div>
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Filters/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import 'react-select/dist/react-select.css'

class SelectFilter extends Component {
static propTypes = {
articles: PropTypes.array.isRequired
articles: PropTypes.object.isRequired
};

handleChange = selected => this.props.changeSelection(selected.map(option => option.value))

render() {
const { articles, selected } = this.props
const options = articles.map(article => ({
const options = Object.values(articles).map(article => ({
label: article.title,
value: article.id
}))
Expand Down
8 changes: 7 additions & 1 deletion src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ export const INCREMENT = 'INCREMENT'
export const DELETE_ARTICLE = 'DELETE_ARTICLE'

export const CHANGE_SELECTION = 'CHANGE_SELECTION'
export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE'
export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE'



export const ADD_NEW_COMMENT = 'ADD_NEW_COMMENT'

export const ADD_NEW_ID_COMMENT_TO_ARCTICLE = "ADD_NEW_ID_COMMENT_TO_ARCTICLE" // это врятли получится
26 changes: 26 additions & 0 deletions src/middlewares/newIdComment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ADD_NEW_COMMENT } from '../constants'


export default store => next => action => {
console.log('--- state before: ', store.getState())
console.log('--- action: ', action)

function randId() {
return (Math.random() * 1e32).toString(36);
}

// console.log(randId()) // для теста


if (action.type === ADD_NEW_COMMENT) {
Copy link
Owner

Choose a reason for hiding this comment

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

через мидлвары будет проходить каждый экшин, они должны быть максимально общими, завязывать на конкретные экшины - не лучшее решение


console.log("new id" + randId())

action.payload.id = randId()
Copy link
Owner

Choose a reason for hiding this comment

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

лучше не мутировать payload, мало-ли что там станут передавать

}
console.log('--- new pay ac', action)


next(action)
console.log('--- state after: ', store.getState())
}
95 changes: 89 additions & 6 deletions src/reducer/articles.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,96 @@
import { DELETE_ARTICLE } from '../constants'
import {normalizedArticles as defaultArticles} from '../fixtures'
import { DELETE_ARTICLE, ADD_NEW_ID_COMMENT_TO_ARCTICLE } from '../constants'
import { normalizedArticles as defaultArticles } from '../fixtures'


const articleMap = defaultArticles.reduce((acc, article) => ({
...acc,
[article.id]: article
}), {})

export default (articlesState = defaultArticles, action) => {
const { type, payload } = action

export default (articlesState = articleMap, action) => {
const { type, payload } = action
/// исправить потом
switch (type) {
case DELETE_ARTICLE:
return articlesState.filter(article => article.id !== payload.id)

const articles = Object.keys(articlesState).reduce((obj, key) => {
if (key !== payload.id) return {...obj, [key]: articlesState[key] }
return obj
}, {})

return articles
case ADD_NEW_ID_COMMENT_TO_ARCTICLE:
console.log(" тут будет логика добавления ")
// console.log(" тут произошло обновление " + payload.idarcticle)
const articleid = payload.idarcticle;
// console.log(articlesState[id])
// console.log(store)
// console.log(store.getState().comments)
// console.log( Object.key(store.getState().comments).reduce( ( obj ) ) )
console.log(Object.values(store.getState().comments).length)
Copy link
Owner

Choose a reason for hiding this comment

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

Не надо в редюсерах к стору обращаться. Все необходимое у тебя в аргументах

const l = Object.values(store.getState().comments).length;

console.log(Object.values(store.getState().comments)[l - 1])


const newIdComment = Object.values(store.getState().comments)[l - 1].id
console.log(newIdComment)


const art = Object.keys(articlesState).reduce((obj, key) => {


if (key == articleid) {
console.log(articlesState[key].comments)
articlesState[key].comments.push(newIdComment)
console.log(articlesState[key].comments)
return {...obj, [key]: articlesState[key] }
}
if (key !== articleid) return {...obj, [key]: articlesState[key] }

}, {})

return art






// Object.values(store.getState().comments).forEach(element => {
// console.log(" zlo" + element)
// });







// return articlesState.filter(article => article.id !== payload.id)
}

return articlesState
}
}





// import {normalizedComments as defaultComments} from '../fixtures'

// const commentsMap = defaultComments.reduce((acc, comment) => ({
// ...acc,
// [comment.id]: comment
// }), {})

// export default (state = commentsMap, action) => {
// const { type } = action

// switch (type) {

// }

// return state
// }
Loading