actions.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { getBackendSrv } from 'app/core/services/backend_srv';
  2. import { ApiKey, ThunkResult } from 'app/types';
  3. import { apiKeysLoaded, includeExpiredToggled, isFetching, setSearchQuery } from './reducers';
  4. export function addApiKey(apiKey: ApiKey, openModal: (key: string) => void): ThunkResult<void> {
  5. return async (dispatch) => {
  6. const result = await getBackendSrv().post('/api/auth/keys', apiKey);
  7. dispatch(setSearchQuery(''));
  8. dispatch(loadApiKeys());
  9. openModal(result.key);
  10. };
  11. }
  12. export function loadApiKeys(): ThunkResult<void> {
  13. return async (dispatch) => {
  14. dispatch(isFetching());
  15. const [keys, keysIncludingExpired] = await Promise.all([
  16. getBackendSrv().get('/api/auth/keys?includeExpired=false&accesscontrol=true'),
  17. getBackendSrv().get('/api/auth/keys?includeExpired=true&accesscontrol=true'),
  18. ]);
  19. dispatch(apiKeysLoaded({ keys, keysIncludingExpired }));
  20. };
  21. }
  22. export function deleteApiKey(id: number): ThunkResult<void> {
  23. return async (dispatch) => {
  24. getBackendSrv()
  25. .delete(`/api/auth/keys/${id}`)
  26. .then(() => dispatch(loadApiKeys()));
  27. };
  28. }
  29. export function toggleIncludeExpired(): ThunkResult<void> {
  30. return (dispatch) => {
  31. dispatch(includeExpiredToggled());
  32. };
  33. }