AdminListOrgsPage.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React, { useEffect } from 'react';
  2. import { useSelector } from 'react-redux';
  3. import useAsyncFn from 'react-use/lib/useAsyncFn';
  4. import { getBackendSrv } from '@grafana/runtime';
  5. import { LinkButton } from '@grafana/ui';
  6. import Page from 'app/core/components/Page/Page';
  7. import { getNavModel } from 'app/core/selectors/navModel';
  8. import { contextSrv } from 'app/core/services/context_srv';
  9. import { AccessControlAction } from 'app/types';
  10. import { StoreState } from 'app/types/store';
  11. import { AdminOrgsTable } from './AdminOrgsTable';
  12. const deleteOrg = async (orgId: number) => {
  13. return await getBackendSrv().delete('/api/orgs/' + orgId);
  14. };
  15. const getOrgs = async () => {
  16. return await getBackendSrv().get('/api/orgs');
  17. };
  18. const getErrorMessage = (error: any) => {
  19. return error?.data?.message || 'An unexpected error happened.';
  20. };
  21. export default function AdminListOrgsPages() {
  22. const navIndex = useSelector((state: StoreState) => state.navIndex);
  23. const navModel = getNavModel(navIndex, 'global-orgs');
  24. const [state, fetchOrgs] = useAsyncFn(async () => await getOrgs(), []);
  25. const canCreateOrg = contextSrv.hasPermission(AccessControlAction.OrgsCreate);
  26. useEffect(() => {
  27. fetchOrgs();
  28. }, [fetchOrgs]);
  29. return (
  30. <Page navModel={navModel}>
  31. <Page.Contents>
  32. <>
  33. <div className="page-action-bar">
  34. <div className="page-action-bar__spacer" />
  35. <LinkButton icon="plus" href="org/new" disabled={!canCreateOrg}>
  36. New org
  37. </LinkButton>
  38. </div>
  39. {state.error && getErrorMessage(state.error)}
  40. {state.loading && 'Fetching organizations'}
  41. {state.value && (
  42. <AdminOrgsTable
  43. orgs={state.value}
  44. onDelete={(orgId) => {
  45. deleteOrg(orgId).then(() => fetchOrgs());
  46. }}
  47. />
  48. )}
  49. </>
  50. </Page.Contents>
  51. </Page>
  52. );
  53. }