123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- import omit from 'lodash/omit';
- import {addErrorMessage} from 'sentry/actionCreators/indicator';
- import {Client} from 'sentry/api';
- import {t} from 'sentry/locale';
- import {DashboardDetails, Widget} from 'sentry/views/dashboardsV2/types';
- export function createDashboard(
- api: Client,
- orgId: string,
- newDashboard: DashboardDetails,
- duplicate?: boolean
- ): Promise<DashboardDetails> {
- const {title, widgets} = newDashboard;
- const promise: Promise<DashboardDetails> = api.requestPromise(
- `/organizations/${orgId}/dashboards/`,
- {
- method: 'POST',
- data: {title, widgets: widgets.map(widget => omit(widget, ['tempId'])), duplicate},
- }
- );
- promise.catch(response => {
- const errorResponse = response?.responseJSON ?? null;
- if (errorResponse) {
- addErrorMessage(errorResponse);
- } else {
- addErrorMessage(t('Unable to create dashboard'));
- }
- });
- return promise;
- }
- export function updateDashboardVisit(
- api: Client,
- orgId: string,
- dashboardId: string | string[]
- ): Promise<void> {
- const promise = api.requestPromise(
- `/organizations/${orgId}/dashboards/${dashboardId}/visit/`,
- {
- method: 'POST',
- }
- );
- return promise;
- }
- export function fetchDashboard(
- api: Client,
- orgId: string,
- dashboardId: string
- ): Promise<DashboardDetails> {
- const promise: Promise<DashboardDetails> = api.requestPromise(
- `/organizations/${orgId}/dashboards/${dashboardId}/`,
- {
- method: 'GET',
- }
- );
- promise.catch(response => {
- const errorResponse = response?.responseJSON ?? null;
- if (errorResponse) {
- addErrorMessage(errorResponse);
- } else {
- addErrorMessage(t('Unable to load dashboard'));
- }
- });
- return promise;
- }
- export function updateDashboard(
- api: Client,
- orgId: string,
- dashboard: DashboardDetails
- ): Promise<DashboardDetails> {
- const data = {
- title: dashboard.title,
- widgets: dashboard.widgets.map(widget => omit(widget, ['tempId'])),
- };
- const promise: Promise<DashboardDetails> = api.requestPromise(
- `/organizations/${orgId}/dashboards/${dashboard.id}/`,
- {
- method: 'PUT',
- data,
- }
- );
- promise.catch(response => {
- const errorResponse = response?.responseJSON ?? null;
- if (errorResponse) {
- addErrorMessage(errorResponse);
- } else {
- addErrorMessage(t('Unable to update dashboard'));
- }
- });
- return promise;
- }
- export function deleteDashboard(
- api: Client,
- orgId: string,
- dashboardId: string
- ): Promise<undefined> {
- const promise: Promise<undefined> = api.requestPromise(
- `/organizations/${orgId}/dashboards/${dashboardId}/`,
- {
- method: 'DELETE',
- }
- );
- promise.catch(response => {
- const errorResponse = response?.responseJSON ?? null;
- if (errorResponse) {
- addErrorMessage(errorResponse);
- } else {
- addErrorMessage(t('Unable to delete dashboard'));
- }
- });
- return promise;
- }
- export function validateWidget(
- api: Client,
- orgId: string,
- widget: Widget
- ): Promise<undefined> {
- const promise: Promise<undefined> = api.requestPromise(
- `/organizations/${orgId}/dashboards/widgets/`,
- {
- method: 'POST',
- data: widget,
- }
- );
- return promise;
- }
|