Remove comments pagination

This commit is contained in:
riggraz
2019-09-17 15:03:25 +02:00
parent b40ddfd543
commit 476f720119
8 changed files with 7 additions and 37 deletions

View File

@@ -7,7 +7,6 @@ class CommentsController < ApplicationController
.left_outer_joins(:user)
.select('comments.id, comments.body, comments.updated_at, users.full_name as user_full_name')
.order(updated_at: :desc)
.page(params[:page])
render json: comments
end

View File

@@ -14,7 +14,6 @@ export const COMMENTS_REQUEST_SUCCESS = 'COMMENTS_REQUEST_SUCCESS';
interface CommentsRequestSuccessAction {
type: typeof COMMENTS_REQUEST_SUCCESS;
comments: Array<ICommentJSON>;
page: number;
}
export const COMMENTS_REQUEST_FAILURE = 'COMMENTS_REQUEST_FAILURE';
@@ -35,11 +34,9 @@ const commentsRequestStart = (): CommentsRequestActionTypes => ({
const commentsRequestSuccess = (
comments: Array<ICommentJSON>,
page: number
): CommentsRequestActionTypes => ({
type: COMMENTS_REQUEST_SUCCESS,
comments,
page,
});
const commentsRequestFailure = (error: string): CommentsRequestActionTypes => ({
@@ -49,14 +46,13 @@ const commentsRequestFailure = (error: string): CommentsRequestActionTypes => ({
export const requestComments = (
postId: number,
page: number,
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
dispatch(commentsRequestStart());
try {
const response = await fetch(`/posts/${postId}/comments?page=${page}`);
const response = await fetch(`/posts/${postId}/comments`);
const json = await response.json();
dispatch(commentsRequestSuccess(json, page));
dispatch(commentsRequestSuccess(json));
} catch (e) {
dispatch(commentsRequestFailure(e));
}

View File

@@ -8,8 +8,6 @@ interface Props {
comments: Array<IComment>;
areLoading: boolean;
error: string;
page: number;
haveMore: boolean;
requestComments(postId: number, page?: number);
}
@@ -24,8 +22,6 @@ class CommentsP extends React.Component<Props> {
comments,
areLoading,
error,
page,
haveMore,
} = this.props;
return (

View File

@@ -16,7 +16,6 @@ interface Props {
authenticityToken: string;
requestPost(postId: number): void;
requestComments(postId: number, page?: number): void;
changePostStatus(
postId: number,
newPostStatusId: number,

View File

@@ -10,13 +10,11 @@ const mapStateToProps = (state: State) => ({
comments: state.currentPost.comments.items,
areLoading: state.currentPost.comments.areLoading,
error: state.currentPost.comments.error,
page: state.currentPost.comments.page,
haveMore: state.currentPost.comments.haveMore,
});
const mapDispatchToProps = (dispatch) => ({
requestComments(postId: number, page: number = 1) {
dispatch(requestComments(postId, page));
requestComments(postId: number) {
dispatch(requestComments(postId));
},
});

View File

@@ -18,10 +18,6 @@ const mapDispatchToProps = (dispatch) => ({
dispatch(requestPost(postId));
},
requestComments(postId: number, page: number = 1) {
dispatch(requestComments(postId, page));
},
changePostStatus(postId: number, newPostStatusId: number, authenticityToken: string) {
if (isNaN(newPostStatusId)) newPostStatusId = null;

View File

@@ -14,16 +14,12 @@ export interface CommentsState {
items: Array<IComment>;
areLoading: boolean;
error: string;
page: number;
haveMore: boolean;
}
const initialState: CommentsState = {
items: [],
areLoading: false,
error: '',
page: 0,
haveMore: true,
};
const commentsReducer = (
@@ -40,17 +36,9 @@ const commentsReducer = (
case COMMENTS_REQUEST_SUCCESS:
return {
...state,
items: action.page === 1 ?
action.comments.map(comment => commentReducer(undefined, commentRequestSuccess(comment)))
:
[
...state.items,
...action.comments.map(
items: action.comments.map(
comment => commentReducer(undefined, commentRequestSuccess(comment))
),
],
page: action.page,
haveMore: action.comments.length === 15,
areLoading: false,
error: '',
};

View File

@@ -5,6 +5,4 @@ class Comment < ApplicationRecord
has_many :children, class_name: 'Comment', foreign_key: 'parent_id', dependent: :destroy
validates :body, presence: true, length: { minimum: 4 }
paginates_per 15
end