diff --git a/src/AC/index.js b/src/AC/index.js index b6fc854..e9e7537 100644 --- a/src/AC/index.js +++ b/src/AC/index.js @@ -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_ALL_ARTICLES, LOAD_ARTICLE, LOAD_ARTICLE_COMMENTS, START, SUCCESS, FAIL, + LOAD_COMMENTS_PAGE } from '../constants' export function increment() { @@ -73,4 +74,11 @@ export function loadArticleComments(articleId) { payload: { articleId }, callAPI: `/api/comment?article=${articleId}` } +} + +export function loadCommentsPage(url) { + return { + type: LOAD_COMMENTS_PAGE, + callAPI: url, + } } \ No newline at end of file diff --git a/src/components/App.js b/src/components/App.js index 64c1c09..6ffdc18 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -3,6 +3,7 @@ import ArticleListPage from './routes/ArticleList' import UserForm from './UserForm' import FiltersPage from './routes/Filters' import CounterPage from './routes/Counter' +import CommentsPage from './routes/CommentsPagination' import { Route, Switch, NavLink } from 'react-router-dom' class App extends Component { @@ -14,14 +15,16 @@ class App extends Component {
  • Articles
  • Filters
  • Counter
  • +
  • Comments
  • +

    Add new Article form

    }/> - -

    Nor found

    }/> + +

    Not found

    }/>
    ) diff --git a/src/components/routes/ArticleList.js b/src/components/routes/ArticleList.js index 1585f91..fba4509 100644 --- a/src/components/routes/ArticleList.js +++ b/src/components/routes/ArticleList.js @@ -19,6 +19,7 @@ class ArticleListPage extends Component { } getArticle = ({ match }) => { + console.log('---match', match) if (!match) return

    Please select an article

    return
    } diff --git a/src/components/routes/Comment.js b/src/components/routes/Comment.js new file mode 100644 index 0000000..a886c6c --- /dev/null +++ b/src/components/routes/Comment.js @@ -0,0 +1,41 @@ +import React, { Component } from 'react' +import {connect} from 'react-redux' +import {Route} from 'react-router-dom' +import {commentsSelector} from '../../selectors' +import {loadCommentsPage} from '../../AC' +import {propTypes} from 'prop-types' + +class Comment extends Component { + static propTypes = { + + }; + + componentDidMount() { + const {loadCommentsPage, page} = this.props; + const offset = 5*page + loadCommentsPage(`/api/comment?limit=5&offset=${offset}`); + } + + render() { + const {comments} = this.props + + if (!comments.length) return

    loading...

    + + const commentElements = comments.map((comment) =>
  • + {comment.text} by {comment.user} +
  • ) + + return ( + + ) + } +} + +export default connect(state => { + return { + comments: commentsSelector(state), + } +}, {loadCommentsPage})(Comment) + diff --git a/src/components/routes/CommentsPagination.js b/src/components/routes/CommentsPagination.js new file mode 100644 index 0000000..759c994 --- /dev/null +++ b/src/components/routes/CommentsPagination.js @@ -0,0 +1,46 @@ +import React, { Component } from 'react' +import Comment from './Comment' +import {connect} from 'react-redux' +import {Route} from 'react-router-dom' +import {commentsTotal} from '../../selectors' +import {NavLink} from 'react-router-dom' +import {propTypes} from 'prop-types' + +class CommentsPagination extends Component { + static propTypes = { + + }; + + render() { + const {comments, total} = this.props + let pagination = []; + + for (let i = 0; i < total; i += 5) { + pagination.push(
  • + {i/5 + 1} +
  • ) + } + + return ( +
    +
      + {pagination} +
    + +
    + ) + } + + getComment = ({ match }) => { + console.log('match', match); + if (!match) return

    Please select an comment

    + return + } +} + +export default connect(state => { + return { + total: commentsTotal(state), + } +})(CommentsPagination) + diff --git a/src/constants/index.js b/src/constants/index.js index c71387f..7e4d177 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -10,6 +10,8 @@ export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE' export const ADD_COMMENT = 'ADD_COMMENT' +export const LOAD_COMMENTS_PAGE = 'LOAD_COMMENTS_PAGE' + export const START = '_START' export const SUCCESS = '_SUCCESS' export const FAIL = '_FAIL' \ No newline at end of file diff --git a/src/reducer/comments.js b/src/reducer/comments.js index 8cff558..9d681b7 100644 --- a/src/reducer/comments.js +++ b/src/reducer/comments.js @@ -1,4 +1,4 @@ -import { ADD_COMMENT, LOAD_ARTICLE_COMMENTS, SUCCESS } from '../constants' +import { ADD_COMMENT, LOAD_ARTICLE_COMMENTS, SUCCESS, LOAD_COMMENTS_PAGE } from '../constants' import { arrToMap } from './utils' import { OrderedMap, Record } from 'immutable' @@ -9,7 +9,8 @@ const CommentRecord = Record({ }) const ReducerState = Record({ - entities: new OrderedMap({}) + entities: new OrderedMap({}), + total: 0 }) @@ -22,6 +23,12 @@ export default (state = new ReducerState(), action) => { case LOAD_ARTICLE_COMMENTS + SUCCESS: return state.mergeIn(['entities'], arrToMap(response, CommentRecord)) + + case LOAD_COMMENTS_PAGE + SUCCESS: + console.log('---response', response) + return state + .set(['entities'], arrToMap(response.records), CommentRecord) + .set('total', response.total) } return state diff --git a/src/selectors/index.js b/src/selectors/index.js index 95a4230..7a6e04c 100644 --- a/src/selectors/index.js +++ b/src/selectors/index.js @@ -2,12 +2,15 @@ import {createSelector} from 'reselect' export const articlesMapSelector = state => state.articles.entities export const articlesLoadingSelector = state => state.articles.loading +export const commentsTotal = state => state.comments.total export const filtersSelector = state => state.filters export const commentMapSelector = state => state.comments.get('entities') export const idSelector = (_, props) => props.id export const articlesSelector = createSelector(articlesMapSelector, articles => articles.valueSeq().toArray()) +export const commentsSelector = createSelector(commentMapSelector, comments => comments.valueSeq().toArray()) + export const filtratedArticlesSelector = createSelector(articlesSelector, filtersSelector, (articles, filters) => { const {selected, dateRange: {from, to}} = filters @@ -24,4 +27,5 @@ export const articleSelector = createSelector(articlesMapSelector, idSelector, export const createCommentSelector = () => createSelector(commentMapSelector, idSelector, (comments, id) => { return comments.get(id) -}) \ No newline at end of file +}) +