Skip to content
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

Added the access route control restiction without login. #265

Merged
merged 1 commit into from
Oct 5, 2024
Merged
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
6 changes: 5 additions & 1 deletion client/src/Context/ToastContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let ToastContextProvider = ({children}) => {

let toastProperties = null;

const showToast = (typeOfToast, toastTitle, toastDescription) =>
const showToast = (typeOfToast, toastTitle, toastDescription, time=5000) =>
{
switch(typeOfToast)
{
Expand All @@ -17,6 +17,7 @@ let ToastContextProvider = ({children}) => {
title : toastTitle,
description : toastDescription,
type:"success",
time: time

}

Expand All @@ -26,20 +27,23 @@ let ToastContextProvider = ({children}) => {
title : toastTitle,
description : toastDescription,
type:"error",
time: time
}
break;
case "warning" : toastProperties = {
id : toastList.length+1,
title : toastTitle,
description : toastDescription,
type:"warning",
time: time
}
break;
case "info" : toastProperties = {
id : toastList.length+1,
title : toastTitle,
description : toastDescription,
type:"info",
time: time
}
break;
default : toastProperties = {}
Expand Down
17 changes: 17 additions & 0 deletions client/src/Pages/Cart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import Spinner from "./Spinner";
import { fetchCartData, addItemToCart, removeItemFromCart } from "../api/api.js";
import "./Cart.css";
import Preloader from '../Components/Preloader';
import { useAuth } from "../Context/AuthContext";
import { useToast } from "../Context/ToastContext";
import { useNavigate } from "react-router-dom";


function Cart() {
Expand All @@ -11,6 +14,11 @@ function Cart() {
const [error, setError] = useState(null);
const [retryCount, setRetryCount] = useState(0);

const { userLoggedIn } = useAuth();
const { showToast } = useToast();
const navigate = useNavigate();


useEffect(() => {
fetchCartData()
.then((cartData) => {
Expand All @@ -25,6 +33,15 @@ function Cart() {
});
}, [retryCount]); // Retry whenever retryCount changes

useEffect(() => {
if (!userLoggedIn) {
showToast("error", "Please login to view your cart.", undefined, 7000);
setTimeout(() => {
navigate("/login");
}, 3000); // 3000 milliseconds = 3 seconds
}
}, [userLoggedIn]);

const handleAddItem = (item) => {
setIsLoading(true);
addItemToCart(item)
Expand Down
16 changes: 16 additions & 0 deletions client/src/Pages/Orders.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ import SearchIcon from "@mui/icons-material/Search";
import SortIcon from "@mui/icons-material/Sort";
import RefreshIcon from "@mui/icons-material/Refresh";
import Preloader from "../Components/Preloader";
import { useAuth } from "../Context/AuthContext";
import { useToast } from "../Context/ToastContext";
import { useNavigate } from "react-router-dom";

function OrderList() {
const [isLoading, setIsLoading] = useState(true);
const [data, setData] = useState([]);
const [searchTerm, setSearchTerm] = useState("");
const [sortOrder, setSortOrder] = useState("asc");

const { userLoggedIn } = useAuth();
const { showToast } = useToast();
const navigate = useNavigate();

useEffect(() => {
fetchData();
}, []);
Expand All @@ -44,6 +51,15 @@ function OrderList() {
}
};

useEffect(() => {
if (!userLoggedIn) {
showToast("error", "Please login to view your orders.", undefined, 7000);
setTimeout(() => {
navigate("/login");
}, 3000); // 3000 milliseconds = 3 seconds
}
}, [userLoggedIn]);

const handleSearch = (event) => {
setSearchTerm(event.target.value);
};
Expand Down
17 changes: 17 additions & 0 deletions client/src/Pages/Wishlist.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import axios from "axios";
import { FaHeart, FaTrash, FaTable, FaRegHeart } from "react-icons/fa";
import "./Wishlist.css"; // Import CSS file for wishlist component styling
import Preloader from "../Components/Preloader";
import { useAuth } from "../Context/AuthContext";
import { useToast } from "../Context/ToastContext";
import { useNavigate } from "react-router-dom";

function Wishlist() {
const [isLoading, setIsLoading] = useState(true);
const [wishlistItems, setWishlistItems] = useState([]);
const [error, setError] = useState(null);
const { userLoggedIn } = useAuth();
const { showToast } = useToast();

const navigate = useNavigate();

useEffect(() => {
const fetchWishlist = async () => {
Expand All @@ -31,6 +38,16 @@ function Wishlist() {
fetchWishlist();
}, []);

useEffect(() => {
if (!userLoggedIn) {
showToast("error", "Please login to view your wishlist.", undefined, 7000);
setTimeout(() => {
navigate("/login");
}, 5000); // 3000 milliseconds = 3 seconds
}
}, [userLoggedIn]);


const removeFromWishlist = async (productId) => {
try {
const authToken = localStorage.getItem('token');
Expand Down
Loading