dashboards.tsx 5.1 KB

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