From 2ad54f6e886a868e7578424aee2c4f1afb02ea0e Mon Sep 17 00:00:00 2001 From: Akshata Katwal Date: Mon, 8 Jul 2024 16:52:09 +0530 Subject: [PATCH] Issue feat#PS-1082: fix issue in protected route --- src/components/ProtectedRoute.js | 17 +++++++++---- src/pages/cohorts.tsx | 4 +-- src/pages/createPlan.tsx | 4 +-- src/pages/dashboard.tsx | 4 +-- src/pages/faciliator.tsx | 42 +++++++++++++++++++++++++++++++- src/pages/index.tsx | 10 ++++---- src/pages/learners.tsx | 4 +-- src/pages/teamLeader.tsx | 4 +-- src/pages/viewPlans.tsx | 4 +-- 9 files changed, 70 insertions(+), 23 deletions(-) diff --git a/src/components/ProtectedRoute.js b/src/components/ProtectedRoute.js index a9e8d3ed..134c22ee 100644 --- a/src/components/ProtectedRoute.js +++ b/src/components/ProtectedRoute.js @@ -5,17 +5,24 @@ import { useTranslation } from 'next-i18next'; const ProtectedRoute = ({ children }) => { const { t } = useTranslation(); - +let user; const router = useRouter(); - const { user, loading } = useAuth(); - + const { loading } = useAuth(); + + console.log(user, loading) useEffect(() => { + let user; + if (typeof window !== "undefined" && window.localStorage) { + user= localStorage.getItem('token'); + } + + if (!loading && !user) { router.push('/login'); } - }, [user, loading, router]); + }, [ loading, router]); - if (loading || !user) { + if (loading) { return

{t('COMMON.LOADING')}

; } diff --git a/src/pages/cohorts.tsx b/src/pages/cohorts.tsx index 5d2584b5..7f6b8c83 100644 --- a/src/pages/cohorts.tsx +++ b/src/pages/cohorts.tsx @@ -5,11 +5,11 @@ import ProtectedRoute from "../components/ProtectedRoute"; const Cohorts = () => { const { t } = useTranslation(); return ( - // + <>

{t("SIDEBAR.COHORTS")}

- //
+
); }; diff --git a/src/pages/createPlan.tsx b/src/pages/createPlan.tsx index c14f320e..d12da359 100644 --- a/src/pages/createPlan.tsx +++ b/src/pages/createPlan.tsx @@ -6,11 +6,11 @@ const CreatePlan = () => { const { t } = useTranslation(); return ( - // + <>

{t("SIDEBAR.CREATE_PLAN")}

- //
+
); }; diff --git a/src/pages/dashboard.tsx b/src/pages/dashboard.tsx index cd72048b..68cf76fe 100644 --- a/src/pages/dashboard.tsx +++ b/src/pages/dashboard.tsx @@ -6,13 +6,13 @@ import ProtectedRoute from '../components/ProtectedRoute'; const Dashboard = () => { const { t } = useTranslation(); return ( - // + <>

{t("SIDEBAR.DASHBOARD")}

Welcome to your dashboard

- //
+
); }; diff --git a/src/pages/faciliator.tsx b/src/pages/faciliator.tsx index 504581ab..741e2e92 100644 --- a/src/pages/faciliator.tsx +++ b/src/pages/faciliator.tsx @@ -27,11 +27,15 @@ type UserDetails = { const AllStates = ["maharashtra", "Gujarat"]; const AllDistrict = ["Kolhapur", "Pune"]; const AllBlocks = ["Kothrud", "Warje"]; +const Sort = ["Names", "A-Z" , "Z-A" ,"Centers"]; + const Facilitators: React.FC = () => { const [selectedState, setSelectedState] = useState("All states"); const [selectedDistrict, setSelectedDistrict] = useState("All Districts"); const [selectedBlock, setSelectedBlock] = useState("All Blocks"); + const [selectedSort, setSelectedSort] = useState("Sort"); + const { t } = useTranslation(); const [data, setData] = useState([]); @@ -48,6 +52,9 @@ const Facilitators: React.FC = () => { const handleBlockChange = (event: SelectChangeEvent) => { setSelectedBlock(event.target.value as string); }; + const handleSortChange = (event: SelectChangeEvent) => { + setSelectedSort(event.target.value as string); + }; console.log(data[0]) useEffect(() => { const fetchUserList = async () => { @@ -152,7 +159,40 @@ console.log(data[0]) {t("SIDEBAR.FACILITATORS")} - + + + + + + + + + + { if (typeof window !== 'undefined' && window.localStorage) { const token = localStorage.getItem('token'); setLoading(false); - // if (token) { - // push('/dashboard'); - // } else { - push('/login', undefined, { locale: 'en' }); - // } + if (token) { + push('/dashboard'); + } else { + push('/login', undefined, { locale: 'en' }); + } } }, []); diff --git a/src/pages/learners.tsx b/src/pages/learners.tsx index 94b47db7..70ee35cf 100644 --- a/src/pages/learners.tsx +++ b/src/pages/learners.tsx @@ -5,11 +5,11 @@ import ProtectedRoute from "../components/ProtectedRoute"; const Learners = () => { const { t } = useTranslation(); return ( - // + <>

{t("SIDEBAR.LEARNERS")}

- //
+
); }; diff --git a/src/pages/teamLeader.tsx b/src/pages/teamLeader.tsx index 2117ec88..7f4e0543 100644 --- a/src/pages/teamLeader.tsx +++ b/src/pages/teamLeader.tsx @@ -5,12 +5,12 @@ import ProtectedRoute from '../components/ProtectedRoute'; const TeamLeader = () => { const { t } = useTranslation(); return ( - // + <>

{t("SIDEBAR.TEAM_LEADERS")}

- //
+
); }; diff --git a/src/pages/viewPlans.tsx b/src/pages/viewPlans.tsx index 56e8b430..85ba9dd5 100644 --- a/src/pages/viewPlans.tsx +++ b/src/pages/viewPlans.tsx @@ -5,12 +5,12 @@ import ProtectedRoute from "../components/ProtectedRoute"; const ViewPlans = () => { const { t } = useTranslation(); return ( - // + <>

{t("SIDEBAR.VIEW_PLANS")}

- //
+
); };