selectors.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { createSelector } from 'reselect';
  2. import {
  3. RootState,
  4. TopicName,
  5. FetchStatus,
  6. TopicsState,
  7. TopicConfigByName,
  8. } from 'redux/interfaces';
  9. import { createFetchingSelector } from 'redux/reducers/loader/selectors';
  10. const topicsState = ({ topics }: RootState): TopicsState => topics;
  11. const getAllNames = (state: RootState) => topicsState(state).allNames;
  12. const getTopicMap = (state: RootState) => topicsState(state).byName;
  13. export const getTopicMessages = (state: RootState) =>
  14. topicsState(state).messages;
  15. const getTopicListFetchingStatus = createFetchingSelector('GET_TOPICS');
  16. const getTopicDetailsFetchingStatus = createFetchingSelector(
  17. 'GET_TOPIC_DETAILS'
  18. );
  19. const getTopicMessagesFetchingStatus = createFetchingSelector(
  20. 'GET_TOPIC_MESSAGES'
  21. );
  22. const getTopicConfigFetchingStatus = createFetchingSelector('GET_TOPIC_CONFIG');
  23. const getTopicCreationStatus = createFetchingSelector('POST_TOPIC');
  24. const getTopicUpdateStatus = createFetchingSelector('PATCH_TOPIC');
  25. export const getIsTopicListFetched = createSelector(
  26. getTopicListFetchingStatus,
  27. (status) => status === FetchStatus.fetched
  28. );
  29. export const getIsTopicDetailsFetched = createSelector(
  30. getTopicDetailsFetchingStatus,
  31. (status) => status === FetchStatus.fetched
  32. );
  33. export const getIsTopicMessagesFetched = createSelector(
  34. getTopicMessagesFetchingStatus,
  35. (status) => status === FetchStatus.fetched
  36. );
  37. export const getTopicConfigFetched = createSelector(
  38. getTopicConfigFetchingStatus,
  39. (status) => status === FetchStatus.fetched
  40. );
  41. export const getTopicCreated = createSelector(
  42. getTopicCreationStatus,
  43. (status) => status === FetchStatus.fetched
  44. );
  45. export const getTopicUpdated = createSelector(
  46. getTopicUpdateStatus,
  47. (status) => status === FetchStatus.fetched
  48. );
  49. export const getTopicList = createSelector(
  50. getIsTopicListFetched,
  51. getAllNames,
  52. getTopicMap,
  53. (isFetched, allNames, byName) => {
  54. if (!isFetched) {
  55. return [];
  56. }
  57. return allNames.map((name) => byName[name]);
  58. }
  59. );
  60. export const getExternalTopicList = createSelector(getTopicList, (topics) =>
  61. topics.filter(({ internal }) => !internal)
  62. );
  63. const getTopicName = (_: RootState, topicName: TopicName) => topicName;
  64. export const getTopicByName = createSelector(
  65. getTopicMap,
  66. getTopicName,
  67. (topics, topicName) => topics[topicName]
  68. );
  69. export const getFullTopic = createSelector(getTopicByName, (topic) =>
  70. topic && topic.config && !!topic.partitionCount ? topic : undefined
  71. );
  72. export const getTopicConfig = createSelector(
  73. getTopicByName,
  74. ({ config }) => config
  75. );
  76. export const getTopicConfigByParamName = createSelector(
  77. getTopicConfig,
  78. (config) => {
  79. const byParamName: TopicConfigByName = {
  80. byName: {},
  81. };
  82. if (config) {
  83. config.forEach((param) => {
  84. byParamName.byName[param.name] = param;
  85. });
  86. }
  87. return byParamName;
  88. }
  89. );