thunks.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import * as api from 'redux/api';
  2. import {
  3. ConsumerGroupID,
  4. PromiseThunk,
  5. Cluster,
  6. ClusterName,
  7. TopicFormData,
  8. TopicName,
  9. Topic,
  10. } from 'redux/interfaces';
  11. import * as actions from './actions';
  12. export const fetchBrokers = (
  13. clusterName: ClusterName
  14. ): PromiseThunk<void> => async (dispatch) => {
  15. dispatch(actions.fetchBrokersAction.request());
  16. try {
  17. const payload = await api.getBrokers(clusterName);
  18. dispatch(actions.fetchBrokersAction.success(payload));
  19. } catch (e) {
  20. dispatch(actions.fetchBrokersAction.failure());
  21. }
  22. };
  23. export const fetchBrokerMetrics = (
  24. clusterName: ClusterName
  25. ): PromiseThunk<void> => async (dispatch) => {
  26. dispatch(actions.fetchBrokerMetricsAction.request());
  27. try {
  28. const payload = await api.getBrokerMetrics(clusterName);
  29. dispatch(actions.fetchBrokerMetricsAction.success(payload));
  30. } catch (e) {
  31. dispatch(actions.fetchBrokerMetricsAction.failure());
  32. }
  33. };
  34. export const fetchClustersList = (): PromiseThunk<void> => async (dispatch) => {
  35. dispatch(actions.fetchClusterListAction.request());
  36. try {
  37. const clusters: Cluster[] = await api.getClusters();
  38. dispatch(actions.fetchClusterListAction.success(clusters));
  39. } catch (e) {
  40. dispatch(actions.fetchClusterListAction.failure());
  41. }
  42. };
  43. export const fetchTopicList = (
  44. clusterName: ClusterName
  45. ): PromiseThunk<void> => async (dispatch) => {
  46. dispatch(actions.fetchTopicListAction.request());
  47. try {
  48. const topics = await api.getTopics(clusterName);
  49. dispatch(actions.fetchTopicListAction.success(topics));
  50. } catch (e) {
  51. dispatch(actions.fetchTopicListAction.failure());
  52. }
  53. };
  54. export const fetchTopicMessages = (
  55. clusterName: ClusterName,
  56. topicName: TopicName
  57. ): PromiseThunk<void> => async (dispatch) => {
  58. dispatch(actions.fetchTopicMessagesAction.request());
  59. try {
  60. const messages = await api.getTopicMessages(clusterName, topicName);
  61. dispatch(actions.fetchTopicMessagesAction.success(messages));
  62. } catch (e) {
  63. dispatch(actions.fetchTopicMessagesAction.failure());
  64. }
  65. };
  66. export const fetchTopicDetails = (
  67. clusterName: ClusterName,
  68. topicName: TopicName
  69. ): PromiseThunk<void> => async (dispatch) => {
  70. dispatch(actions.fetchTopicDetailsAction.request());
  71. try {
  72. const topicDetails = await api.getTopicDetails(clusterName, topicName);
  73. dispatch(
  74. actions.fetchTopicDetailsAction.success({
  75. topicName,
  76. details: topicDetails,
  77. })
  78. );
  79. } catch (e) {
  80. dispatch(actions.fetchTopicDetailsAction.failure());
  81. }
  82. };
  83. export const fetchTopicConfig = (
  84. clusterName: ClusterName,
  85. topicName: TopicName
  86. ): PromiseThunk<void> => async (dispatch) => {
  87. dispatch(actions.fetchTopicConfigAction.request());
  88. try {
  89. const config = await api.getTopicConfig(clusterName, topicName);
  90. dispatch(actions.fetchTopicConfigAction.success({ topicName, config }));
  91. } catch (e) {
  92. dispatch(actions.fetchTopicConfigAction.failure());
  93. }
  94. };
  95. export const createTopic = (
  96. clusterName: ClusterName,
  97. form: TopicFormData
  98. ): PromiseThunk<void> => async (dispatch) => {
  99. dispatch(actions.createTopicAction.request());
  100. try {
  101. const topic: Topic = await api.postTopic(clusterName, form);
  102. dispatch(actions.createTopicAction.success(topic));
  103. } catch (e) {
  104. dispatch(actions.createTopicAction.failure());
  105. }
  106. };
  107. export const updateTopic = (
  108. clusterName: ClusterName,
  109. form: TopicFormData
  110. ): PromiseThunk<void> => async (dispatch) => {
  111. dispatch(actions.updateTopicAction.request());
  112. try {
  113. const topic: Topic = await api.patchTopic(clusterName, form);
  114. dispatch(actions.updateTopicAction.success(topic));
  115. } catch (e) {
  116. dispatch(actions.updateTopicAction.failure());
  117. }
  118. };
  119. export const fetchConsumerGroupsList = (
  120. clusterName: ClusterName
  121. ): PromiseThunk<void> => async (dispatch) => {
  122. dispatch(actions.fetchConsumerGroupsAction.request());
  123. try {
  124. const consumerGroups = await api.getConsumerGroups(clusterName);
  125. dispatch(actions.fetchConsumerGroupsAction.success(consumerGroups));
  126. } catch (e) {
  127. dispatch(actions.fetchConsumerGroupsAction.failure());
  128. }
  129. };
  130. export const fetchConsumerGroupDetails = (
  131. clusterName: ClusterName,
  132. consumerGroupID: ConsumerGroupID
  133. ): PromiseThunk<void> => async (dispatch) => {
  134. dispatch(actions.fetchConsumerGroupDetailsAction.request());
  135. try {
  136. const consumerGroupDetails = await api.getConsumerGroupDetails(
  137. clusterName,
  138. consumerGroupID
  139. );
  140. dispatch(
  141. actions.fetchConsumerGroupDetailsAction.success({
  142. consumerGroupID,
  143. details: consumerGroupDetails,
  144. })
  145. );
  146. } catch (e) {
  147. dispatch(actions.fetchConsumerGroupDetailsAction.failure());
  148. }
  149. };