Skip to content

Commit

Permalink
Merge pull request #5 from khanghy2130/add-displayname-to-side-panel
Browse files Browse the repository at this point in the history
Saving user name & improved auth handling
  • Loading branch information
khanghy2130 authored Mar 16, 2024
2 parents edaf332 + f559760 commit 1f86d51
Show file tree
Hide file tree
Showing 8 changed files with 1,634 additions and 1,740 deletions.
70 changes: 42 additions & 28 deletions app/components/SidePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,59 @@
import { Link } from '@remix-run/react';
import { Link } from "@remix-run/react";

import { useTheme } from '~/utils/Navbar/ThemeProvider';

import type { ContextProps } from '../utils/types/ContextProps.type';
import { useTheme } from "~/utils/Navbar/ThemeProvider";

import type { ContextProps } from "../utils/types/ContextProps.type";

type Props = {
sidePanelIsShown: boolean;
setSidePanelIsShown: React.Dispatch<React.SetStateAction<boolean>>;
user: ContextProps["user"];
supabase: ContextProps["supabase"]
supabase: ContextProps["supabase"];
};

export default function SidePanel(
{sidePanelIsShown, setSidePanelIsShown, user, supabase}
: Props){
export default function SidePanel({
sidePanelIsShown,
setSidePanelIsShown,
user,
supabase,
}: Props) {
const [theme, setTheme] = useTheme();
const toggleTheme = () => {
setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light"));
};

const logout = async () => {
const {error} = await supabase.auth.signOut();
const { error } = await supabase.auth.signOut();
if (error) alert("Error while logging out.");
}

return <div
className={(sidePanelIsShown ? "right-0" : "-right-96") +
` fixed h-screen transition-[right] ease-in-out duration-500
w-96 max-w-full bg-gray-800 flex flex-col items-center`
}>
<button className='btn'
onClick={()=>setSidePanelIsShown(false)}>CLOSE</button>
<button className='btn' onClick={toggleTheme}>Theme: {theme}</button>

{user? <div className='flex flex-col mt-12'>
<p>Logged in as {user.email}</p>
<button className='btn' onClick={logout}>Log out</button>
</div> : <Link to="/login">
<button className='btn'>Login</button>
</Link>}
};

</div>
}
return (
<div
className={
(sidePanelIsShown ? "right-0" : "-right-96") +
` fixed flex h-screen w-96 max-w-full
flex-col items-center bg-gray-800 transition-[right] duration-500 ease-in-out`
}
>
<button className="btn" onClick={() => setSidePanelIsShown(false)}>
CLOSE
</button>
<button className="btn" onClick={toggleTheme}>
Theme: {theme}
</button>

{user ? (
<div className="mt-12 flex flex-col">
<p>Logged in as {user.user_metadata.full_name}!</p>
<button className="btn" onClick={logout}>
Log out
</button>
</div>
) : (
<Link to="/login">
<button className="btn">Login</button>
</Link>
)}
</div>
);
}
13 changes: 11 additions & 2 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,19 @@ function App() {
data: { subscription },
} = supabase.auth.onAuthStateChange((event, session) => {
revalidator.revalidate();
if (event === "SIGNED_IN") {
// handle events
if (event === "INITIAL_SESSION") {
if (session?.user) setUser(session.user);
} else if (event === "SIGNED_IN") {
setUser(session?.user);
} else if (event === "SIGNED_OUT") {
setUser(undefined);
} else if (event === "PASSWORD_RECOVERY") {
// handle password recovery event
} else if (event === "TOKEN_REFRESHED") {
// handle token refreshed event
} else if (event === "USER_UPDATED") {
// handle user updated event
}
});

Expand All @@ -80,7 +89,7 @@ function App() {

// hide for specific routes
const location = useLocation();
const routesToHideNavigation = ["/login", "/signup"]; ///// add homepage
const routesToHideNavigation = ["/login"]; ///// add homepage
const shouldHideNavigation = routesToHideNavigation.includes(
location.pathname,
);
Expand Down
2 changes: 1 addition & 1 deletion app/routes/login/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function LoginForm({
}) {
const fillDemoAcc = () => {
setEmail("[email protected]");
setPassword("123+Ab");
setPassword("Test!123");
};

return (
Expand Down
8 changes: 7 additions & 1 deletion app/routes/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export default function Login() {
setErrorMessage(error.message);
return;
}

return navigate("/");
};

Expand All @@ -69,6 +68,8 @@ export default function Login() {
setIsSubmitting(true);

const form = event.currentTarget;

/// BUG: Need to reload the app every time user_data in option has changed ( 111-115 ).
const formValues: { [key: string]: string } = {
displayName: form["reg_display_name_input"].value,
email: form["reg_email_input"].value,
Expand Down Expand Up @@ -107,6 +108,11 @@ export default function Login() {
const { error, data } = await supabase.auth.signUp({
email: formValues.email,
password: formValues.password,
options: {
data: {
full_name: formValues.displayName,
},
},
});
if (error) {
setIsSubmitting(false);
Expand Down
90 changes: 0 additions & 90 deletions app/routes/test.tsx

This file was deleted.

33 changes: 15 additions & 18 deletions app/tests/somefile.test.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import {describe, it, expect} from "vitest"
import {render, screen} from '@testing-library/react'
import {userEvent} from "@testing-library/user-event"
import "@testing-library/jest-dom"
import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import "@testing-library/jest-dom";

import Index from '../routes/_index';
import Index from "../routes/_index";


describe('App', () => {

it('clicks to change text', async () => {
describe("App", () => {
it("clicks to change text", async () => {
// ARRANGE
render(<Index/>)
render(<Index />);

// ACT
await userEvent.click(screen.getByText('click here'))
await screen.findByRole('heading')

// ASSERT
expect(screen.getByRole('heading')).toHaveTextContent("done")
})
await userEvent.click(screen.getByText("click here"));
await screen.findByRole("heading");

})
// ASSERT
expect(screen.getByRole("heading")).toHaveTextContent("done");
});
});
Loading

0 comments on commit 1f86d51

Please sign in to comment.