Added API connection to add and view comments

This commit is contained in:
Simon Backx 2022-07-05 15:53:28 +02:00
parent 74defe9e0c
commit 4de5c7a64b
2 changed files with 13 additions and 39 deletions

View File

@ -1,9 +1,9 @@
function loadMoreComments({state, api}) {
async function loadMoreComments({state, api}) {
let page = 1;
if (state.pagination && state.pagination.page) {
page = state.pagination.page + 1;
}
const data = api.comments.browse({page, postId: state.postId});
const data = await api.comments.browse({page, postId: state.postId});
return {
comments: [...data.comments, ...state.comments],
@ -11,11 +11,17 @@ function loadMoreComments({state, api}) {
};
}
function addComment({state, api, data: comment}) {
const data = api.comments.add({comment});
async function addComment({state, api, data: comment}) {
await api.comments.add({comment});
const commentStructured = {
...comment,
member: state.member,
created_at: new Date().toISOString()
};
return {
comments: [...data.comments, ...state.comments]
comments: [...state.comments, commentStructured]
// todo: fix pagination now?
};
}

View File

@ -77,39 +77,7 @@ function setupGhostApi({siteUrl = window.location.origin, apiUrl, apiKey}) {
api.comments = {
browse({page, postId}) {
const limit = 5;
const comments = (new Array(limit)).fill().map(() => {
return {
id: 'comment-' + Math.random() * 10000 + Date.now(),
member: {
avatar: '',
bio: 'CEO',
name: 'Terry Korsgaard'
},
html: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut mollis erat vitae diam gravida accumsan vitae quis nisl. Donec luctus laoreet mauris, nec posuere turpis accumsan in. Proin sagittis magna quis vulputate tempus.',
created_at: '2022-07-05T13:33:00.284Z'
};
});
// Temporary placeholder until we have a proper API
return {
comments,
meta: {
pagination: {
page: page,
limit,
pages: 3,
total: 15 * 3,
next: null,
prev: null
}
}
};
// !! This commented code is working, don't delete it ;)
// This fetches the comments from the real API.
/*const filter = encodeURIComponent('post_id:' + postId);
const filter = encodeURIComponent('post_id:' + postId);
const url = endpointFor({type: 'members', resource: 'comments', params: `?limit=15&include=member&filter=${filter}&page=${page}`});
return makeRequest({
@ -125,7 +93,7 @@ function setupGhostApi({siteUrl = window.location.origin, apiUrl, apiKey}) {
} else {
throw new Error('Failed to fetch comments');
}
});*/
});
},
add({comment}) {
const body = {