2022-07-05 15:53:28 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-07-05 15:53:28 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-05 15:53:28 +02:00
|
|
|
async function addComment({state, api, data: comment}) {
|
2022-07-06 17:30:44 +02:00
|
|
|
const data = await api.comments.add({comment});
|
|
|
|
comment = data.comments[0];
|
2022-07-05 15:53:28 +02:00
|
|
|
|
|
|
|
const commentStructured = {
|
|
|
|
...comment,
|
2022-07-06 17:30:44 +02:00
|
|
|
// Temporary workaround for missing member relation (bug in API)
|
|
|
|
member: state.member
|
2022-07-05 15:53:28 +02:00
|
|
|
};
|
2022-07-05 15:30:04 +02:00
|
|
|
|
|
|
|
return {
|
2022-07-05 16:10:24 +02:00
|
|
|
comments: [commentStructured, ...state.comments]
|
2022-07-05 15:30:04 +02:00
|
|
|
// todo: fix pagination now?
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:11:54 +02:00
|
|
|
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:45:45 +02:00
|
|
|
async function showComment({state, adminApi, data: comment}) {
|
|
|
|
await adminApi.showComment(comment.id);
|
|
|
|
|
|
|
|
return {
|
|
|
|
comments: state.comments.map((c) => {
|
|
|
|
if (c.id === comment.id) {
|
|
|
|
return {
|
|
|
|
...c,
|
|
|
|
status: 'published'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-06 17:20:19 +02:00
|
|
|
async function likeComment({state, api, data: comment}) {
|
|
|
|
await api.comments.like({comment});
|
|
|
|
|
|
|
|
return {
|
|
|
|
comments: state.comments.map((c) => {
|
|
|
|
if (c.id === comment.id) {
|
|
|
|
return {
|
|
|
|
...c,
|
|
|
|
liked: true,
|
|
|
|
likes_count: c.likes_count + 1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function unlikeComment({state, api, data: comment}) {
|
|
|
|
await api.comments.unlike({comment});
|
|
|
|
|
|
|
|
return {
|
|
|
|
comments: state.comments.map((c) => {
|
|
|
|
if (c.id === comment.id) {
|
|
|
|
return {
|
|
|
|
...c,
|
|
|
|
liked: false,
|
|
|
|
likes_count: c.likes_count - 1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-07 10:29:29 +02:00
|
|
|
async function editComment({state, api, data: comment}) {
|
|
|
|
const data = await api.comments.edit({
|
|
|
|
comment
|
|
|
|
});
|
|
|
|
comment = data.comments[0];
|
|
|
|
|
|
|
|
// Replace the comment in the state with the new one
|
|
|
|
return {
|
|
|
|
comments: state.comments.map((c) => {
|
|
|
|
if (c.id === comment.id) {
|
|
|
|
return comment;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-04 17:23:01 +02:00
|
|
|
const Actions = {
|
|
|
|
// Put your actions here
|
2022-07-05 15:30:04 +02:00
|
|
|
addComment,
|
2022-07-07 10:29:29 +02:00
|
|
|
editComment,
|
2022-07-06 11:11:54 +02:00
|
|
|
hideComment,
|
2022-07-06 11:27:03 +02:00
|
|
|
deleteComment,
|
2022-07-06 11:45:45 +02:00
|
|
|
showComment,
|
2022-07-06 17:20:19 +02:00
|
|
|
likeComment,
|
|
|
|
unlikeComment,
|
2022-07-05 14:24:29 +02:00
|
|
|
loadMoreComments
|
2022-07-04 17:23:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Handle actions in the App, returns updated state */
|
2022-07-06 11:11:54 +02:00
|
|
|
export default async function ActionHandler({action, data, state, api, adminApi}) {
|
2022-07-04 17:23:01 +02:00
|
|
|
const handler = Actions[action];
|
|
|
|
if (handler) {
|
2022-07-06 11:11:54 +02:00
|
|
|
return await handler({data, state, api, adminApi}) || {};
|
2022-07-04 17:23:01 +02:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|