actions.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { ThunkAction } from 'redux-thunk';
  2. import { getBackendSrv } from 'app/core/services/backend_srv';
  3. import { StoreState, ApiKey } from 'app/types';
  4. export enum ActionTypes {
  5. LoadApiKeys = 'LOAD_API_KEYS',
  6. SetApiKeysSearchQuery = 'SET_API_KEYS_SEARCH_QUERY',
  7. }
  8. export interface LoadApiKeysAction {
  9. type: ActionTypes.LoadApiKeys;
  10. payload: ApiKey[];
  11. }
  12. export interface SetSearchQueryAction {
  13. type: ActionTypes.SetApiKeysSearchQuery;
  14. payload: string;
  15. }
  16. export type Action = LoadApiKeysAction | SetSearchQueryAction;
  17. type ThunkResult<R> = ThunkAction<R, StoreState, undefined, Action>;
  18. const apiKeysLoaded = (apiKeys: ApiKey[]): LoadApiKeysAction => ({
  19. type: ActionTypes.LoadApiKeys,
  20. payload: apiKeys,
  21. });
  22. export function addApiKey(apiKey: ApiKey, openModal: (key: string) => void): ThunkResult<void> {
  23. return async dispatch => {
  24. const result = await getBackendSrv().post('/api/auth/keys', apiKey);
  25. dispatch(setSearchQuery(''));
  26. dispatch(loadApiKeys());
  27. openModal(result.key);
  28. };
  29. }
  30. export function loadApiKeys(): ThunkResult<void> {
  31. return async dispatch => {
  32. const response = await getBackendSrv().get('/api/auth/keys');
  33. dispatch(apiKeysLoaded(response));
  34. };
  35. }
  36. export function deleteApiKey(id: number): ThunkResult<void> {
  37. return async dispatch => {
  38. getBackendSrv()
  39. .delete('/api/auth/keys/' + id)
  40. .then(dispatch(loadApiKeys()));
  41. };
  42. }
  43. export const setSearchQuery = (searchQuery: string): SetSearchQueryAction => ({
  44. type: ActionTypes.SetApiKeysSearchQuery,
  45. payload: searchQuery,
  46. });