Skip to content

update user #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/NavBar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import routes from "../../routes/route.json";
import { useEffect, useState } from "react";
import Cookies from "js-cookie";
import user from "../../assets/user .png";
import { useNavigate } from "react-router-dom";

const Navbar = () => {
const location = useLocation();
const [isLoggedIn, setIsLoggedIn] = useState(false);
const navigate = useNavigate();

useEffect(() => {
if (Cookies.get("access_token")) {
setIsLoggedIn(true);
}
}, []);
}, [navigate]);

return (
<div className="navbar bg-primaryLighter border-b-2 border-b-secondary">
Expand Down
94 changes: 41 additions & 53 deletions src/components/Profile/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,35 @@ import { useCallback, useEffect, useState } from "react";
import editing from "../../assets/editing .png";
import virtualClass from "../../assets/virtual-class.png";
import Cookies from "js-cookie";
import { set } from "firebase/database";
import { useAuthentication } from "../../hooks/useAuthentication";
import { useNavigate } from "react-router-dom";

const UserAccount = () => {
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [phone, setPhone] = useState("");
const [role, setRole] = useState("");
const { user } = useAuthentication();
const [isnewUser, setIsNewUser] = useState(false);
const navigate = useNavigate();

const [isEditing, setIsEditing] = useState(false);

useEffect(() => {
const fetchUser = async () => {
try {
const response = await fetch(
"http://localhost:3000/api/user/currentUser",
{
method: "GET",
headers: {
"Content-Type": "application/json",
access_token: Cookies.get("access_token"),
},
}
);
if (!response.ok) {
throw new Error("Failed to fetch user data");
}
console.log(user);

const data = await response.json();
setUsername(user?.fullName || "");
setEmail(user?.email || "");
setPhone(user?.phone || "");
setRole(user?.role || "");

setUsername(data.fullName);
setEmail(data.email);
setPhone(data.phone);
setRole(data.role);
if (role === "" || phone === "") {
setIsNewUser(true);
}
} catch (error) {
console.error(error);
}
};
console.log(!user?.role && !user?.phone);

fetchUser();
}, []);
if (!(!user?.role && !user?.phone)) {
setIsNewUser(true);
console.log(isnewUser);
}
}, [user]);

const handleEditClick = () => {
setIsEditing(!isEditing);
Expand Down Expand Up @@ -98,35 +83,35 @@ const UserAccount = () => {
throw new Error("Failed to delete user data");
}
Cookies.remove("access_token");
window.location.href = "/";
navigate("/");
} catch (error) {
console.error(error);
}
};

const handleNewUser = async (e) => {
const handleNewUser = async () => {
console.log("update new user");
// try {
// const response = await fetch(
// "http://localhost:3000/api/user/updateNewUser",
// {
// method: "PATCH",
// headers: {
// "Content-Type": "application/json",
// access_token: Cookies.get("access_token"),
// },
// body: JSON.stringify({
// fullName: username,
// phone: phone,
// }),
// }
// );
// if (!response.ok) {
// throw new Error("Failed to update user data");
// }
// } catch (error) {
// console.error(error);
// }
try {
const response = await fetch(
"http://localhost:3000/api/user/updateNewUser",
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
access_token: Cookies.get("access_token"),
},
body: JSON.stringify({
role: role,
phone: phone,
}),
}
);
if (!response.ok) {
throw new Error("Failed to update user data");
}
} catch (error) {
console.error(error);
}
setRole("LEARNER");
setPhone("94710627526");
setIsNewUser(false);
Expand Down Expand Up @@ -263,6 +248,9 @@ const UserAccount = () => {
<option value="instructor" className="text-white">
Instructor
</option>
<option value="instructor" className="text-white">
Adminss
</option>
</select>
</div>
<div>
Expand Down
61 changes: 41 additions & 20 deletions src/hooks/useAuthentication.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
import { useEffect, useState } from 'react';
import { useEffect, useState } from "react";
import Cookies from "js-cookie";

export interface User {
id: string;
fullName: string;
email: string;
name: string;
avatar: string;
phone?: string;
role: 'student' | 'instructor' | 'admin';
phone: string;
role: "student" | "instructor" | "admin";

}

export const useAuthentication = () => {
const [user, setUser] = useState<User | null>(null);
const [isLoading, setIsLoading] = useState(true);
// const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
setIsLoading(true);
setUser({
id: '664191c26d71d98b40c14326',
email: 'navojith22@gmail.com',
name: 'Example',
//role: 'instructor',
//role: 'admin',
role: 'student',
phone: '94713197055',
avatar: 'https://example.com/avatar.png',
});
//setUser(null);
setIsLoading(false);
const fetchUser = async () => {
// setIsLoading(true);
try {
const response = await fetch(
"http://localhost:3000/api/user/currentUser",
{
method: "GET",
headers: {
"Content-Type": "application/json",
access_token: Cookies.get("access_token"),
},
}
);
if (!response.ok) {
throw new Error("Error fetching user");
}

const data = await response.json();
setUser({
id: data.id,
fullName: data.fullName,
email: data.email,
phone: data.phone,
role: data.role,
});
} catch (error) {
console.error(error);
}
};
// setIsLoading(false);
fetchUser();

}, []);
return { user, isLoading };
// return { user, isLoading };
return { user };
};
8 changes: 3 additions & 5 deletions src/routes/Routes.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Route, Routes } from "react-router-dom";
import { Navigate, Route, Routes } from "react-router-dom";
import { useAuthentication } from "../hooks/useAuthentication";
import AuthWrapper from "../pages/AuthWrapper";
import Login from "../pages/auth/Login";
import SignUp from "../pages/auth/Signup";
import Course from "../pages/course/Course";
import IndividualCourse from "../pages/course/IndividualCourse/IndividualCourse";
import CourseContent from "../pages/courseContent/[id]";
Expand All @@ -20,7 +19,6 @@ import {
LOGIN,
MY_COURSES,
ROOT,
SIGNUP,
} from "./route.json";

const AppRoutes = () => {
Expand All @@ -45,8 +43,8 @@ const AppRoutes = () => {
</Route>
<Route
path={LOGIN.route}
// element={user ? <Navigate to={ROOT.route} replace /> : <Login />}
element={<Login />}
element={user ? <Navigate to={ROOT.route} replace /> : <Login />}
// element={<Login />}
/>
</Routes>
);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/userAuthentication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Cookies from "js-cookie";

export const getAccessToken = async () => {
const accessToken = Cookies.get("accessToken");
const accessToken = Cookies.get("access_token");
return accessToken;
};