grafana/public/app/features/library-panels/components/LibraryPanelsView/actions.ts
Hugo Häggmark f1b2c750e5
LibraryElements: Adds library elements api and tables (#33741)
* WIP: intial structure

* Refactor: adds create library element endpoint

* Feature: adds delete library element

* wip

* Refactor: adds get api

* Refactor: adds get all api

* Refactor: adds patch api

* Refactor: changes to library_element_connection

* Refactor: add get connections api

* wip: in the middle of refactor

* wip

* Refactor: consolidating both api:s

* Refactor: points front end to library elements api

* Tests: Fixes broken test

* Fix: fixes delete library elements in folder and adds tests

* Refactor: changes order of tabs in manage folder

* Refactor: fixes so link does not cover whole card

* Update pkg/services/libraryelements/libraryelements.go

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Update pkg/services/libraryelements/libraryelements_permissions_test.go

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Update pkg/services/libraryelements/database.go

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Chore: changes after PR comments

* Update libraryelements.go

* Chore: updates after PR comments

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
2021-05-11 07:10:19 +02:00

73 lines
2.4 KiB
TypeScript

import { Dispatch } from 'react';
import { AnyAction } from '@reduxjs/toolkit';
import { from, merge, of, Subscription, timer } from 'rxjs';
import { catchError, finalize, mapTo, mergeMap, share, takeUntil } from 'rxjs/operators';
import { deleteLibraryPanel as apiDeleteLibraryPanel, getLibraryPanels } from '../../state/api';
import { initialLibraryPanelsViewState, initSearch, searchCompleted } from './reducer';
type DispatchResult = (dispatch: Dispatch<AnyAction>) => void;
interface SearchArgs {
perPage: number;
page: number;
searchString: string;
sortDirection?: string;
panelFilter?: string[];
folderFilter?: string[];
currentPanelId?: string;
}
export function searchForLibraryPanels(args: SearchArgs): DispatchResult {
return function (dispatch) {
const subscription = new Subscription();
const dataObservable = from(
getLibraryPanels({
searchString: args.searchString,
perPage: args.perPage,
page: args.page,
excludeUid: args.currentPanelId,
sortDirection: args.sortDirection,
typeFilter: args.panelFilter,
folderFilter: args.folderFilter,
})
).pipe(
mergeMap(({ perPage, elements: libraryPanels, page, totalCount }) =>
of(searchCompleted({ libraryPanels, page, perPage, totalCount }))
),
catchError((err) => {
console.error(err);
return of(searchCompleted({ ...initialLibraryPanelsViewState, page: args.page, perPage: args.perPage }));
}),
finalize(() => subscription.unsubscribe()), // make sure we unsubscribe
share()
);
subscription.add(
// If 50ms without a response dispatch a loading state
// mapTo will translate the timer event into a loading state
// takeUntil will cancel the timer emit when first response is received on the dataObservable
merge(timer(50).pipe(mapTo(initSearch()), takeUntil(dataObservable)), dataObservable).subscribe(dispatch)
);
};
}
export function deleteLibraryPanel(uid: string, args: SearchArgs): DispatchResult {
return async function (dispatch) {
try {
await apiDeleteLibraryPanel(uid);
searchForLibraryPanels(args)(dispatch);
} catch (e) {
console.error(e);
}
};
}
export function asyncDispatcher(dispatch: Dispatch<AnyAction>) {
return function (action: any) {
if (action instanceof Function) {
return action(dispatch);
}
return dispatch(action);
};
}