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
22 changes: 21 additions & 1 deletion src/AC/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
INCREMENT, DELETE_ARTICLE, CHANGE_DATE_RANGE, CHANGE_SELECTION, ADD_COMMENT, LOAD_ALL_ARTICLES, LOAD_ARTICLE,
LOAD_ARTICLE_COMMENTS, START, SUCCESS, FAIL
LOAD_ARTICLE_COMMENTS, START, SUCCESS, FAIL, LOAD_COMMENT
} from '../constants'

export function increment() {
Expand Down Expand Up @@ -82,4 +82,24 @@ export function loadArticleComments(articleId) {
payload: { articleId },
callAPI: `/api/comment?article=${articleId}`
}
}

export function loadCommentOnNumber(commentNumber) {
return (dispatch) => {
dispatch({
type: LOAD_COMMENT + START,
payload: { commentNumber }
})

fetch(`/api/comment?limit=1&offset=${commentNumber}`)
.then(res => res.json())
.then(response => dispatch({
type: LOAD_COMMENT + SUCCESS,
payload: { commentNumber, response }
}))
.catch(error => dispatch({
type: LOAD_COMMENT + FAIL,
payload: { commentNumber, error }
}))
}
}
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react'
import {findDOMNode} from 'react-dom'
import {Route, NavLink, Switch} from 'react-router-dom'
import ArticlesPage from './components/routes/articles'
import CommentRoute from './components/routes/comment'
import UserForm from './components/user-form'
import Filters from './components/filters'
import Counter from './components/counter'
Expand All @@ -25,6 +26,7 @@ class App extends Component {
<Route path = "/filters" component = {Filters} />
<Route path = "/articles/new" render = {() => <h1>New Article Form</h1>} />
<Route path = "/articles" component = {ArticlesPage} />
<Route path = "/comment/:commentNumber" component = {CommentRoute}/>
<Route path = "*" render = {() => <h1>Not Found Page</h1>} />
</Switch>
</div>
Expand Down
8 changes: 6 additions & 2 deletions src/components/comment-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ class CommentList extends Component {
}

getComments() {
const {comments} = this.props;

return (
<ul>
{
this.props.article.comments.map(id =>
<li key = {id} className = "test__comment-list--item">
<Comment id = {id}/>
<Comment id = {id} comment={comments.get(id)}/>
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

У тебя Comment сам это умеет делать

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Хотел сделать чтобы этот компонент работал как в Article (где комент берется по id), так и отдельно (когда нужно с общего списка комментов брать комент (по его номеру)), поэтому передаю соmment с родителя

</li>)
}
</ul>
Expand All @@ -77,4 +79,6 @@ class CommentList extends Component {
}


export default connect(null, { loadArticleComments })(toggleOpen(CommentList))
export default connect(state => ({
comments: state.comments.entities
}), { loadArticleComments })(toggleOpen(CommentList))
11 changes: 1 addition & 10 deletions src/components/comment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { createCommentSelector } from '../selectors'

function Comment({comment}) {
return (
Expand All @@ -18,12 +16,5 @@ Comment.propTypes = {
}).isRequired
}

const createMapStateToProps = () => {
const commentSelector = createCommentSelector()

return (state, ownProps) => ({
comment: commentSelector(state, ownProps)
})
}

export default connect(createMapStateToProps)(Comment)
export default Comment
49 changes: 49 additions & 0 deletions src/components/routes/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Route } from 'react-router-dom'
import Comment from '../comment';
import { loadCommentOnNumber } from '../../AC'
import { oneCommentSelector } from '../../selectors';

class CommentRoute extends Component {
static propTypes = {
loadCommentOnNumber: PropTypes.func,
match: PropTypes.object
}

componentWillReceiveProps(nextProps) {
console.log('nextProps', nextProps);
}

componentDidMount() {
const { match, loadCommentOnNumber, comment } = this.props;

if (match && !comment) {
console.log(match.params.commentNumber);
loadCommentOnNumber(match.params.commentNumber);
}
}

render() {
const { match, comment } = this.props;

if (!match) return <h2>Please select an number comment</h2>
if (!comment) return null;

if (comment) {
return <Comment comment={comment}/>
}
}
}


const createMapStateToProps = () => {
const commentSelector = oneCommentSelector()

return (state, ownProps) => ({
comment: commentSelector(state, ownProps.match.params)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Зачем это?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Чтобы взять коммент по его номеру (1, 2, 3, ...) c общего списка комментов

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Понял, неправильно архитектурно реализовал

})
}

export default connect(createMapStateToProps, { loadCommentOnNumber })(CommentRoute);
1 change: 1 addition & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const DELETE_ARTICLE = 'DELETE_ARTICLE'
export const LOAD_ALL_ARTICLES = 'LOAD_ALL_ARTICLES'
export const LOAD_ARTICLE = 'LOAD_ARTICLE'
export const LOAD_ARTICLE_COMMENTS = 'LOAD_ARTICLE_COMMENTS'
export const LOAD_COMMENT = 'LOAD_COMMENT'

export const CHANGE_SELECTION = 'CHANGE_SELECTION'
export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE'
Expand Down
8 changes: 6 additions & 2 deletions src/reducer/comments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ADD_COMMENT, LOAD_ARTICLE_COMMENTS, SUCCESS } from '../constants'
import { ADD_COMMENT, LOAD_ARTICLE_COMMENTS, SUCCESS, LOAD_COMMENT } from '../constants'
import {Record, OrderedMap} from 'immutable'
import {arrToMap} from './utils'

Expand All @@ -9,7 +9,8 @@ const CommentRecord = Record({
})

const ReducerState = Record({
entities: new OrderedMap({})
entities: new OrderedMap({}),
commentsList: new OrderedMap({}),
})

export default (state = new ReducerState(), action) => {
Expand All @@ -25,6 +26,9 @@ export default (state = new ReducerState(), action) => {
case LOAD_ARTICLE_COMMENTS + SUCCESS:
return state.mergeIn(['entities'], arrToMap(response, CommentRecord))

case LOAD_COMMENT + SUCCESS:
return state.setIn(['commentsList', payload.commentNumber], payload.response.records[0]);

default:
return state
}
Expand Down
7 changes: 6 additions & 1 deletion src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export const loadedArticlesSelector = state => state.articles.loaded
const commentsSelector = state => state.comments.entities
const filtersSelector = state => state.filters
const idSelector = (_, props) => props.id
const onlyCommentListSelector = state => state.comments.commentsList
const commentNumberSelector = (_, props) => props.commentNumber

export const filtratedArticles = createSelector(articleListSelector, filtersSelector, (articles, filters) => {
const {selected, dateRange: {from, to}} = filters
Expand All @@ -22,4 +24,7 @@ export const filtratedArticles = createSelector(articleListSelector, filtersSele

export const articleSelector = createSelector(articlesMapSelector, idSelector, (articles, id) => articles.get(id))

export const createCommentSelector = () => createSelector(commentsSelector, idSelector, (comments, id) => comments.get(id))
export const createCommentSelector = () => createSelector(commentsSelector, idSelector, (comments, id) => comments.get(id))

export const oneCommentSelector = () => createSelector(onlyCommentListSelector, commentNumberSelector,
(comments, number) => comments.get(number))
Loading