Files
frontend-app-discussions/src/discussions/posts/data/selectors.js
Kshitij Sobti d31c438fc1 feat: add support for new provider and new topic api (#54)
Adds support for the new openedx provider and the new topics structure it introduces which ses the new topics v2 API.
2022-03-02 08:13:44 +00:00

49 lines
1.3 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 = topicIds => createSelector(
[
state => (topicIds || []).flatMap(topicId => 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;
export const selectThreadSorting = () => state => state.threads.sortedBy;
export const selectThreadFilters = () => state => state.threads.filters;
export const selectThreadNextPage = () => state => state.threads.nextPage;
export const selectAuthorAvatars = author => state => (
state.threads.avatars?.[author]?.profile.image
);