Skip to content
Open

HT6 #56

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: 20 additions & 2 deletions src/AC/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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, LOAD_ALL_COMMENTS, LOAD_PAGE_COMMENTS,
START, SUCCESS, FAIL, PER_PAGE
} from '../constants'

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

export function loadAllComments() {
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.

это зачем?

return {
type: LOAD_ALL_COMMENTS,
payload: {},
callAPI: `/api/comment`
}
}

export function loadPageComments(no) {
const limit = PER_PAGE, offset = (no-1)*limit;
return {
type: LOAD_PAGE_COMMENTS,
payload: {no},
callAPI: `/api/comment?limit=${limit}&offset=${offset}`
}
}
6 changes: 5 additions & 1 deletion 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 CommentsPage from './components/routes/comments'
import UserForm from './components/user-form'
import Filters from './components/filters'
import Counter from './components/counter'
Expand All @@ -19,17 +20,20 @@ class App extends Component {
<li><NavLink to = "/counter" activeStyle = {{ color: 'red' }}>counter</NavLink></li>
<li><NavLink to = "/filters" activeStyle = {{ color: 'red' }}>filters</NavLink></li>
<li><NavLink to = "/articles" activeStyle = {{ color: 'red' }}>articles</NavLink></li>
<li><NavLink to = "/comments" activeStyle = {{ color: 'red' }}>comments</NavLink></li>
</ul>
<Switch>
<Route path = "/counter" component = {Counter} exact />
<Route path = "/filters" component = {Filters} />
<Route path = "/articles/new" render = {() => <h1>New Article Form</h1>} />
<Route path = "/articles" component = {ArticlesPage} />
<Route path = "/comments/:id" component = {CommentsPage} />
<Route path = "/comments" component = {CommentsPage} />
<Route path = "*" render = {() => <h1>Not Found Page</h1>} />
</Switch>
</div>
)
}
}

export default App
export default App
75 changes: 75 additions & 0 deletions src/components/routes/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React, { Component } from 'react'
import {connect} from 'react-redux'
import { NavLink, Route } from 'react-router-dom'
import Comment from '../comment'
import Loader from '../loader'
import { commentPagesSelector, commentsListSelector, commentsTotalSelector } from '../../selectors'
import { PER_PAGE } from '../../constants'
import { loadAllComments, loadPageComments } from '../../AC'

class CommentsRoute extends Component {
static propTypes = {

};

componentDidMount() {
const { loadAllComments, loaded} = this.props
if (!loaded) loadPageComments()
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

@mas-student mas-student Mar 19, 2018

Choose a reason for hiding this comment

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

Работает.
loadAllComments надо убрать.

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.

if (!loaded) loadPageComments() лишний.
Нужный находится ниже

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.

И неправильный к тому же. Правильный loadPageComments(no)

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.

То есть, конечно, конкретно этот кусок кода не работает. Работает в целом. Это ошибка.

}

render() {
const { pages, total} = this.props
const pageCount = Math.ceil(total / PER_PAGE);
const range = Array.apply(null, Array(pageCount)).map(function (_, i) {return i+1;});
const pageElements = range.map(no =>
<li><NavLink to = {`/comments/${no}`} >{no}</NavLink></li>
)
return (
<div>
<h2>Comments page</h2>
{pageElements}

{this.getPage()}
</div>
)
}

getPage() {
const {match, loadPageComments} = this.props;
const no = match.params && match.params.id || '1';
const { pages } = this.props
const page = pages.get(no)
const { loaded, loading, comments } = page || {}

if (loading)
return <Loader />

if (!loaded) {
loadPageComments(no)
return <div></div>
}
return <div> {this.getComments(comments)} </div>
}

getComments(comments) {
return (
<ul>
{
comments.map(id =>
<li key = {id} className = "test__comment-list--item">
<Comment id = {id}/>
</li>)
}
</ul>
)
}

}

const createMapStateToProps = (state) => ({
comments: commentsListSelector(state),
pages: commentPagesSelector(state),
total: commentsTotalSelector(state),
})

export default connect(createMapStateToProps, {loadAllComments, loadPageComments})(CommentsRoute)
8 changes: 7 additions & 1 deletion src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ 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_PAGE_COMMENTS = 'LOAD_PAGE_COMMENTS'

export const LOAD_ALL_COMMENTS = 'LOAD_ALL_COMMENTS'

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

export const ADD_COMMENT = 'ADD_COMMENT'

export const START = '_START'
export const SUCCESS = '_SUCCESS'
export const FAIL = '_FAIL'
export const FAIL = '_FAIL'

export const PER_PAGE = 5
35 changes: 31 additions & 4 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, LOAD_PAGE_COMMENTS, START, SUCCESS } from '../constants'
import {Record, OrderedMap} from 'immutable'
import {arrToMap} from './utils'

Expand All @@ -8,12 +8,21 @@ const CommentRecord = Record({
user: null
})

const PageRecord = Record({
no: null,
comments: [],
loading: false,
loaded: false
})

const ReducerState = Record({
entities: new OrderedMap({})
entities: new OrderedMap({}),
pages: new OrderedMap({}),
total: 0
})

export default (state = new ReducerState(), action) => {
const { type, payload, randomId, response } = action
const { type, payload, randomId, response} = action

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

case LOAD_PAGE_COMMENTS + START:
return state
.setIn(['pages', payload.no], new PageRecord( {
no: payload.no,
comment: [],
loading: true,
loaded: false }
))

case LOAD_PAGE_COMMENTS + SUCCESS:
console.log('LOAD_PAGE_COMMENTS + SUCCESS', response);
return state
.mergeIn(['entities'], arrToMap(response.records, CommentRecord))
.setIn(['pages', payload.no, 'comments'], response.records.map(comment => comment.id))
.setIn(['total'], response.total)
.setIn(['pages', payload.no, 'loading'], false)
.setIn(['pages', payload.no, 'loaded'], true)

default:
return state
}
}
}
11 changes: 9 additions & 2 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ export const articleListSelector = createSelector(articlesMapSelector, articlesM
export const loadingArticlesSelector = state => state.articles.loading
export const loadedArticlesSelector = state => state.articles.loaded

const commentsSelector = state => state.comments.entities
export const commentPagesSelector = state => state.comments.pages

export const commentsSelector = state => state.comments.entities
export const commentsMapSelector = state => state.comments.entities
export const commentsListSelector = createSelector(commentsMapSelector, commentsMap => commentsMap.valueSeq().toArray())
export const commentsTotalSelector = state => state.comments.total


const filtersSelector = state => state.filters
const idSelector = (_, props) => props.id

Expand All @@ -22,4 +29,4 @@ 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))