actions.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { getBackendSrv } from '@grafana/runtime';
  2. import { updateConfigurationSubtitle } from 'app/core/actions';
  3. import { ThunkResult } from 'app/types';
  4. import { organizationLoaded, userOrganizationsLoaded } from './reducers';
  5. type OrganizationDependencies = { getBackendSrv: typeof getBackendSrv };
  6. export function loadOrganization(
  7. dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv }
  8. ): ThunkResult<any> {
  9. return async (dispatch) => {
  10. const organizationResponse = await dependencies.getBackendSrv().get('/api/org');
  11. dispatch(organizationLoaded(organizationResponse));
  12. return organizationResponse;
  13. };
  14. }
  15. export function updateOrganization(
  16. dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv }
  17. ): ThunkResult<any> {
  18. return async (dispatch, getStore) => {
  19. const organization = getStore().organization.organization;
  20. await dependencies.getBackendSrv().put('/api/org', { name: organization.name });
  21. dispatch(updateConfigurationSubtitle(organization.name));
  22. dispatch(loadOrganization(dependencies));
  23. };
  24. }
  25. export function setUserOrganization(
  26. orgId: number,
  27. dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv }
  28. ): ThunkResult<any> {
  29. return async (dispatch) => {
  30. const organizationResponse = await dependencies.getBackendSrv().post('/api/user/using/' + orgId);
  31. dispatch(updateConfigurationSubtitle(organizationResponse.name));
  32. };
  33. }
  34. export function createOrganization(
  35. newOrg: { name: string },
  36. dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv }
  37. ): ThunkResult<any> {
  38. return async (dispatch) => {
  39. const result = await dependencies.getBackendSrv().post('/api/orgs/', newOrg);
  40. dispatch(setUserOrganization(result.orgId));
  41. };
  42. }
  43. export function getUserOrganizations(
  44. dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv }
  45. ): ThunkResult<any> {
  46. return async (dispatch) => {
  47. const result = await dependencies.getBackendSrv().get('/api/user/orgs');
  48. dispatch(userOrganizationsLoaded(result));
  49. return result;
  50. };
  51. }