@ -1,10 +1,10 @@
import React , { FC , useState } from 'react' ;
import React , { FC } from 'react' ;
import Page from 'app/core/components/Page/Page' ;
import { getBackendSrv , config } from '@grafana/runtime' ;
import { UserOrg } from 'app/types' ;
import { useAsync } from 'react-use' ;
import { config } from '@grafana/runtime' ;
import { StoreState , UserOrg } from 'app/types' ;
import { useEffectOnce } from 'react-use' ;
import { Button , HorizontalGroup } from '@grafana/ui' ;
import { setUserOrganization } from './state/actions' ;
import { getUserOrganizations , setUserOrganization } from './state/actions' ;
import { connect , ConnectedProps } from 'react-redux' ;
const navModel = {
@ -18,29 +18,31 @@ const navModel = {
} ,
} ;
const getUserOrgs = async ( ) = > {
return await getBackendSrv ( ) . get ( '/api/user/orgs' ) ;
const mapStateToProps = ( state : StoreState ) = > {
return {
userOrgs : state.organization.userOrgs ,
} ;
} ;
const mapDispatchToProps = {
setUserOrganization ,
getUserOrganizations ,
} ;
const connector = connect ( null , mapDispatchToProps ) ;
const connector = connect ( mapStateToProps , mapDispatchToProps ) ;
type Props = ConnectedProps < typeof connector > ;
export const SelectOrgPage : FC < Props > = ( { setUserOrganization } ) = > {
const [ orgs , setOrgs ] = useState < UserOrg [ ] > ( ) ;
export const SelectOrgPage : FC < Props > = ( { setUserOrganization , getUserOrganizations , userOrgs } ) = > {
const setUserOrg = async ( org : UserOrg ) = > {
await setUserOrganization ( org . orgId ) ;
window . location . href = config . appSubUrl + '/' ;
} ;
useAsync ( async ( ) = > {
setOrgs ( await getUserOrgs ( ) ) ;
} , [ ] ) ;
useEffectOnce ( ( ) = > {
getUserOrganizations ( ) ;
} ) ;
return (
< Page navModel = { navModel } >
< Page.Contents >
@ -50,8 +52,8 @@ export const SelectOrgPage: FC<Props> = ({ setUserOrganization }) => {
now . You can change this later at any time .
< / p >
< HorizontalGroup wrap >
{ o rgs &&
o rgs. map ( ( org ) = > (
{ userO rgs &&
userO rgs. map ( ( org ) = > (
< Button key = { org . orgId } icon = "signin" onClick = { ( ) = > setUserOrg ( org ) } >
{ org . name }
< / Button >