dashboards.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 fetchDashboard(
  30. api: Client,
  31. orgId: string,
  32. dashboardId: string
  33. ): Promise<DashboardDetails> {
  34. const promise: Promise<DashboardDetails> = api.requestPromise(
  35. `/organizations/${orgId}/dashboards/${dashboardId}/`,
  36. {
  37. method: 'GET',
  38. }
  39. );
  40. promise.catch(response => {
  41. const errorResponse = response?.responseJSON ?? null;
  42. if (errorResponse) {
  43. addErrorMessage(errorResponse);
  44. } else {
  45. addErrorMessage(t('Unable to load dashboard'));
  46. }
  47. });
  48. return promise;
  49. }
  50. export function updateDashboard(
  51. api: Client,
  52. orgId: string,
  53. dashboard: DashboardDetails
  54. ): Promise<DashboardDetails> {
  55. const data = {
  56. title: dashboard.title,
  57. widgets: dashboard.widgets,
  58. };
  59. const promise: Promise<DashboardDetails> = api.requestPromise(
  60. `/organizations/${orgId}/dashboards/${dashboard.id}/`,
  61. {
  62. method: 'PUT',
  63. data,
  64. }
  65. );
  66. promise.catch(response => {
  67. const errorResponse = response?.responseJSON ?? null;
  68. if (errorResponse) {
  69. addErrorMessage(errorResponse);
  70. } else {
  71. addErrorMessage(t('Unable to update dashboard'));
  72. }
  73. });
  74. return promise;
  75. }
  76. export function deleteDashboard(
  77. api: Client,
  78. orgId: string,
  79. dashboardId: string
  80. ): Promise<undefined> {
  81. const promise: Promise<undefined> = api.requestPromise(
  82. `/organizations/${orgId}/dashboards/${dashboardId}/`,
  83. {
  84. method: 'DELETE',
  85. }
  86. );
  87. promise.catch(response => {
  88. const errorResponse = response?.responseJSON ?? null;
  89. if (errorResponse) {
  90. addErrorMessage(errorResponse);
  91. } else {
  92. addErrorMessage(t('Unable to delete dashboard'));
  93. }
  94. });
  95. return promise;
  96. }
  97. export function validateWidget(
  98. api: Client,
  99. orgId: string,
  100. widget: Widget
  101. ): Promise<undefined> {
  102. const promise: Promise<undefined> = api.requestPromise(
  103. `/organizations/${orgId}/dashboards/widgets/`,
  104. {
  105. method: 'POST',
  106. data: widget,
  107. }
  108. );
  109. return promise;
  110. }