-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a useIsSignedIn custom hook for react (#3093)
Add signed in state core functionality Add a hook to check the signed in state Add documentation of custom hooks on read me Add tests for isSignedIn Update the react-contoso sample to use the mgt-react hook Add sample stories for HTML and React usages
- Loading branch information
Showing
12 changed files
with
149 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* ------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. | ||
* See License in the project root for license information. | ||
* ------------------------------------------------------------------------------------------- | ||
*/ | ||
|
||
import { expect } from '@open-wc/testing'; | ||
import { MockProvider } from '../mock/MockProvider'; | ||
import { ProviderState } from '../providers/IProvider'; | ||
import { Providers } from '../providers/Providers'; | ||
import { isSignedIn } from './isSignedIn'; | ||
|
||
describe('signedInState', () => { | ||
it('should change', () => { | ||
/* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
Providers.globalProvider = new MockProvider(false); | ||
expect(isSignedIn()).to.be.false; | ||
Providers.globalProvider.setState(ProviderState.SignedIn); | ||
expect(isSignedIn()).to.be.true; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* ------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. | ||
* See License in the project root for license information. | ||
* ------------------------------------------------------------------------------------------- | ||
*/ | ||
|
||
import { ProviderState } from '../providers/IProvider'; | ||
import { Providers } from '../providers/Providers'; | ||
|
||
/** | ||
* Checks the provider state if it's in signed in. | ||
* | ||
* @returns true if signed in, otherwise false. | ||
*/ | ||
export const isSignedIn = (): boolean => { | ||
const provider = Providers.globalProvider; | ||
return provider && provider.state === ProviderState.SignedIn; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* ------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. | ||
* See License in the project root for license information. | ||
* ------------------------------------------------------------------------------------------- | ||
*/ | ||
|
||
export * from './useIsSignedIn'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* ------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. | ||
* See License in the project root for license information. | ||
* ------------------------------------------------------------------------------------------- | ||
*/ | ||
|
||
import { isSignedIn, Providers } from '@microsoft/mgt-element'; | ||
import { useState, useEffect } from 'react'; | ||
|
||
/** | ||
* Hook to check if a user is signed in. | ||
* | ||
* @returns true if a user is signed on, otherwise false. | ||
*/ | ||
export const useIsSignedIn = (): [boolean] => { | ||
const [signedIn, setIsSignedIn] = useState(false); | ||
useEffect(() => { | ||
const updateState = () => { | ||
setIsSignedIn(isSignedIn()); | ||
}; | ||
|
||
Providers.onProviderUpdated(updateState); | ||
updateState(); | ||
|
||
return () => { | ||
Providers.removeProviderUpdatedListener(updateState); | ||
}; | ||
}, [setIsSignedIn]); | ||
return [signedIn]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* ------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. | ||
* See License in the project root for license information. | ||
* ------------------------------------------------------------------------------------------- | ||
*/ | ||
|
||
import { html } from 'lit'; | ||
import { withCodeEditor } from '../../.storybook/addons/codeEditorAddon/codeAddon'; | ||
|
||
export default { | ||
title: 'Samples / General / React', | ||
component: 'person-card', | ||
decorators: [withCodeEditor] | ||
}; | ||
|
||
export const Hooks = () => html` | ||
<p>This demonstrates how to use the <code>useIsSignedIn</code> hook in React.</p> | ||
<mgt-person-card person-query="me"></mgt-person-card> | ||
<react> | ||
import { PersonCard, useIsSignedIn } from '@microsoft/mgt-react'; | ||
const [isSignedIn] = useIsSignedIn(); | ||
export default () => ( | ||
isSignedIn ? <PersonCard personQuery="me"></PersonCard> : null | ||
); | ||
</react> | ||
`; |