Skip to content

Commit

Permalink
Merge pull request #72 from the-collab-lab/sign-loading
Browse files Browse the repository at this point in the history
Loading after sign in
  • Loading branch information
borjaMarti committed Apr 4, 2024
2 parents 40fdec7 + beaa03c commit abe1764
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
14 changes: 12 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function App() {
* This custom hook holds info about the current signed in user.
* Check ./api/useAuth.jsx for its implementation.
*/
const { user } = useAuth();
const { user, isLoadingUser } = useAuth();
const userId = user?.uid;
const userEmail = user?.email;

Expand All @@ -45,7 +45,17 @@ export function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Layout listPath={listPath} lists={lists} />}>
<Route
path="/"
element={
<Layout
listPath={listPath}
lists={lists}
user={user}
isLoadingUser={isLoadingUser}
/>
}
>
<Route
index
element={
Expand Down
5 changes: 4 additions & 1 deletion src/api/useAuth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ export const SignOut = () => {
*/
export const useAuth = () => {
const [user, setUser] = useState(null);
const [isLoadingUser, setIsLoadingUser] = useState(false);

useEffect(() => {
setIsLoadingUser(true);
auth.onAuthStateChanged((user) => {
setIsLoadingUser(false);
setUser(user);
if (user) {
addUserToDatabase(user);
}
});
}, []);

return { user };
return { user, isLoadingUser };
};
2 changes: 1 addition & 1 deletion src/components/Loading.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
.spinner {
width: 5rem; /* 1 */
padding: 0.5rem; /* 2 */
background: var(--color-accent); /* 3 */
background: #6966a8; /* 3 */

aspect-ratio: 1;
border-radius: 50%;
Expand Down
12 changes: 6 additions & 6 deletions src/views/Layout.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Outlet } from 'react-router-dom';
import { auth } from '../api/config.js';
import { SignIn, useAuth } from '../api/useAuth.jsx';
import { SignIn } from '../api/useAuth.jsx';
import { NavBar } from '../components/NavBar/NavBar.jsx';
import Groceries from '../assets/groceries.png';
import Loading from '../components/Loading.jsx';

export function Layout({ lists, listPath }) {
const { user } = useAuth();

export function Layout({ lists, listPath, user, isLoadingUser }) {
const handleClickSignIn = () => {
SignIn();
};
Expand All @@ -15,7 +13,9 @@ export function Layout({ lists, listPath }) {
<div className="w-screen flex flex-col text-poppins min-w-96 bg-puurWhite">
<NavBar user={user} lists={lists} listPath={listPath} />
<main className="h-screen w-full lg:pt-16 xl:w-9/12 xl:mx-auto">
{!!user ? (
{isLoadingUser ? (
<Loading />
) : !!user ? (
<Outlet />
) : (
<div className="flex flex-col justify-items-center pt-12 lg:justify-between lg:m-20 lg:pt-0 lg:flex-row">
Expand Down

0 comments on commit abe1764

Please sign in to comment.