2021-03-23 01:45:04 -05:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
|
|
|
|
|
|
|
import { notifyApp } from '../../core/actions';
|
|
|
|
import { createErrorNotification, createSuccessNotification } from '../../core/copy/appNotification';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { dispatch } from '../../store/store';
|
|
|
|
|
|
|
|
import { Playlist, PlaylistDTO } from './types';
|
2021-03-23 01:45:04 -05:00
|
|
|
|
|
|
|
export async function createPlaylist(playlist: Playlist) {
|
2021-09-01 07:23:57 -05:00
|
|
|
await withErrorHandling(() => getBackendSrv().post('/api/playlists', playlist));
|
2021-03-23 01:45:04 -05:00
|
|
|
}
|
|
|
|
|
2022-06-14 14:32:52 -05:00
|
|
|
export async function updatePlaylist(uid: string, playlist: Playlist) {
|
|
|
|
await withErrorHandling(() => getBackendSrv().put(`/api/playlists/${uid}`, playlist));
|
2021-09-01 07:23:57 -05:00
|
|
|
}
|
|
|
|
|
2022-06-14 14:32:52 -05:00
|
|
|
export async function deletePlaylist(uid: string) {
|
|
|
|
await withErrorHandling(() => getBackendSrv().delete(`/api/playlists/${uid}`), 'Playlist deleted');
|
2021-03-23 01:45:04 -05:00
|
|
|
}
|
|
|
|
|
2022-06-14 14:32:52 -05:00
|
|
|
export async function getPlaylist(uid: string): Promise<Playlist> {
|
|
|
|
const result: Playlist = await getBackendSrv().get(`/api/playlists/${uid}`);
|
2021-03-23 01:45:04 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-01 07:23:57 -05:00
|
|
|
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') {
|
2021-03-23 01:45:04 -05:00
|
|
|
try {
|
|
|
|
await apiCall();
|
2021-09-01 07:23:57 -05:00
|
|
|
dispatch(notifyApp(createSuccessNotification(message)));
|
2021-03-23 01:45:04 -05:00
|
|
|
} catch (e) {
|
2022-06-15 02:59:29 -05:00
|
|
|
if (e instanceof Error) {
|
|
|
|
dispatch(notifyApp(createErrorNotification('Unable to save playlist', e)));
|
|
|
|
}
|
2021-03-23 01:45:04 -05:00
|
|
|
}
|
|
|
|
}
|