From d2c66675366ebc9032c22ed12c16be0af789986e Mon Sep 17 00:00:00 2001 From: keannu125 Date: Sun, 16 Apr 2023 18:37:51 +0800 Subject: [PATCH] Added previous session checker --- .../DrawerScreenSettings.tsx | 2 ++ .../PreviousSessionChecker.tsx | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/Components/PreviousSessionChecker/PreviousSessionChecker.tsx diff --git a/src/Components/Drawer/DrawerScreenSettings/DrawerScreenSettings.tsx b/src/Components/Drawer/DrawerScreenSettings/DrawerScreenSettings.tsx index 541d342..c691645 100644 --- a/src/Components/Drawer/DrawerScreenSettings/DrawerScreenSettings.tsx +++ b/src/Components/Drawer/DrawerScreenSettings/DrawerScreenSettings.tsx @@ -1,6 +1,7 @@ import { View, Image, Text } from "react-native"; import type { DrawerNavigationOptions } from "@react-navigation/drawer"; import AppIcon from "../../Icons/AppIcon/AppIcon"; +import PreviousSessionChecker from "../../PreviousSessionChecker/PreviousSessionChecker"; const DrawerScreenSettings: DrawerNavigationOptions = { headerTitleStyle: { color: "white", fontSize: 26 }, unmountOnBlur: true, @@ -18,6 +19,7 @@ const DrawerScreenSettings: DrawerNavigationOptions = { + ), diff --git a/src/Components/PreviousSessionChecker/PreviousSessionChecker.tsx b/src/Components/PreviousSessionChecker/PreviousSessionChecker.tsx new file mode 100644 index 0000000..ddd3b8e --- /dev/null +++ b/src/Components/PreviousSessionChecker/PreviousSessionChecker.tsx @@ -0,0 +1,33 @@ +import * as React from "react"; +import { View, Text, TextInput, ScrollView } from "react-native"; +import { useEffect, useCallback } from "react"; +import { useDispatch, useSelector } from "react-redux"; +import { UserInfo } from "../Api/Api"; +import { Toggle_Login } from "../../Features/Redux/Slices/LoginSlice/LoginSlice"; +import { SetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice"; +import { set_checked } from "../../Features/Redux/Slices/OldSession/OldSessionSlice"; +import { RootState } from "../../Features/Redux/Store/Store"; +export default function PreviousSessionChecker() { + const dispatch = useDispatch(); + const logged_in = useSelector((state: RootState) => state.logged_in.value); + // Function to check for previous login session + const check = useCallback(async () => { + if (await UserInfo()) { + if (logged_in !== true) { + console.log("Previous session found. Restoring"); + await dispatch(Toggle_Login()); + await dispatch(SetUser(await UserInfo())); + } + } else { + console.log("No old session found"); + localStorage.removeItem("token"); + } + await dispatch(set_checked()); + }, [dispatch, logged_in]); + useEffect(() => { + if (!logged_in) { + check(); + } + }, [check, logged_in]); + return ; +}