SelectOrgPage.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React, { FC } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { useEffectOnce } from 'react-use';
  4. import { config } from '@grafana/runtime';
  5. import { Button, HorizontalGroup } from '@grafana/ui';
  6. import Page from 'app/core/components/Page/Page';
  7. import { StoreState, UserOrg } from 'app/types';
  8. import { getUserOrganizations, setUserOrganization } from './state/actions';
  9. const navModel = {
  10. main: {
  11. icon: 'grafana',
  12. subTitle: 'Preferences',
  13. text: 'Select active organization',
  14. },
  15. node: {
  16. text: 'Select active organization',
  17. },
  18. };
  19. const mapStateToProps = (state: StoreState) => {
  20. return {
  21. userOrgs: state.organization.userOrgs,
  22. };
  23. };
  24. const mapDispatchToProps = {
  25. setUserOrganization,
  26. getUserOrganizations,
  27. };
  28. const connector = connect(mapStateToProps, mapDispatchToProps);
  29. type Props = ConnectedProps<typeof connector>;
  30. export const SelectOrgPage: FC<Props> = ({ setUserOrganization, getUserOrganizations, userOrgs }) => {
  31. const setUserOrg = async (org: UserOrg) => {
  32. await setUserOrganization(org.orgId);
  33. window.location.href = config.appSubUrl + '/';
  34. };
  35. useEffectOnce(() => {
  36. getUserOrganizations();
  37. });
  38. return (
  39. <Page navModel={navModel}>
  40. <Page.Contents>
  41. <div>
  42. <p>
  43. You have been invited to another organization! Please select which organization that you want to use right
  44. now. You can change this later at any time.
  45. </p>
  46. <HorizontalGroup wrap>
  47. {userOrgs &&
  48. userOrgs.map((org) => (
  49. <Button key={org.orgId} icon="signin" onClick={() => setUserOrg(org)}>
  50. {org.name}
  51. </Button>
  52. ))}
  53. </HorizontalGroup>
  54. </div>
  55. </Page.Contents>
  56. </Page>
  57. );
  58. };
  59. export default connector(SelectOrgPage);