Skip to content
Snippets Groups Projects
Commit 76dd445b authored by xaralis's avatar xaralis
Browse files

feat: re-sort posts after ranking in a throttled manner

parent 9004a89b
No related branches found
No related tags found
No related merge requests found
Pipeline #2081 passed
...@@ -22,16 +22,12 @@ const PostFilters = () => { ...@@ -22,16 +22,12 @@ const PostFilters = () => {
{ title: "Jen příspěvky", value: "discussionOnly" }, { title: "Jen příspěvky", value: "discussionOnly" },
]; ];
const setFilter = (prop, newValue, resetPage = true) => { const setFilter = (prop, newValue) => {
PostStore.update((state) => { PostStore.update((state) => {
state.filters[prop] = newValue; state.filters[prop] = newValue;
state.window.itemCount = state.window.items.length; state.window.itemCount = state.window.items.length;
updateWindowPosts(state); updateWindowPosts(state);
if (resetPage) {
state.window.page = 1;
}
}); });
}; };
......
import has from "lodash/has"; import has from "lodash/has";
import throttle from "lodash/throttle";
import { markdownConverter } from "markdown"; import { markdownConverter } from "markdown";
import { PostStore } from "stores"; import { PostStore } from "stores";
import { parseRawPost, postsStateMapping, updateWindowPosts } from "utils"; import { parseRawPost, postsStateMapping, updateWindowPosts } from "utils";
/**
* Re-apply sorting by rank but no more than once every 3 seconds.
*/
const sortOnRankThrottled = throttle(() => {
PostStore.update((state) => {
if (state.filters.sort === "byScore") {
updateWindowPosts(state);
}
});
}, 3000);
export const handlePostRanked = (payload) => { export const handlePostRanked = (payload) => {
PostStore.update((state) => { PostStore.update((state) => {
if (state.items[payload.id]) { if (state.items[payload.id]) {
...@@ -12,12 +24,11 @@ export const handlePostRanked = (payload) => { ...@@ -12,12 +24,11 @@ export const handlePostRanked = (payload) => {
state.items[payload.id].ranking.score = state.items[payload.id].ranking.score =
state.items[payload.id].ranking.likes - state.items[payload.id].ranking.likes -
state.items[payload.id].ranking.dislikes; state.items[payload.id].ranking.dislikes;
if (state.filters.sort === "byScore") {
updateWindowPosts(state);
}
} }
}); });
// Run sorting in a throttled manner.
sortOnRankThrottled();
}; };
export const handlePostChanged = (payload) => { export const handlePostChanged = (payload) => {
...@@ -33,13 +44,13 @@ export const handlePostChanged = (payload) => { ...@@ -33,13 +44,13 @@ export const handlePostChanged = (payload) => {
if (has(payload, "state")) { if (has(payload, "state")) {
state.items[payload.id].state = postsStateMapping[payload.state]; state.items[payload.id].state = postsStateMapping[payload.state];
updateWindowPosts(state);
} }
if (has(payload, "is_archived")) { if (has(payload, "is_archived")) {
state.items[payload.id].archived = payload.is_archived; state.items[payload.id].archived = payload.is_archived;
updateWindowPosts(state);
} }
updateWindowPosts(state);
} }
}); });
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment