Skip to content

Commit

Permalink
Merge pull request #106 from Sunbird-Serve/release-ui-2.1.0.1
Browse files Browse the repository at this point in the history
Release UI 2.1.0.1
  • Loading branch information
Sowmya-Raghuram authored Nov 29, 2024
2 parents a2f3aba + 76c0c42 commit 0988a02
Show file tree
Hide file tree
Showing 4 changed files with 440 additions and 309 deletions.
270 changes: 143 additions & 127 deletions src/main/frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@

// export default App;



import React, { useEffect, useState } from "react";
import LoginPage from "./containers/LoginPage/LoginPage";
import MainPage from "./containers/MainPage/MainPage";
Expand All @@ -115,132 +113,150 @@ import { fetchNeedtypes } from "./state/needtypeSlice";
import { fetchNeedsByUid } from "./state/needByUidSlice";
import { fetchEntities } from "./state/entitySlice";
import { fetchUserList } from "./state/userListSlice";
import "./index.css"
function App() {
const dispatch = useDispatch();
const [presentUser, setPresentUser] = useState(null); // Holds Firebase-authenticated user
const [loading, setLoading] = useState(true); // Global loading state

// Redux state for user details
const userDetails = useSelector((state) => state.user.data);
const userStatus = useSelector((state) => state.user.status); // loading, succeeded, failed
const [volunteer, setVolunteer] = useState(false); // State for volunteer redirection

// Token-Based Authentication Logic
useEffect(() => {
const params = new URLSearchParams(window.location.search);
const token = params.get("token");

if (token) {
signInWithCustomToken(auth, token)
.then((userCredential) => {
console.log("User authenticated with custom token:", userCredential.user);

const email = userCredential.user?.uid; // Access email from the user object
if (email) {
setPresentUser({
uid: userCredential.user.uid,
email: email,
});

// Fetch user details
const userEmail = email?.replace(/@/g, "%40");
dispatch(fetchUserByEmail(userEmail));
} else {
console.error("Email is missing from the user object.");
}
})
.catch((error) => {
console.error("Authentication with custom token failed:", error);
setPresentUser(null);
})
.finally(() => setLoading(false));
} else {
// Handle case where no token is present
setLoading(false);
}
}, [dispatch]);

// Firebase Listener-Based Authentication Logic
useEffect(() => {
const unsubscribe = auth.onAuthStateChanged((user) => {
if (user) {
setPresentUser({
uid: user.uid,
email: user.email,
});

// Fetch user details for listener-based login
const userEmail = user.email?.replace(/@/g, "%40");
dispatch(fetchUserByEmail(userEmail));
} else {
setPresentUser(null);
}
});

// Cleanup subscription on unmount
return () => unsubscribe();
}, [dispatch]);

// Fetch user list after user details are loaded
useEffect(() => {
if (userStatus === "succeeded") {
dispatch(fetchUserList());
}
}, [dispatch, userStatus]);

// Fetch needs by UID when userDetails are populated
useEffect(() => {
if (userDetails.osid) {
dispatch(fetchNeedsByUid(userDetails.osid));
}
}, [dispatch, userDetails]);

// Fetch global needs
useEffect(() => {
dispatch(fetchNeeds());
}, [dispatch]);

// Fetch need types
useEffect(() => {
dispatch(fetchNeedtypes());
}, [dispatch]);

// Fetch entities
useEffect(() => {
dispatch(fetchEntities());
}, [dispatch]);


// Handle volunteer status change
const handleVolunteer = (value) => {
setVolunteer(value);
};

// Conditional rendering logic
if (loading) {
return <div>Loading...</div>;
}
// Handle explore button click
if (volunteer) {
return <ExplorePage />;
}
if (presentUser === null) {
return <LoginPage getVolunteerStatus={handleVolunteer} />;
}
import { redirectToOnlineTeachingPage } from "./state/redirectionToOnlineTeachingSlice";
import "./index.css";

if (userStatus === "loading") {
return (<div className="loading-div">
<div className="spiner"></div>
</div>);
}

if (userDetails.role && userDetails.role.includes("Volunteer")) {
return <ExplorePage />;
}

return <MainPage />;
function App() {
const dispatch = useDispatch();
const [presentUser, setPresentUser] = useState(null); // Holds Firebase-authenticated user
const [loading, setLoading] = useState(true); // Global loading state

// Redux state for user details
const userDetails = useSelector((state) => state.user.data);
const userStatus = useSelector((state) => state.user.status); // loading, succeeded, failed
const [volunteer, setVolunteer] = useState(false); // State for volunteer redirection

// const [countReloads, setCountReload] = useState(0);
// Token-Based Authentication Logic
useEffect(() => {
const params = new URLSearchParams(window.location.search);
const token = params.get("token");

if (!token) {
dispatch(redirectToOnlineTeachingPage());
}

if (token) {
signInWithCustomToken(auth, token)
.then((userCredential) => {
console.log(
"User authenticated with custom token:",
userCredential.user
);

const email = userCredential.user?.uid; // Access email from the user object
if (email) {
setPresentUser({
uid: userCredential.user.uid,
email: email,
});

// Fetch user details
const userEmail = email?.replace(/@/g, "%40");
dispatch(fetchUserByEmail(userEmail));

// setCountReload((prev) => prev + 1);
// if (countReloads > 2) {
// window.location.reload();
// }
} else {
console.error("Email is missing from the user object.");
}
})
.catch((error) => {
console.error(
"Authentication with custom token failed:",
error
);
setPresentUser(null);
})
.finally(() => setLoading(false));
} else {
// Handle case where no token is present
setLoading(false);
}
}, [dispatch]);

// Firebase Listener-Based Authentication Logic
useEffect(() => {
const unsubscribe = auth.onAuthStateChanged((user) => {
if (user) {
setPresentUser({
uid: user.uid,
email: user.email,
});

// Fetch user details for listener-based login
const userEmail = user.email?.replace(/@/g, "%40");
dispatch(fetchUserByEmail(userEmail));
} else {
setPresentUser(null);
}
});

// Cleanup subscription on unmount
return () => unsubscribe();
}, [dispatch]);

// Fetch user list after user details are loaded
useEffect(() => {
if (userStatus === "succeeded") {
dispatch(fetchUserList());
}
}, [dispatch, userStatus]);

// Fetch needs by UID when userDetails are populated
useEffect(() => {
if (userDetails.osid) {
dispatch(fetchNeedsByUid(userDetails.osid));
}
}, [dispatch, userDetails]);

// Fetch global needs
useEffect(() => {
dispatch(fetchNeeds());
}, [dispatch]);

// Fetch need types
useEffect(() => {
dispatch(fetchNeedtypes());
}, [dispatch]);

// Fetch entities
useEffect(() => {
dispatch(fetchEntities());
}, [dispatch]);

// Handle volunteer status change
const handleVolunteer = (value) => {
setVolunteer(value);
};

// Conditional rendering logic
if (loading) {
return <div>Loading...</div>;
}
// Handle explore button click
if (volunteer) {
return <ExplorePage />;
}
if (presentUser === null) {
return <LoginPage getVolunteerStatus={handleVolunteer} />;
}

if (userStatus === "loading") {
return (
<div className="loading-div">
<div className="spiner"></div>
</div>
);
}

if (userDetails.role && userDetails.role.includes("Volunteer")) {
return <ExplorePage />;
}

return <MainPage />;
}

export default App;

Loading

0 comments on commit 0988a02

Please sign in to comment.