Skip to content

Commit

Permalink
Merge pull request #3 from LinumLabs/feature/rr/FAIR-84
Browse files Browse the repository at this point in the history
[FAIR-84] add simple context for user handling and hooks
  • Loading branch information
Rocco Russo authored Dec 17, 2021
2 parents 4db1bbe + b7981f5 commit 72cb110
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
8 changes: 6 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"editor.formatOnSave": true
}
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
12 changes: 12 additions & 0 deletions contexts/User.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createContext, Dispatch, SetStateAction } from 'react';

export type UserContextProps = {
user: any;
setUser?: Dispatch<SetStateAction<any>>;
};

export const UserContext = createContext<UserContextProps>({
user: null,
});

export default UserContext.Provider;
6 changes: 6 additions & 0 deletions hooks/useUser.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { UserContext } from 'contexts/User';
import { useContext } from 'react';

const useUser = () => useContext(UserContext);

export default useUser;
6 changes: 5 additions & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { useState } from 'react';
import { AppProps } from 'next/app';
import Head from 'next/head';
import AppProvider from 'contexts/App';
import UserProvider from 'contexts/User';

const MyApp = ({ Component, pageProps }: AppProps) => {
const [theme, setTheme] = useState('dark');
const [user, setUser] = useState(null);

return (
<>
Expand All @@ -14,7 +16,9 @@ const MyApp = ({ Component, pageProps }: AppProps) => {
<meta name="viewport" content="width=device-width, initial-scale=1" />
</Head>
<AppProvider value={{ theme, setTheme }}>
<Component {...pageProps} />
<UserProvider value={{ user, setUser }}>
<Component {...pageProps} />
</UserProvider>
</AppProvider>
</>
);
Expand Down
4 changes: 4 additions & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { NextPage } from 'next';
import Layout from 'components/Layout';
import useUser from 'hooks/useUser';

const Home: NextPage = () => {
const { user } = useUser();

return (
<Layout>
<h1 className="text-center">Fairdrive</h1>
{!user && <h3 className="text-center">user loggeout</h3>}
</Layout>
);
};
Expand Down

0 comments on commit 72cb110

Please sign in to comment.