Skip to content
Snippets Groups Projects
Commit 13b0065a authored by Fatih20's avatar Fatih20
Browse files

Fix bug with setting user state after login, register, logout.

parent f806f885
Branches
Tags
No related merge requests found
...@@ -13,4 +13,5 @@ body { ...@@ -13,4 +13,5 @@ body {
align-items: flex-start; align-items: flex-start;
margin: 0 auto; margin: 0 auto;
text-align: center; text-align: center;
width: 100%;
} }
...@@ -6,7 +6,6 @@ import { ...@@ -6,7 +6,6 @@ import {
RouterProvider, RouterProvider,
} from "react-router-dom"; } from "react-router-dom";
import { QueryClient, QueryClientProvider } from "react-query"; import { QueryClient, QueryClientProvider } from "react-query";
import Home from "@/pages/Home";
import Login from "@/pages/Login"; import Login from "@/pages/Login";
import ProtectedLayout from "@/components/ProtectedLayout"; import ProtectedLayout from "@/components/ProtectedLayout";
import HomeLayout from "@/components/HomeLayout"; import HomeLayout from "@/components/HomeLayout";
...@@ -22,7 +21,6 @@ const router = createBrowserRouter( ...@@ -22,7 +21,6 @@ const router = createBrowserRouter(
createRoutesFromElements( createRoutesFromElements(
<> <>
<Route path="" element={<ProtectedLayout />}> <Route path="" element={<ProtectedLayout />}>
<Route path="" element={<Home />} />
<Route path="/profile" element={<Profile />} /> <Route path="/profile" element={<Profile />} />
<Route path="/applications" element={<Applications />} /> <Route path="/applications" element={<Applications />} />
<Route path="/gyms" element={<Gym />}></Route> <Route path="/gyms" element={<Gym />}></Route>
......
...@@ -7,7 +7,7 @@ function HomeLayout() { ...@@ -7,7 +7,7 @@ function HomeLayout() {
const nav = useNavigate(); const nav = useNavigate();
useEffect(() => { useEffect(() => {
if (user && status === "success") { if (user && status === "success") {
nav("/"); nav("/profile");
} }
}, [user, nav, status]); }, [user, nav, status]);
......
...@@ -19,7 +19,7 @@ function Employer({ user }: { user: UserReturned }) { ...@@ -19,7 +19,7 @@ function Employer({ user }: { user: UserReturned }) {
{status !== "success" ? ( {status !== "success" ? (
<p>Loading...</p> <p>Loading...</p>
) : gym === undefined ? ( ) : gym === undefined ? (
<p>You're not currently employed</p> <p className="self-start">You're not currently employed</p>
) : ( ) : (
<GymCard horizontal={true} gym={gym} /> <GymCard horizontal={true} gym={gym} />
)} )}
......
...@@ -14,6 +14,7 @@ import { useState } from "react"; ...@@ -14,6 +14,7 @@ import { useState } from "react";
import validate from "@/utils/validationSchema/validate"; import validate from "@/utils/validationSchema/validate";
function ProfileForm({ user }: { user: UserReturned }) { function ProfileForm({ user }: { user: UserReturned }) {
console.log("User in profile form", user);
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { const {
field: username, field: username,
......
...@@ -212,7 +212,7 @@ function SkillManagement() { ...@@ -212,7 +212,7 @@ function SkillManagement() {
userSkills ? ( userSkills ? (
<p>Loading</p> <p>Loading</p>
) : ( ) : (
<div className="w-full h-[18.75rem] bg-slate-600 px-3 py-3 lg:px-6 lg:py-6 flex flex-col rounded-lg overflow-y-scroll gap-2"> <div className="w-full h-[18.75rem] bg-slate-600 px-3 py-3 lg:px-6 lg:py-6 flex flex-col rounded-lg overflow-y-scroll gap-2 box-border">
{allSkillsProcessor(allSkills ?? [], userSkills ?? []).map( {allSkillsProcessor(allSkills ?? [], userSkills ?? []).map(
({ alreadyOwned, description, skillName }) => ( ({ alreadyOwned, description, skillName }) => (
<AllSkillBit <AllSkillBit
......
...@@ -34,9 +34,10 @@ function Login() { ...@@ -34,9 +34,10 @@ function Login() {
setUsername(""); setUsername("");
setPassword(""); setPassword("");
setLoginError(""); setLoginError("");
queryClient.setQueryData(["user"], { user: data, status: "success" }); queryClient.setQueryData(["user"], data);
queryClient.invalidateQueries(["user"]); console.log(queryClient.getQueryData(["user"]));
navigate("/");
navigate("/profile");
}, },
}); });
......
...@@ -13,11 +13,11 @@ function Profile() { ...@@ -13,11 +13,11 @@ function Profile() {
const { mutateAsync: logoutMutation } = useMutation({ const { mutateAsync: logoutMutation } = useMutation({
mutationFn: logout, mutationFn: logout,
onSuccess: () => { onSuccess: () => {
queryClient.invalidateQueries(); queryClient.setQueryData(["user"], undefined);
queryClient.setQueryData(["user"], { user: undefined, status: "idle" }); queryClient.invalidateQueries({ queryKey: ["skills", "me"] });
queryClient.invalidateQueries({ queryKey: ["applications"] });
}, },
}); });
console.log(user);
return ( return (
<div className="flex flex-col w-full justify-center items-start box-border py-4 lg:py-8 px-4 lg:px-8 text-left gap-4"> <div className="flex flex-col w-full justify-center items-start box-border py-4 lg:py-8 px-4 lg:px-8 text-left gap-4">
......
...@@ -55,15 +55,14 @@ function Register() { ...@@ -55,15 +55,14 @@ function Register() {
mutationFn: async () => mutationFn: async () =>
await register({ username, password, description, email, name }), await register({ username, password, description, email, name }),
onSuccess(data: UserReturned) { onSuccess(data: UserReturned) {
console.log(data); queryClient.setQueryData(["user"], data);
queryClient.setQueryData(["user"], { user: data, status: "success" });
setUsername(""); setUsername("");
setDescription(""); setDescription("");
setEmail(""); setEmail("");
setName(""); setName("");
setPassword(""); setPassword("");
setRegisterError(""); setRegisterError("");
navigate("/"); navigate("/profile");
}, },
}); });
......
...@@ -29,7 +29,9 @@ async function getMe(): Promise<UserReturned> { ...@@ -29,7 +29,9 @@ async function getMe(): Promise<UserReturned> {
.user; .user;
} }
async function editMe(payload: UserReturned): Promise<UserReturned> { async function editMe(
payload: Omit<UserReturned, "gymId">
): Promise<UserReturned> {
return ( return (
await axios.patch(`${config.NODE_JS_API}/api/auth/me`, payload, header) await axios.patch(`${config.NODE_JS_API}/api/auth/me`, payload, header)
).data.user; ).data.user;
......
...@@ -3,7 +3,6 @@ import { QueryStatus, useQuery } from "react-query"; ...@@ -3,7 +3,6 @@ import { QueryStatus, useQuery } from "react-query";
import { Skill } from "../validationSchema/skill"; import { Skill } from "../validationSchema/skill";
import { getSkills, getUserSkills } from "../api/skill"; import { getSkills, getUserSkills } from "../api/skill";
import { UserReturned } from "../validationSchema/trainer"; import { UserReturned } from "../validationSchema/trainer";
import { useUser } from "./AuthProvider";
const AllSkillsContext = createContext<{ const AllSkillsContext = createContext<{
allSkills: Skill[] | undefined; allSkills: Skill[] | undefined;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment