dashboards.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. ): Promise<DashboardDetails> {
  10. const {title, widgets} = newDashboard;
  11. const promise: Promise<DashboardDetails> = api.requestPromise(
  12. `/organizations/${orgId}/dashboards/`,
  13. {
  14. method: 'POST',
  15. data: {title, widgets},
  16. }
  17. );
  18. promise.catch(response => {
  19. const errorResponse = response?.responseJSON ?? null;
  20. if (errorResponse) {
  21. addErrorMessage(errorResponse);
  22. } else {
  23. addErrorMessage(t('Unable to create dashboard'));
  24. }
  25. });
  26. return promise;
  27. }
  28. export function updateDashboard(
  29. api: Client,
  30. orgId: string,
  31. dashboard: DashboardDetails
  32. ): Promise<DashboardDetails> {
  33. const data = {
  34. title: dashboard.title,
  35. widgets: dashboard.widgets,
  36. };
  37. const promise: Promise<DashboardDetails> = api.requestPromise(
  38. `/organizations/${orgId}/dashboards/${dashboard.id}/`,
  39. {
  40. method: 'PUT',
  41. data,
  42. }
  43. );
  44. promise.catch(response => {
  45. const errorResponse = response?.responseJSON ?? null;
  46. if (errorResponse) {
  47. addErrorMessage(errorResponse);
  48. } else {
  49. addErrorMessage(t('Unable to update dashboard'));
  50. }
  51. });
  52. return promise;
  53. }
  54. export function deleteDashboard(
  55. api: Client,
  56. orgId: string,
  57. dashboardId: string
  58. ): Promise<undefined> {
  59. const promise: Promise<undefined> = api.requestPromise(
  60. `/organizations/${orgId}/dashboards/${dashboardId}/`,
  61. {
  62. method: 'DELETE',
  63. }
  64. );
  65. promise.catch(response => {
  66. const errorResponse = response?.responseJSON ?? null;
  67. if (errorResponse) {
  68. addErrorMessage(errorResponse);
  69. } else {
  70. addErrorMessage(t('Unable to delete dashboard'));
  71. }
  72. });
  73. return promise;
  74. }
  75. export function validateWidget(
  76. api: Client,
  77. orgId: string,
  78. widget: Widget
  79. ): Promise<undefined> {
  80. const promise: Promise<undefined> = api.requestPromise(
  81. `/organizations/${orgId}/dashboards/widgets/`,
  82. {
  83. method: 'POST',
  84. data: widget,
  85. }
  86. );
  87. return promise;
  88. }