Files
frontend-app-discussions/src/discussions/posts/data/selectors.js
Kshitij Sobti 586cf3e581 Remove Object.assign
Use arrays for pages instead of a map
2021-09-14 21:51:09 +05:30

53 lines
1.4 KiB
JavaScript

/* eslint-disable import/prefer-default-export */
import { createSelector } from '@reduxjs/toolkit';
const selectThreads = state => state.threads.threadsById;
const mapIdsToThreads = (ids, threads) => ids.map(id => threads?.[id]);
export const selectTopicThreads = topicId => createSelector(
[
state => state.threads.threadsInTopic[topicId] || [],
selectThreads,
],
mapIdsToThreads,
);
export const selectThread = threadId => createSelector(
[selectThreads],
(threads) => threads?.[threadId],
);
export const selectAllThreadsOnPage = (page) => createSelector(
[
state => state.threads.pages[page] || [],
selectThreads,
],
mapIdsToThreads,
);
export const selectAllThreads = createSelector(
[
state => state.threads.pages,
selectThreads,
],
(pages, threads) => pages.flatMap(ids => mapIdsToThreads(ids, threads)),
);
export const threadsLoadingStatus = () => state => state.threads.status;
// TODO: eventually this should be server-side filtering
export const selectUserThreads = author => createSelector(
[selectAllThreads],
threads => threads.filter(thread => thread.author === author),
);
export const selectThreadSorting = () => state => state.threads.sortedBy;
export const selectThreadFilters = () => state => state.threads.filters;
export const selectAuthorAvatars = author => state => (
state.threads.avatars?.[author].profile.image
);