selectors.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { createSelector } from 'reselect';
  2. import { RootState, TopicName, FetchStatus, TopicsState } from 'lib/interfaces';
  3. import { createFetchingSelector } from 'redux/reducers/loader/selectors';
  4. const topicsState = ({ topics }: RootState): TopicsState => topics;
  5. const getAllNames = (state: RootState) => topicsState(state).allNames;
  6. const getTopicMap = (state: RootState) => topicsState(state).byName;
  7. const getTopicListFetchingStatus = createFetchingSelector('GET_TOPICS');
  8. const getTopicDetailsFetchingStatus = createFetchingSelector('GET_TOPIC_DETAILS');
  9. const getTopicConfigFetchingStatus = createFetchingSelector('GET_TOPIC_CONFIG');
  10. const getTopicCreationStatus = createFetchingSelector('POST_TOPIC');
  11. export const getIsTopicListFetched = createSelector(
  12. getTopicListFetchingStatus,
  13. (status) => status === FetchStatus.fetched,
  14. );
  15. export const getIsTopicDetailsFetched = createSelector(
  16. getTopicDetailsFetchingStatus,
  17. (status) => status === FetchStatus.fetched,
  18. );
  19. export const getTopicConfigFetched = createSelector(
  20. getTopicConfigFetchingStatus,
  21. (status) => status === FetchStatus.fetched,
  22. );
  23. export const getTopicCreated = createSelector(
  24. getTopicCreationStatus,
  25. (status) => status === FetchStatus.fetched,
  26. );
  27. export const getTopicList = createSelector(
  28. getIsTopicListFetched,
  29. getAllNames,
  30. getTopicMap,
  31. (isFetched, allNames, byName) => {
  32. if (isFetched) {
  33. return allNames.map((name) => byName[name])
  34. }
  35. return [];
  36. },
  37. );
  38. export const getExternalTopicList = createSelector(
  39. getTopicList,
  40. (topics) => topics.filter(({ internal }) => !internal),
  41. );
  42. const getTopicName = (_: RootState, topicName: TopicName) => topicName;
  43. export const getTopicByName = createSelector(
  44. getTopicMap,
  45. getTopicName,
  46. (topics, topicName) => topics[topicName],
  47. );
  48. export const getTopicConfig = createSelector(getTopicByName, ({ config }) => config);