dashboards.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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, filters, utc} =
  38. newDashboard;
  39. const promise: Promise<DashboardDetails> = api.requestPromise(
  40. `/organizations/${orgId}/dashboards/`,
  41. {
  42. method: 'POST',
  43. data: {
  44. title,
  45. widgets: widgets.map(widget => omit(widget, ['tempId'])),
  46. duplicate,
  47. projects,
  48. environment,
  49. period,
  50. start,
  51. end,
  52. filters,
  53. utc,
  54. },
  55. query: {
  56. project: projects,
  57. },
  58. }
  59. );
  60. promise.catch(response => {
  61. const errorResponse = response?.responseJSON ?? null;
  62. if (errorResponse) {
  63. const errors = flattenErrors(errorResponse, {});
  64. addErrorMessage(errors[Object.keys(errors)[0]]);
  65. } else {
  66. addErrorMessage(t('Unable to create dashboard'));
  67. }
  68. });
  69. return promise;
  70. }
  71. export function updateDashboardVisit(
  72. api: Client,
  73. orgId: string,
  74. dashboardId: string | string[]
  75. ): Promise<void> {
  76. const promise = api.requestPromise(
  77. `/organizations/${orgId}/dashboards/${dashboardId}/visit/`,
  78. {
  79. method: 'POST',
  80. }
  81. );
  82. return promise;
  83. }
  84. export function fetchDashboard(
  85. api: Client,
  86. orgId: string,
  87. dashboardId: string
  88. ): Promise<DashboardDetails> {
  89. const promise: Promise<DashboardDetails> = api.requestPromise(
  90. `/organizations/${orgId}/dashboards/${dashboardId}/`,
  91. {
  92. method: 'GET',
  93. }
  94. );
  95. promise.catch(response => {
  96. const errorResponse = response?.responseJSON ?? null;
  97. if (errorResponse) {
  98. const errors = flattenErrors(errorResponse, {});
  99. addErrorMessage(errors[Object.keys(errors)[0]]);
  100. } else {
  101. addErrorMessage(t('Unable to load dashboard'));
  102. }
  103. });
  104. return promise;
  105. }
  106. export function updateDashboard(
  107. api: Client,
  108. orgId: string,
  109. dashboard: DashboardDetails
  110. ): Promise<DashboardDetails> {
  111. const {title, widgets, projects, environment, period, start, end, filters, utc} =
  112. dashboard;
  113. const data = {
  114. title,
  115. widgets: widgets.map(widget => omit(widget, ['tempId'])),
  116. projects,
  117. environment,
  118. period,
  119. start,
  120. end,
  121. filters,
  122. utc,
  123. };
  124. const promise: Promise<DashboardDetails> = api.requestPromise(
  125. `/organizations/${orgId}/dashboards/${dashboard.id}/`,
  126. {
  127. method: 'PUT',
  128. data,
  129. query: {
  130. project: projects,
  131. },
  132. }
  133. );
  134. promise.catch(response => {
  135. const errorResponse = response?.responseJSON ?? null;
  136. if (errorResponse) {
  137. const errors = flattenErrors(errorResponse, {});
  138. addErrorMessage(errors[Object.keys(errors)[0]]);
  139. } else {
  140. addErrorMessage(t('Unable to update dashboard'));
  141. }
  142. });
  143. return promise;
  144. }
  145. export function deleteDashboard(
  146. api: Client,
  147. orgId: string,
  148. dashboardId: string
  149. ): Promise<undefined> {
  150. const promise: Promise<undefined> = api.requestPromise(
  151. `/organizations/${orgId}/dashboards/${dashboardId}/`,
  152. {
  153. method: 'DELETE',
  154. }
  155. );
  156. promise.catch(response => {
  157. const errorResponse = response?.responseJSON ?? null;
  158. if (errorResponse) {
  159. const errors = flattenErrors(errorResponse, {});
  160. addErrorMessage(errors[Object.keys(errors)[0]]);
  161. } else {
  162. addErrorMessage(t('Unable to delete dashboard'));
  163. }
  164. });
  165. return promise;
  166. }
  167. export function validateWidget(
  168. api: Client,
  169. orgId: string,
  170. widget: Widget
  171. ): Promise<undefined> {
  172. const promise: Promise<undefined> = api.requestPromise(
  173. `/organizations/${orgId}/dashboards/widgets/`,
  174. {
  175. method: 'POST',
  176. data: widget,
  177. query: {
  178. // TODO: This should be replaced in the future with projects
  179. // when we save Dashboard page filters. This is being sent to
  180. // bypass validation when creating or updating dashboards
  181. project: [ALL_ACCESS_PROJECTS],
  182. },
  183. }
  184. );
  185. return promise;
  186. }