Fixed comments order

This commit is contained in:
Simon Backx 2022-07-05 16:08:08 +02:00
parent 771fd15f27
commit fdeed0eeec
3 changed files with 6 additions and 4 deletions

View File

@ -5,8 +5,9 @@ async function loadMoreComments({state, api}) {
}
const data = await api.comments.browse({page, postId: state.postId});
// Note: we store the comments from new to old, and show them in reverse order
return {
comments: [...data.comments, ...state.comments],
comments: [...state.comments, ...data.comments],
pagination: data.meta.pagination
};
}

View File

@ -41,7 +41,7 @@ class CommentsBox extends React.Component {
}
render() {
const comments = !this.context.comments ? 'Loading...' : this.context.comments.map(comment => <Comment comment={comment} key={comment.id} />);
const comments = !this.context.comments ? 'Loading...' : this.context.comments.slice().reverse().map(comment => <Comment comment={comment} key={comment.id} />);
const containerClass = this.darkMode() ? 'dark' : '';

View File

@ -78,8 +78,9 @@ function setupGhostApi({siteUrl = window.location.origin, apiUrl, apiKey}) {
api.comments = {
browse({page, postId}) {
const filter = encodeURIComponent('post_id:' + postId);
const order = encodeURIComponent('created_at DESC');
const url = endpointFor({type: 'members', resource: 'comments', params: `?limit=15&include=member&filter=${filter}&page=${page}`});
const url = endpointFor({type: 'members', resource: 'comments', params: `?limit=5&include=member&order=${order}&filter=${filter}&page=${page}`});
return makeRequest({
url,
method: 'GET',
@ -89,7 +90,7 @@ function setupGhostApi({siteUrl = window.location.origin, apiUrl, apiKey}) {
credentials: 'same-origin'
}).then(function (res) {
if (res.ok) {
return res.json();
return res.json()
} else {
throw new Error('Failed to fetch comments');
}