dashboards.tsx 3.2 KB

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