dashboards.tsx 5.0 KB

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