dashboards.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import omit from 'lodash/omit';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import {Client} from 'sentry/api';
  4. import {t} from 'sentry/locale';
  5. import {DashboardDetails, Widget} from 'sentry/views/dashboardsV2/types';
  6. import {flattenErrors} from 'sentry/views/dashboardsV2/utils';
  7. export function createDashboard(
  8. api: Client,
  9. orgId: string,
  10. newDashboard: DashboardDetails,
  11. duplicate?: boolean
  12. ): Promise<DashboardDetails> {
  13. const {title, widgets} = newDashboard;
  14. const promise: Promise<DashboardDetails> = api.requestPromise(
  15. `/organizations/${orgId}/dashboards/`,
  16. {
  17. method: 'POST',
  18. data: {title, widgets: widgets.map(widget => omit(widget, ['tempId'])), duplicate},
  19. }
  20. );
  21. promise.catch(response => {
  22. const errorResponse = response?.responseJSON ?? null;
  23. if (errorResponse) {
  24. const errors = flattenErrors(errorResponse, {});
  25. addErrorMessage(errors[Object.keys(errors)[0]]);
  26. } else {
  27. addErrorMessage(t('Unable to create dashboard'));
  28. }
  29. });
  30. return promise;
  31. }
  32. export function updateDashboardVisit(
  33. api: Client,
  34. orgId: string,
  35. dashboardId: string | string[]
  36. ): Promise<void> {
  37. const promise = api.requestPromise(
  38. `/organizations/${orgId}/dashboards/${dashboardId}/visit/`,
  39. {
  40. method: 'POST',
  41. }
  42. );
  43. return promise;
  44. }
  45. export function fetchDashboard(
  46. api: Client,
  47. orgId: string,
  48. dashboardId: string
  49. ): Promise<DashboardDetails> {
  50. const promise: Promise<DashboardDetails> = api.requestPromise(
  51. `/organizations/${orgId}/dashboards/${dashboardId}/`,
  52. {
  53. method: 'GET',
  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 load dashboard'));
  63. }
  64. });
  65. return promise;
  66. }
  67. export function updateDashboard(
  68. api: Client,
  69. orgId: string,
  70. dashboard: DashboardDetails
  71. ): Promise<DashboardDetails> {
  72. const data = {
  73. title: dashboard.title,
  74. widgets: dashboard.widgets.map(widget => omit(widget, ['tempId'])),
  75. };
  76. const promise: Promise<DashboardDetails> = api.requestPromise(
  77. `/organizations/${orgId}/dashboards/${dashboard.id}/`,
  78. {
  79. method: 'PUT',
  80. data,
  81. }
  82. );
  83. promise.catch(response => {
  84. const errorResponse = response?.responseJSON ?? null;
  85. if (errorResponse) {
  86. const errors = flattenErrors(errorResponse, {});
  87. addErrorMessage(errors[Object.keys(errors)[0]]);
  88. } else {
  89. addErrorMessage(t('Unable to update dashboard'));
  90. }
  91. });
  92. return promise;
  93. }
  94. export function deleteDashboard(
  95. api: Client,
  96. orgId: string,
  97. dashboardId: string
  98. ): Promise<undefined> {
  99. const promise: Promise<undefined> = api.requestPromise(
  100. `/organizations/${orgId}/dashboards/${dashboardId}/`,
  101. {
  102. method: 'DELETE',
  103. }
  104. );
  105. promise.catch(response => {
  106. const errorResponse = response?.responseJSON ?? null;
  107. if (errorResponse) {
  108. const errors = flattenErrors(errorResponse, {});
  109. addErrorMessage(errors[Object.keys(errors)[0]]);
  110. } else {
  111. addErrorMessage(t('Unable to delete dashboard'));
  112. }
  113. });
  114. return promise;
  115. }
  116. export function validateWidget(
  117. api: Client,
  118. orgId: string,
  119. widget: Widget
  120. ): Promise<undefined> {
  121. const promise: Promise<undefined> = api.requestPromise(
  122. `/organizations/${orgId}/dashboards/widgets/`,
  123. {
  124. method: 'POST',
  125. data: widget,
  126. }
  127. );
  128. return promise;
  129. }