Skip to content

Commit

Permalink
found that the reload was breaking register
Browse files Browse the repository at this point in the history
  • Loading branch information
GitLukeW committed Jun 15, 2023
1 parent 5dd3da4 commit 57ea043
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
43 changes: 25 additions & 18 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ import CurrentAvailabilities from "./pages/Profile/Current Availabilities";
import { Routes, Route } from "react-router-dom";
import axios from "axios";
import "./assets/App.css";
import { act } from "react-dom/test-utils";

function App() {
const [token, setToken] = useLocalStorageState("momentorsToken", null);
const [userName, setUserName] = useLocalStorageState("momentorsUserName", "");
const [pk, setPk] = useLocalStorageState("pk", "");

// Setting up in session storage so they're not lost on refresh
const [mentor, setMentor] = useState(JSON.parse(sessionStorage.getItem("is_mentor")));
const [mentee, setMentee] = useState(JSON.parse(sessionStorage.getItem("is_mentee")));

const [mentor, setMentor] = useState(
JSON.parse(sessionStorage.getItem("is_mentor"))
);
const [mentee, setMentee] = useState(
JSON.parse(sessionStorage.getItem("is_mentee"))
);

const [loading, setLoading] = useState(false);

const setAuth = (userName, token, pk) => {
Expand Down Expand Up @@ -70,33 +73,37 @@ function App() {

// log user out if they close the tab
// used for reference: https://typeofnan.dev/using-session-storage-in-react-with-hooks/
const getSessionStorageDefault = (key, defaultValue) => {
const stored = sessionStorage.getItem(key);
if (!stored) {
return defaultValue;
}
return stored;
};
const getSessionStorageDefault = (key, defaultValue) => {
const stored = sessionStorage.getItem(key);
if (!stored) {
return defaultValue;
}
return stored;
};

const [userLive, setUserLive] = useState(getSessionStorageDefault("user_live", false));
const [userLive, setUserLive] = useState(
getSessionStorageDefault("user_live", false)
);

useEffect( () => {
useEffect(() => {
sessionStorage.setItem("user_live", userLive);
}, [userLive]);

// Not using any dependencies because I only want this to run on page load
useEffect( () => {
useEffect(() => {
if (userLive === false && token) {
handleLogout();
}
}, []);

console.log(userLive);
console.log(token);

return (
<div>
<NavBar isLoggedIn={isLoggedIn} token={token} handleLogout={handleLogout} loading={loading} />
<NavBar
isLoggedIn={isLoggedIn}
token={token}
handleLogout={handleLogout}
loading={loading}
/>
<Routes>
<Route path="/" element={<Hero />} />
<Route
Expand Down
2 changes: 0 additions & 2 deletions src/pages/Register/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export default function Profile({
.then((res) => {
sessionStorage.setItem("is_mentor", true);
setMentor(true);
window.location.reload(true)
})
.catch((e) => {
setError(e.message);
Expand Down Expand Up @@ -95,7 +94,6 @@ export default function Profile({
.then((res) => {
sessionStorage.setItem("is_mentee", true);
setMentee(true);
window.location.reload(true)
})
.catch((e) => {
setError(e.message);
Expand Down

0 comments on commit 57ea043

Please sign in to comment.