2022-07-05 14:24:29 +02:00
|
|
|
function loadMoreComments({state, api}) {
|
|
|
|
let page = 1;
|
|
|
|
if (state.pagination && state.pagination.page) {
|
|
|
|
page = state.pagination.page + 1;
|
|
|
|
}
|
2022-07-05 15:30:04 +02:00
|
|
|
const data = 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:30:04 +02:00
|
|
|
function addComment({state, api, data: comment}) {
|
|
|
|
const data = api.comments.add({comment});
|
|
|
|
|
|
|
|
return {
|
|
|
|
comments: [...data.comments, ...state.comments]
|
|
|
|
// 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 {};
|
|
|
|
}
|