0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00
ghost/apps/comments-ui/src/actions.js

83 lines
2 KiB
JavaScript
Raw Normal View History

async function loadMoreComments({state, api}) {
2022-07-05 14:24:29 +02:00
let page = 1;
if (state.pagination && state.pagination.page) {
page = state.pagination.page + 1;
}
const data = await api.comments.browse({page, postId: state.postId});
2022-07-05 14:24:29 +02:00
2022-07-05 16:08:08 +02:00
// Note: we store the comments from new to old, and show them in reverse order
2022-07-05 14:24:29 +02:00
return {
2022-07-05 16:08:08 +02:00
comments: [...state.comments, ...data.comments],
2022-07-05 14:24:29 +02:00
pagination: data.meta.pagination
};
}
async function addComment({state, api, data: comment}) {
await api.comments.add({comment});
const commentStructured = {
...comment,
member: state.member,
created_at: new Date().toISOString()
};
return {
2022-07-05 16:10:24 +02:00
comments: [commentStructured, ...state.comments]
// todo: fix pagination now?
};
}
async function hideComment({state, adminApi, data: comment}) {
await adminApi.hideComment(comment.id);
return {
comments: state.comments.map((c) => {
if (c.id === comment.id) {
return {
...c,
status: 'hidden'
};
}
return c;
})
};
}
2022-07-06 11:27:03 +02:00
async function deleteComment({state, api, data: comment}) {
await api.comments.edit({
comment: {
id: comment.id,
status: 'deleted'
}
});
return {
comments: state.comments.map((c) => {
if (c.id === comment.id) {
return {
...c,
status: 'deleted'
};
}
return c;
})
};
}
const Actions = {
// Put your actions here
addComment,
hideComment,
2022-07-06 11:27:03 +02:00
deleteComment,
2022-07-05 14:24:29 +02:00
loadMoreComments
};
/** Handle actions in the App, returns updated state */
export default async function ActionHandler({action, data, state, api, adminApi}) {
const handler = Actions[action];
if (handler) {
return await handler({data, state, api, adminApi}) || {};
}
return {};
}