discoverSavedQueries.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  2. import {Client} from 'sentry/api';
  3. import {t} from 'sentry/locale';
  4. import {NewQuery, SavedQuery} from 'sentry/types';
  5. export function fetchSavedQueries(
  6. api: Client,
  7. orgId: string,
  8. query: string = ''
  9. ): Promise<SavedQuery[]> {
  10. const promise: Promise<SavedQuery[]> = api.requestPromise(
  11. `/organizations/${orgId}/discover/saved/`,
  12. {
  13. method: 'GET',
  14. query: {query: `version:2 ${query}`.trim()},
  15. }
  16. );
  17. promise.catch(() => {
  18. addErrorMessage(t('Unable to load saved queries'));
  19. });
  20. return promise;
  21. }
  22. export function fetchSavedQuery(
  23. api: Client,
  24. orgId: string,
  25. queryId: string
  26. ): Promise<SavedQuery> {
  27. const promise: Promise<SavedQuery> = api.requestPromise(
  28. `/organizations/${orgId}/discover/saved/${queryId}/`,
  29. {
  30. method: 'GET',
  31. }
  32. );
  33. promise.catch(() => {
  34. addErrorMessage(t('Unable to load saved query'));
  35. });
  36. return promise;
  37. }
  38. export function createSavedQuery(
  39. api: Client,
  40. orgId: string,
  41. query: NewQuery
  42. ): Promise<SavedQuery> {
  43. const promise: Promise<SavedQuery> = api.requestPromise(
  44. `/organizations/${orgId}/discover/saved/`,
  45. {
  46. method: 'POST',
  47. data: query,
  48. }
  49. );
  50. promise.catch(() => {
  51. addErrorMessage(t('Unable to create your saved query'));
  52. });
  53. return promise;
  54. }
  55. export function updateSavedQuery(
  56. api: Client,
  57. orgId: string,
  58. query: NewQuery
  59. ): Promise<SavedQuery> {
  60. const promise: Promise<SavedQuery> = api.requestPromise(
  61. `/organizations/${orgId}/discover/saved/${query.id}/`,
  62. {
  63. method: 'PUT',
  64. data: query,
  65. }
  66. );
  67. promise.catch(() => {
  68. addErrorMessage(t('Unable to update your saved query'));
  69. });
  70. return promise;
  71. }
  72. export function updateSavedQueryVisit(
  73. orgId: string,
  74. queryId: string | string[]
  75. ): Promise<void> {
  76. // Create a new client so the request is not cancelled
  77. const api = new Client();
  78. const promise = api.requestPromise(
  79. `/organizations/${orgId}/discover/saved/${queryId}/visit/`,
  80. {
  81. method: 'POST',
  82. }
  83. );
  84. return promise;
  85. }
  86. export function deleteSavedQuery(
  87. api: Client,
  88. orgId: string,
  89. queryId: string
  90. ): Promise<void> {
  91. const promise: Promise<void> = api.requestPromise(
  92. `/organizations/${orgId}/discover/saved/${queryId}/`,
  93. {method: 'DELETE'}
  94. );
  95. promise.catch(() => {
  96. addErrorMessage(t('Unable to delete the saved query'));
  97. });
  98. return promise;
  99. }