Skip to content

Commit

Permalink
Fix useUser hook
Browse files Browse the repository at this point in the history
  • Loading branch information
tanish35 committed Oct 9, 2024
1 parent 85b19e4 commit ac6c985
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
18 changes: 16 additions & 2 deletions frontend/src/components/Posts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Heading,
Stack,
Select,
useToast,
} from "@chakra-ui/react";
import axios from "axios";
import CreatePost from "./CreatePosts";
Expand All @@ -25,8 +26,9 @@ const Posts = () => {
const [selectedCommunity, setSelectedCommunity] = useState("all");
const [allCommunities, setAllCommunities] = useState([]);
const [page, setPage] = useState(1);
const { userDeatils} = useUser();
const { userDetails, loadingUser } = useUser();
const navigate = useNavigate();
const toast = useToast();

const fetchPosts = async () => {
try {
Expand Down Expand Up @@ -54,6 +56,15 @@ const Posts = () => {
setPage(page + 1);
setLoading(false);
} catch (err) {
if (err.response.status === 401) {
toast({
title: "Please login to view posts",
status: "error",
duration: 3000,
isClosable: true,
});
return;
}
setLoading(false);
alert("Error fetching posts");
}
Expand Down Expand Up @@ -135,7 +146,10 @@ const Posts = () => {
window.location.reload();
};
if (loading) return <div>Loading...</div>;
if (!userDeatils) return <Navigate to="/login" />;
if (loadingUser) return <div>Loading...</div>;
if (!userDetails) {
return <Navigate to="/login" />;
}
return (
<Container centerContent>
<SearchBar />
Expand Down
48 changes: 23 additions & 25 deletions frontend/src/hook/useUser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@ import axios from "axios";
import { useEffect, useState } from "react";

export const useUser = () => {
const [loading, setLoading] = useState(true);
const [userDetails, setUserDetails] = useState(null);
const [loadingUser, setLoadingUser] = useState(true);
const [userDetails, setUserDetails] = useState(null);

async function getDetails() {
try {
const res = await axios.get('/user/me', {
withCredentials: true,
});
setUserDetails(res.data);
setLoading(false); // Move setLoading inside try block to ensure it's always set
}
catch (err) {
console.log(err);
setLoading(false); // Set loading to false in case of error
}
async function getDetails() {
try {
const res = await axios.get("/user/me", {
withCredentials: true,
});
setUserDetails(res.data);
setLoadingUser(false); // Move setLoading inside try block to ensure it's always set
} catch (err) {
console.log(err);
setLoadingUser(false); // Set loading to false in case of error
}
}

useEffect(() => {
getDetails();
}, []); // Empty dependency array, so it runs only once on component mount
useEffect(() => {
getDetails();
}, []); // Empty dependency array, so it runs only once on component mount

useEffect(() => {
if (userDetails) {

// console.log("User details:", userDetails);
}
}, [userDetails]); // Log userDetails whenever it changes
useEffect(() => {
if (userDetails) {
// console.log("User details:", userDetails);
}
}, [userDetails]); // Log userDetails whenever it changes

return { loading, userDetails };
};
return { loadingUser, userDetails };
};

0 comments on commit ac6c985

Please sign in to comment.