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
12 changes: 10 additions & 2 deletions 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
INCREMENT, DELETE_ARTICLE, CHANGE_DATE_RANGE, CHANGE_SELECTION, ADD_COMMENT, LOAD_ALL_ARTICLES, LOAD_ARTICLE, LOAD_COMMENTS,
LOAD_ARTICLE_COMMENTS, LIMIT, START, SUCCESS, FAIL
} from '../constants'

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

export function loadCommentsList(i = 1) {
return {
type: LOAD_COMMENTS,
payload: { i },
callAPI: `/api/comment?limit=${LIMIT * i}&offset=${LIMIT * ++i}`
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.

++i так себе идея

}
}
5 changes: 4 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 CommentsLists from './components/routes/comments'
import UserForm from './components/user-form'
import Filters from './components/filters'
import Counter from './components/counter'
Expand All @@ -25,11 +26,13 @@ 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 = "/comments" component = {CommentsLists} />
<Route path = "*" render = {() => <h1>Not Found Page</h1>} />
</Switch>
</div>
)
}
}

export default App
export default App
//export default withRouter(connect(mapStateToProps)(App))
63 changes: 63 additions & 0 deletions src/components/routes/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { Component } from 'react'
import { Route, NavLink } from 'react-router-dom'
import { connect } from 'react-redux'
import Loader from '../loader'
import { commentListSelector } from '../../selectors'
import { loadCommentsList } from '../../AC'
import { LIMIT } from '../../constants'

class CommentsRoute extends Component {
componentDidMount(){
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.

вызывается только раз

const {loadCommentsList} = this.props
loadCommentsList()
}
componentWillReceiveProps(){
//console.log("need rerender!")
}
render(){
const {total} = this.props

return (
<div>
{ this.getLinks(total) }
<Route {...this.props} path = {`${this.props.match.path}/:list`} children = {this.getComments}/>
</div>
)
}

getComments = ({ match }) => {
if (!match) return <h3>Select comments list </h3>
const {loadedComments} = this.props
if (!loadedComments) return <Loader/>
const list = loadedComments[match.params.list - 1]
if ( !list || !list.length) return <Loader/>
const commentElement = list.map( comment => {
return(
<div key={comment.id}>
<b>{comment.user}</b>: {comment.text}
</div>
)
})
return <section>{ commentElement }</section>
}

getLinks = (total) => {
if (!total) return <Loader/>
const count = Math.floor(total/ LIMIT) +( total % LIMIT === 0 ? 0 : 1)
const links =(Array.from(Array(count).keys())).map( i => {
const p = i + 1
return <li key = {i}>
<NavLink to={`${this.props.match.path}/${p}`}>{p}</NavLink>
</li>
})
return <ul>
{links}
</ul>
}
}


export default connect(state => ({
loadedComments: commentListSelector(state),
total: state.comments.total
}), { loadCommentsList })(CommentsRoute)
2 changes: 2 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const CHANGE_SELECTION = 'CHANGE_SELECTION'
export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE'

export const ADD_COMMENT = 'ADD_COMMENT'
export const LOAD_COMMENTS = 'LOAD_COMMENTS'
export const LIMIT = 5

export const START = '_START'
export const SUCCESS = '_SUCCESS'
Expand Down
16 changes: 14 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, LOAD_COMMENTS, START, SUCCESS } from '../constants'
import {Record, OrderedMap} from 'immutable'
import {arrToMap} from './utils'

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

const ReducerState = Record({
entities: new OrderedMap({})
entities: new OrderedMap({}),
loadedComments: [],
total: null
})

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

case LOAD_COMMENTS + START:
let item = state.loadedComments[payload.i - 1]
return typeof item === 'undefined' ? state.setIn(['loadedComments', payload.i - 1], false) : state

case LOAD_COMMENTS + SUCCESS:
console.log("SUCCESS load comments: ", response)

return state.set('total', response.total)
.setIn(['loadedComments', payload.i - 1], response.records)//arrToMap(response.records, CommentRecord))
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.

Не хочешь records тоже immutable хранить, как остальные комменты?


default:
return state
}
Expand Down
1 change: 1 addition & 0 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const loadedArticlesSelector = state => state.articles.loaded
const commentsSelector = state => state.comments.entities
const filtersSelector = state => state.filters
const idSelector = (_, props) => props.id
export const commentListSelector = state => state.comments.get('loadedComments')

export const filtratedArticles = createSelector(articleListSelector, filtersSelector, (articles, filters) => {
const {selected, dateRange: {from, to}} = filters
Expand Down