grafana/public/app/features/playlist/api.ts
Uchechukwu Obasi 4e8ab0512c
PlayList: add delete functionality to remove playlist (#38120)
* WIP: add delete functionality to playlist

* fixes deleted item to be removed instantly without manual refresh

* update confirmModal to reference playlist name

* refactor confirmModal message to be clear enough

* WIP: some unit tests for the playlistPage

* added more tests and did some cleanup

* some code refactoring

* adds ability for user roles to control playlist delete

* some abstraction to cleanup code

* modified alert message for delete button to correspond with action

* tried a better approach to modify the alert message

* fixes playlist lookup on each render

* update handlers to not use anonymous function

* exposed getBackendSrv().get api to fetch all playlist

* used better naming convention

* removes unecessary async/await construct

* some code refactoring

* used the correct param structure
2021-09-01 13:23:57 +01:00

38 lines
1.4 KiB
TypeScript

import { getBackendSrv } from '@grafana/runtime';
import { Playlist, PlaylistDTO } from './types';
import { dispatch } from '../../store/store';
import { notifyApp } from '../../core/actions';
import { createErrorNotification, createSuccessNotification } from '../../core/copy/appNotification';
export async function createPlaylist(playlist: Playlist) {
await withErrorHandling(() => getBackendSrv().post('/api/playlists', playlist));
}
export async function updatePlaylist(id: number, playlist: Playlist) {
await withErrorHandling(() => getBackendSrv().put(`/api/playlists/${id}`, playlist));
}
export async function deletePlaylist(id: number) {
await withErrorHandling(() => getBackendSrv().delete(`/api/playlists/${id}`), 'Playlist deleted');
}
export async function getPlaylist(id: number): Promise<Playlist> {
const result: Playlist = await getBackendSrv().get(`/api/playlists/${id}`);
return result;
}
export async function getAllPlaylist(query: string): Promise<PlaylistDTO[]> {
const result: PlaylistDTO[] = await getBackendSrv().get('/api/playlists/', { query });
return result;
}
async function withErrorHandling(apiCall: () => Promise<void>, message = 'Playlist saved') {
try {
await apiCall();
dispatch(notifyApp(createSuccessNotification(message)));
} catch (e) {
dispatch(notifyApp(createErrorNotification('Unable to save playlist', e)));
}
}