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
|
|
|
|
|
|
|
return {
|
|
|
|
comments: [...data.comments, ...state.comments],
|
|
|
|
pagination: data.meta.pagination
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-05 15:53:28 +02:00
|
|
|
async function addComment({state, api, data: comment}) {
|
|
|
|
await api.comments.add({comment});
|
|
|
|
|
|
|
|
const commentStructured = {
|
|
|
|
...comment,
|
|
|
|
member: state.member,
|
|
|
|
created_at: new Date().toISOString()
|
|
|
|
};
|
2022-07-05 15:30:04 +02:00
|
|
|
|
|
|
|
return {
|
2022-07-05 15:53:28 +02:00
|
|
|
comments: [...state.comments, commentStructured]
|
2022-07-05 15:30:04 +02:00
|
|
|
// todo: fix pagination now?
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-05 14:24:29 +02:00
|
|
|
loadMoreComments
|
2022-07-04 17:23:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Handle actions in the App, returns updated state */
|
|
|
|
export default async function ActionHandler({action, data, state, api}) {
|
|
|
|
const handler = Actions[action];
|
|
|
|
if (handler) {
|
|
|
|
return await handler({data, state, api}) || {};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|