diff --git a/src/App.js b/src/App.js
index c85e55d..e7c6c72 100644
--- a/src/App.js
+++ b/src/App.js
@@ -1,4 +1,5 @@
-import { useState, useEffect } from "react";
+import { useState } from "react";
+import { useNavigate } from "react-router-dom";
import Dashboard from "./pages/Dashboard";
import ComplaintAnalytics from "./pages/ComplaintAnalytics";
import OverAllAnalytics from "./pages/OverAllAnalytics";
@@ -17,54 +18,50 @@ import EditProfile from "./pages/EditProfile";
import ChangeMesses from "./pages/ChangeMesses";
import Assign from "./pages/Assign";
import { auth } from "./firebaseUtils/firebaseConfig";
-import DetailsModel from "./components/DetailsModal";
-import { onAuthStateChanged } from "firebase/auth";
+// import DetailsModel from "./components/DetailsModal";
+// import { onAuthStateChanged } from "firebase/auth";
import { toast, ToastContainer } from "react-toastify";
-import { LoginPrompt } from "./components/LoginPrompt";
import SendNotice from "./pages/SendNotice";
+import { signInWithPopup, GoogleAuthProvider } from "firebase/auth";
+import { isInDb } from "./firebaseUtils/getRequests";
+const provider = new GoogleAuthProvider();
function App() {
- const [shouldShowDetailsModel, setShouldShowDetailsModel] = useState(false);
+ // const [shouldShowDetailsModel, setShouldShowDetailsModel] = useState(false);
const [user, setUser] = useState(false);
- const [showLoginPrompt, setShowLoginPrompt] = useState(false);
+ const navigate = useNavigate();
- useEffect(() => {
- const unsubscribe = onAuthStateChanged(auth, (user) => {
- if (user) {
- const detailsIncomplete = localStorage.getItem("details") === "false";
- setShouldShowDetailsModel(detailsIncomplete);
- toast.success("succsesfully logged in");
- } else {
- setShouldShowDetailsModel(false);
- setUser(false);
- }
- });
+ const handleUserLogin = async () => {
+ try {
+ const result = await signInWithPopup(auth, provider);
+ const credential = GoogleAuthProvider.credentialFromResult(result);
+ // eslint-disable-next-line no-unused-vars
+ const token = credential.accessToken;
+ const user = result.user;
- return () => {
- unsubscribe();
- };
- }, []);
-
- useEffect(() => {
- const interval = setInterval(() => {
- if (user) {
- setShowLoginPrompt(true);
+ const check = await isInDb(user.uid);
+ setUser(user);
+ toast.success("Successfully logged in");
+ if (check) {
+ localStorage.setItem("details", "true");
+ } else {
+ localStorage.setItem("details", "false");
}
- }, 30000);
-
- return () => clearInterval(interval);
- }, [user]);
-
+ localStorage.setItem("id", user.uid);
+ navigate("/");
+ } catch (error) {
+ console.log("error", error.code, "error msg: ", error.message);
+ }
+ };
return (
- {shouldShowDetailsModel && (
+ {/* {shouldShowDetailsModel && (
setShouldShowDetailsModel(false)}
/>
- )}
- {showLoginPrompt && }
+ )} */}
}>
} />
@@ -87,9 +84,12 @@ function App() {
} />
} />
} />
- } />
+ } />
- } />
+ }
+ />
);
diff --git a/src/components/SignInCard.js b/src/components/SignInCard.js
index d2ef286..6ccc7bd 100644
--- a/src/components/SignInCard.js
+++ b/src/components/SignInCard.js
@@ -6,7 +6,6 @@ import Typography from "@mui/material/Typography";
import { styled } from "@mui/material/styles";
import GoogleIcon from "@mui/icons-material/Google";
import logo from "../images/rguktLogo.png";
-import { handleUserLogin } from "../firebaseUtils/login";
import { toast } from "react-toastify";
const Card = styled(MuiCard)(({ theme }) => ({
display: "flex",
@@ -26,10 +25,10 @@ const Card = styled(MuiCard)(({ theme }) => ({
}),
}));
-export default function SignInCard() {
- const handleLogin = async () => {
+export default function SignInCard({ handleLogin }) {
+ const handleUserLogin = async () => {
try {
- await handleUserLogin();
+ await handleLogin();
} catch (error) {
toast.error("Failed to login");
}
@@ -56,7 +55,7 @@ export default function SignInCard() {
}
>
Sign in with Google
diff --git a/src/firebaseUtils/login.js b/src/firebaseUtils/login.js
index 0e1ef92..b70404e 100644
--- a/src/firebaseUtils/login.js
+++ b/src/firebaseUtils/login.js
@@ -1,7 +1,6 @@
import { signInWithPopup, GoogleAuthProvider } from "firebase/auth";
import { auth } from "../firebaseUtils/firebaseConfig";
import { isInDb } from "../firebaseUtils/getRequests";
-
const provider = new GoogleAuthProvider();
const handleUserLogin = async () => {
try {
@@ -18,8 +17,8 @@ const handleUserLogin = async () => {
} else {
localStorage.setItem("details", "false");
}
+ localStorage.setItem("id", user.uid);
window.history.replaceState(null, "", "/");
- window.location.href = "/";
} catch (error) {
console.log("error", error.code, "error msg: ", error.message);
}
diff --git a/src/firebaseUtils/logout.js b/src/firebaseUtils/logout.js
index dabc3b3..9e4c793 100644
--- a/src/firebaseUtils/logout.js
+++ b/src/firebaseUtils/logout.js
@@ -6,6 +6,7 @@ const logoutUser = () => {
.then(() => {
localStorage.removeItem("currentUser");
localStorage.removeItem("details");
+ localStorage.removeItem("id");
})
.catch((error) => {
"error in logging out user";
diff --git a/src/pages/EditProfile.js b/src/pages/EditProfile.js
index 4e1b000..5823096 100644
--- a/src/pages/EditProfile.js
+++ b/src/pages/EditProfile.js
@@ -6,13 +6,33 @@ import MenuItem from "@mui/material/MenuItem";
import FormControl from "@mui/material/FormControl";
import Select from "@mui/material/Select";
import { auth } from "../firebaseUtils/firebaseConfig";
-import { updateUser } from "../firebaseUtils/createDoc";
+import { createUser, updateUser } from "../firebaseUtils/createDoc";
import { useNavigate } from "react-router-dom";
+import { getUserInfo } from "../firebaseUtils/getRequests";
+import { onAuthStateChanged } from "firebase/auth";
const EditProfile = () => {
const location = useLocation();
const navigate = useNavigate();
console.log(location.state);
- const [profile, setProfile] = useState(location.state || {});
+ const [profile, setProfile] = useState(null);
+ const [user, setUser] = useState(null);
+ console.log("profile", profile);
+
+ React.useEffect(() => {
+ const unsubscribe = onAuthStateChanged(auth, async (user) => {
+ console.log(user);
+ if (user) {
+ setUser(user);
+ const res = await getUserInfo(user.uid);
+
+ setProfile(res);
+ console.log("res", res);
+ }
+ });
+
+ return () => unsubscribe();
+ }, []);
+
const handleChange = (e) => {
const { name, value } = e.target;
setProfile((prev) => ({ ...prev, [name]: value }));
@@ -20,10 +40,20 @@ const EditProfile = () => {
const handleSave = async () => {
try {
- const res = await updateUser(profile);
- if (res === 200) {
- navigate("/profile");
+ profile.uid = localStorage.getItem("id");
+ profile.email = user.email;
+ if (localStorage.getItem("details") === "false") {
+ const res = await createUser(profile);
+ if (res === 200) {
+ localStorage.setItem("details", "true");
+ }
+ } else {
+ const res = await updateUser(profile);
+
+ if (res === 200) {
+ }
}
+ navigate("/profile");
} catch (error) {
console.log(error);
}
@@ -49,7 +79,7 @@ const EditProfile = () => {
id="outlined"
name="name"
variant="standard"
- value={profile.name}
+ value={profile && profile.name}
onChange={handleChange}
fullWidth
/>
@@ -57,14 +87,14 @@ const EditProfile = () => {
label="Mobile Number"
name="phoneNumber"
variant="standard"
- value={profile.phoneNumber}
+ value={profile && profile.phoneNumber}
onChange={handleChange}
fullWidth
/>
{
Gender