selectors.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. const getTopicListFetchingStatus = createFetchingSelector('GET_TOPICS');
  14. const getTopicDetailsFetchingStatus = createFetchingSelector(
  15. 'GET_TOPIC_DETAILS'
  16. );
  17. const getTopicConfigFetchingStatus = createFetchingSelector('GET_TOPIC_CONFIG');
  18. const getTopicCreationStatus = createFetchingSelector('POST_TOPIC');
  19. const getTopicUpdateStatus = createFetchingSelector('PATCH_TOPIC');
  20. export const getIsTopicListFetched = createSelector(
  21. getTopicListFetchingStatus,
  22. (status) => status === FetchStatus.fetched
  23. );
  24. export const getIsTopicDetailsFetched = createSelector(
  25. getTopicDetailsFetchingStatus,
  26. (status) => status === FetchStatus.fetched
  27. );
  28. export const getTopicConfigFetched = createSelector(
  29. getTopicConfigFetchingStatus,
  30. (status) => status === FetchStatus.fetched
  31. );
  32. export const getTopicCreated = createSelector(
  33. getTopicCreationStatus,
  34. (status) => status === FetchStatus.fetched
  35. );
  36. export const getTopicUpdated = createSelector(
  37. getTopicUpdateStatus,
  38. (status) => status === FetchStatus.fetched
  39. );
  40. export const getTopicList = createSelector(
  41. getIsTopicListFetched,
  42. getAllNames,
  43. getTopicMap,
  44. (isFetched, allNames, byName) => {
  45. if (!isFetched) {
  46. return [];
  47. }
  48. return allNames.map((name) => byName[name]);
  49. }
  50. );
  51. export const getExternalTopicList = createSelector(getTopicList, (topics) =>
  52. topics.filter(({ internal }) => !internal)
  53. );
  54. const getTopicName = (_: RootState, topicName: TopicName) => topicName;
  55. export const getTopicByName = createSelector(
  56. getTopicMap,
  57. getTopicName,
  58. (topics, topicName) => topics[topicName]
  59. );
  60. export const getFullTopic = createSelector(getTopicByName, (topic) =>
  61. topic && topic.config && !!topic.partitionCount ? topic : undefined
  62. );
  63. export const getTopicConfig = createSelector(
  64. getTopicByName,
  65. ({ config }) => config
  66. );
  67. export const getTopicConfigByParamName = createSelector(
  68. getTopicConfig,
  69. (config) => {
  70. const byParamName: TopicConfigByName = {
  71. byName: {},
  72. };
  73. if (config) {
  74. config.forEach((param) => {
  75. byParamName.byName[param.name] = param;
  76. });
  77. }
  78. return byParamName;
  79. }
  80. );