dashboards.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import omit from 'lodash/omit';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import {Client} from 'sentry/api';
  4. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  5. import {t} from 'sentry/locale';
  6. import {
  7. DashboardDetails,
  8. DashboardListItem,
  9. Widget,
  10. } from 'sentry/views/dashboardsV2/types';
  11. import {flattenErrors} from 'sentry/views/dashboardsV2/utils';
  12. export function fetchDashboards(api: Client, orgSlug: string) {
  13. const promise: Promise<DashboardListItem[]> = api.requestPromise(
  14. `/organizations/${orgSlug}/dashboards/`,
  15. {
  16. method: 'GET',
  17. query: {sort: 'myDashboardsAndRecentlyViewed'},
  18. }
  19. );
  20. promise.catch(response => {
  21. const errorResponse = response?.responseJSON ?? null;
  22. if (errorResponse) {
  23. const errors = flattenErrors(errorResponse, {});
  24. addErrorMessage(errors[Object.keys(errors)[0]]);
  25. } else {
  26. addErrorMessage(t('Unable to fetch dashboards'));
  27. }
  28. });
  29. return promise;
  30. }
  31. export function createDashboard(
  32. api: Client,
  33. orgId: string,
  34. newDashboard: DashboardDetails,
  35. duplicate?: boolean
  36. ): Promise<DashboardDetails> {
  37. const {title, widgets, projects, environment, period, start, end} = newDashboard;
  38. const promise: Promise<DashboardDetails> = api.requestPromise(
  39. `/organizations/${orgId}/dashboards/`,
  40. {
  41. method: 'POST',
  42. data: {
  43. title,
  44. widgets: widgets.map(widget => omit(widget, ['tempId'])),
  45. duplicate,
  46. projects,
  47. environment,
  48. period,
  49. start,
  50. end,
  51. },
  52. query: {
  53. project: projects,
  54. },
  55. }
  56. );
  57. promise.catch(response => {
  58. const errorResponse = response?.responseJSON ?? null;
  59. if (errorResponse) {
  60. const errors = flattenErrors(errorResponse, {});
  61. addErrorMessage(errors[Object.keys(errors)[0]]);
  62. } else {
  63. addErrorMessage(t('Unable to create dashboard'));
  64. }
  65. });
  66. return promise;
  67. }
  68. export function updateDashboardVisit(
  69. api: Client,
  70. orgId: string,
  71. dashboardId: string | string[]
  72. ): Promise<void> {
  73. const promise = api.requestPromise(
  74. `/organizations/${orgId}/dashboards/${dashboardId}/visit/`,
  75. {
  76. method: 'POST',
  77. }
  78. );
  79. return promise;
  80. }
  81. export function fetchDashboard(
  82. api: Client,
  83. orgId: string,
  84. dashboardId: string
  85. ): Promise<DashboardDetails> {
  86. const promise: Promise<DashboardDetails> = api.requestPromise(
  87. `/organizations/${orgId}/dashboards/${dashboardId}/`,
  88. {
  89. method: 'GET',
  90. }
  91. );
  92. promise.catch(response => {
  93. const errorResponse = response?.responseJSON ?? null;
  94. if (errorResponse) {
  95. const errors = flattenErrors(errorResponse, {});
  96. addErrorMessage(errors[Object.keys(errors)[0]]);
  97. } else {
  98. addErrorMessage(t('Unable to load dashboard'));
  99. }
  100. });
  101. return promise;
  102. }
  103. export function updateDashboard(
  104. api: Client,
  105. orgId: string,
  106. dashboard: DashboardDetails
  107. ): Promise<DashboardDetails> {
  108. const data = {
  109. title: dashboard.title,
  110. widgets: dashboard.widgets.map(widget => omit(widget, ['tempId'])),
  111. };
  112. const promise: Promise<DashboardDetails> = api.requestPromise(
  113. `/organizations/${orgId}/dashboards/${dashboard.id}/`,
  114. {
  115. method: 'PUT',
  116. data,
  117. query: {
  118. // TODO: This should be replaced in the future with projects
  119. // when we save Dashboard page filters. This is being sent to
  120. // bypass validation when creating or updating dashboards
  121. project: [ALL_ACCESS_PROJECTS],
  122. },
  123. }
  124. );
  125. promise.catch(response => {
  126. const errorResponse = response?.responseJSON ?? null;
  127. if (errorResponse) {
  128. const errors = flattenErrors(errorResponse, {});
  129. addErrorMessage(errors[Object.keys(errors)[0]]);
  130. } else {
  131. addErrorMessage(t('Unable to update dashboard'));
  132. }
  133. });
  134. return promise;
  135. }
  136. export function deleteDashboard(
  137. api: Client,
  138. orgId: string,
  139. dashboardId: string
  140. ): Promise<undefined> {
  141. const promise: Promise<undefined> = api.requestPromise(
  142. `/organizations/${orgId}/dashboards/${dashboardId}/`,
  143. {
  144. method: 'DELETE',
  145. }
  146. );
  147. promise.catch(response => {
  148. const errorResponse = response?.responseJSON ?? null;
  149. if (errorResponse) {
  150. const errors = flattenErrors(errorResponse, {});
  151. addErrorMessage(errors[Object.keys(errors)[0]]);
  152. } else {
  153. addErrorMessage(t('Unable to delete dashboard'));
  154. }
  155. });
  156. return promise;
  157. }
  158. export function validateWidget(
  159. api: Client,
  160. orgId: string,
  161. widget: Widget
  162. ): Promise<undefined> {
  163. const promise: Promise<undefined> = api.requestPromise(
  164. `/organizations/${orgId}/dashboards/widgets/`,
  165. {
  166. method: 'POST',
  167. data: widget,
  168. query: {
  169. // TODO: This should be replaced in the future with projects
  170. // when we save Dashboard page filters. This is being sent to
  171. // bypass validation when creating or updating dashboards
  172. project: [ALL_ACCESS_PROJECTS],
  173. },
  174. }
  175. );
  176. return promise;
  177. }