-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add global state example via context
- Loading branch information
1 parent
d79bb98
commit a232aeb
Showing
8 changed files
with
188 additions
and
5 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,7 @@ | ||
import { IAuthButton } from './AuthButton'; | ||
|
||
const base: IAuthButton = {}; | ||
|
||
export const mockAuthButtonProps = { | ||
base, | ||
}; |
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,2 @@ | ||
.component { | ||
} |
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 @@ | ||
import { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
import AuthButton, { IAuthButton } from './AuthButton'; | ||
import { mockAuthButtonProps } from './AuthButton.mocks'; | ||
|
||
export default { | ||
title: 'buttons/AuthButton', | ||
component: AuthButton, | ||
// More on argTypes: https://storybook.js.org/docs/react/api/argtypes | ||
argTypes: {}, | ||
} as ComponentMeta<typeof AuthButton>; | ||
|
||
// More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args | ||
const Template: ComponentStory<typeof AuthButton> = (args) => ( | ||
<AuthButton {...args} /> | ||
); | ||
|
||
export const Base = Template.bind({}); | ||
// More on args: https://storybook.js.org/docs/react/writing-stories/args | ||
|
||
Base.args = { | ||
...mockAuthButtonProps.base, | ||
} as IAuthButton; |
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,21 @@ | ||
import { useContext } from 'react'; | ||
import AuthContext from '../../../state/auth/AuthContext'; | ||
import styles from './AuthButton.module.css'; | ||
|
||
export interface IAuthButton extends React.ComponentPropsWithoutRef<'button'> {} | ||
|
||
const AuthButton: React.FC<IAuthButton> = ({ className, ...buttonProps }) => { | ||
const { authenticated, login, logOut } = useContext(AuthContext); | ||
|
||
return ( | ||
<button | ||
onClick={authenticated ? logOut : login} | ||
className={`${styles.container} ${className} border-1 p-2 px-4 sm:px-6 bg-blue-500 rounded text-white w-28`} | ||
{...buttonProps} | ||
> | ||
{authenticated ? 'Sign Out' : 'Sign In'} | ||
</button> | ||
); | ||
}; | ||
|
||
export default AuthButton; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { createContext, useState } from 'react'; | ||
|
||
interface IAuthContext { | ||
authenticated: boolean; | ||
login: () => void; | ||
logOut: () => void; | ||
} | ||
|
||
const defaultValue: IAuthContext = { | ||
authenticated: false, | ||
login: () => undefined, | ||
logOut: () => undefined, | ||
}; | ||
|
||
const AuthContext = createContext<IAuthContext>(defaultValue); | ||
|
||
export const AuthProvider: React.FC = ({ children }) => { | ||
const [authenticated, setAuthenticated] = useState( | ||
defaultValue.authenticated | ||
); | ||
const login = () => setAuthenticated(true); | ||
const logOut = () => setAuthenticated(false); | ||
|
||
return ( | ||
<AuthContext.Provider value={{ authenticated, login, logOut }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export default AuthContext; |