mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 11:17:49 +01:00
Remove comments pagination
This commit is contained in:
@@ -7,7 +7,6 @@ class CommentsController < ApplicationController
|
|||||||
.left_outer_joins(:user)
|
.left_outer_joins(:user)
|
||||||
.select('comments.id, comments.body, comments.updated_at, users.full_name as user_full_name')
|
.select('comments.id, comments.body, comments.updated_at, users.full_name as user_full_name')
|
||||||
.order(updated_at: :desc)
|
.order(updated_at: :desc)
|
||||||
.page(params[:page])
|
|
||||||
|
|
||||||
render json: comments
|
render json: comments
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ export const COMMENTS_REQUEST_SUCCESS = 'COMMENTS_REQUEST_SUCCESS';
|
|||||||
interface CommentsRequestSuccessAction {
|
interface CommentsRequestSuccessAction {
|
||||||
type: typeof COMMENTS_REQUEST_SUCCESS;
|
type: typeof COMMENTS_REQUEST_SUCCESS;
|
||||||
comments: Array<ICommentJSON>;
|
comments: Array<ICommentJSON>;
|
||||||
page: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const COMMENTS_REQUEST_FAILURE = 'COMMENTS_REQUEST_FAILURE';
|
export const COMMENTS_REQUEST_FAILURE = 'COMMENTS_REQUEST_FAILURE';
|
||||||
@@ -35,11 +34,9 @@ const commentsRequestStart = (): CommentsRequestActionTypes => ({
|
|||||||
|
|
||||||
const commentsRequestSuccess = (
|
const commentsRequestSuccess = (
|
||||||
comments: Array<ICommentJSON>,
|
comments: Array<ICommentJSON>,
|
||||||
page: number
|
|
||||||
): CommentsRequestActionTypes => ({
|
): CommentsRequestActionTypes => ({
|
||||||
type: COMMENTS_REQUEST_SUCCESS,
|
type: COMMENTS_REQUEST_SUCCESS,
|
||||||
comments,
|
comments,
|
||||||
page,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const commentsRequestFailure = (error: string): CommentsRequestActionTypes => ({
|
const commentsRequestFailure = (error: string): CommentsRequestActionTypes => ({
|
||||||
@@ -49,14 +46,13 @@ const commentsRequestFailure = (error: string): CommentsRequestActionTypes => ({
|
|||||||
|
|
||||||
export const requestComments = (
|
export const requestComments = (
|
||||||
postId: number,
|
postId: number,
|
||||||
page: number,
|
|
||||||
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
|
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
|
||||||
dispatch(commentsRequestStart());
|
dispatch(commentsRequestStart());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/posts/${postId}/comments?page=${page}`);
|
const response = await fetch(`/posts/${postId}/comments`);
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
dispatch(commentsRequestSuccess(json, page));
|
dispatch(commentsRequestSuccess(json));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
dispatch(commentsRequestFailure(e));
|
dispatch(commentsRequestFailure(e));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ interface Props {
|
|||||||
comments: Array<IComment>;
|
comments: Array<IComment>;
|
||||||
areLoading: boolean;
|
areLoading: boolean;
|
||||||
error: string;
|
error: string;
|
||||||
page: number;
|
|
||||||
haveMore: boolean;
|
|
||||||
|
|
||||||
requestComments(postId: number, page?: number);
|
requestComments(postId: number, page?: number);
|
||||||
}
|
}
|
||||||
@@ -24,8 +22,6 @@ class CommentsP extends React.Component<Props> {
|
|||||||
comments,
|
comments,
|
||||||
areLoading,
|
areLoading,
|
||||||
error,
|
error,
|
||||||
page,
|
|
||||||
haveMore,
|
|
||||||
} = this.props;
|
} = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ interface Props {
|
|||||||
authenticityToken: string;
|
authenticityToken: string;
|
||||||
|
|
||||||
requestPost(postId: number): void;
|
requestPost(postId: number): void;
|
||||||
requestComments(postId: number, page?: number): void;
|
|
||||||
changePostStatus(
|
changePostStatus(
|
||||||
postId: number,
|
postId: number,
|
||||||
newPostStatusId: number,
|
newPostStatusId: number,
|
||||||
|
|||||||
@@ -10,13 +10,11 @@ const mapStateToProps = (state: State) => ({
|
|||||||
comments: state.currentPost.comments.items,
|
comments: state.currentPost.comments.items,
|
||||||
areLoading: state.currentPost.comments.areLoading,
|
areLoading: state.currentPost.comments.areLoading,
|
||||||
error: state.currentPost.comments.error,
|
error: state.currentPost.comments.error,
|
||||||
page: state.currentPost.comments.page,
|
|
||||||
haveMore: state.currentPost.comments.haveMore,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const mapDispatchToProps = (dispatch) => ({
|
const mapDispatchToProps = (dispatch) => ({
|
||||||
requestComments(postId: number, page: number = 1) {
|
requestComments(postId: number) {
|
||||||
dispatch(requestComments(postId, page));
|
dispatch(requestComments(postId));
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -18,10 +18,6 @@ const mapDispatchToProps = (dispatch) => ({
|
|||||||
dispatch(requestPost(postId));
|
dispatch(requestPost(postId));
|
||||||
},
|
},
|
||||||
|
|
||||||
requestComments(postId: number, page: number = 1) {
|
|
||||||
dispatch(requestComments(postId, page));
|
|
||||||
},
|
|
||||||
|
|
||||||
changePostStatus(postId: number, newPostStatusId: number, authenticityToken: string) {
|
changePostStatus(postId: number, newPostStatusId: number, authenticityToken: string) {
|
||||||
if (isNaN(newPostStatusId)) newPostStatusId = null;
|
if (isNaN(newPostStatusId)) newPostStatusId = null;
|
||||||
|
|
||||||
|
|||||||
@@ -14,16 +14,12 @@ export interface CommentsState {
|
|||||||
items: Array<IComment>;
|
items: Array<IComment>;
|
||||||
areLoading: boolean;
|
areLoading: boolean;
|
||||||
error: string;
|
error: string;
|
||||||
page: number;
|
|
||||||
haveMore: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialState: CommentsState = {
|
const initialState: CommentsState = {
|
||||||
items: [],
|
items: [],
|
||||||
areLoading: false,
|
areLoading: false,
|
||||||
error: '',
|
error: '',
|
||||||
page: 0,
|
|
||||||
haveMore: true,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const commentsReducer = (
|
const commentsReducer = (
|
||||||
@@ -40,17 +36,9 @@ const commentsReducer = (
|
|||||||
case COMMENTS_REQUEST_SUCCESS:
|
case COMMENTS_REQUEST_SUCCESS:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
items: action.page === 1 ?
|
items: action.comments.map(
|
||||||
action.comments.map(comment => commentReducer(undefined, commentRequestSuccess(comment)))
|
comment => commentReducer(undefined, commentRequestSuccess(comment))
|
||||||
:
|
),
|
||||||
[
|
|
||||||
...state.items,
|
|
||||||
...action.comments.map(
|
|
||||||
comment => commentReducer(undefined, commentRequestSuccess(comment))
|
|
||||||
),
|
|
||||||
],
|
|
||||||
page: action.page,
|
|
||||||
haveMore: action.comments.length === 15,
|
|
||||||
areLoading: false,
|
areLoading: false,
|
||||||
error: '',
|
error: '',
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,6 +5,4 @@ class Comment < ApplicationRecord
|
|||||||
has_many :children, class_name: 'Comment', foreign_key: 'parent_id', dependent: :destroy
|
has_many :children, class_name: 'Comment', foreign_key: 'parent_id', dependent: :destroy
|
||||||
|
|
||||||
validates :body, presence: true, length: { minimum: 4 }
|
validates :body, presence: true, length: { minimum: 4 }
|
||||||
|
|
||||||
paginates_per 15
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user