dashboards.tsx 3.0 KB

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