diff --git a/src/components/DrawerSettings/CustomDrawerContent.tsx b/src/components/DrawerSettings/CustomDrawerContent.tsx index f453b46..c479bc7 100644 --- a/src/components/DrawerSettings/CustomDrawerContent.tsx +++ b/src/components/DrawerSettings/CustomDrawerContent.tsx @@ -14,7 +14,7 @@ import DrawerButton from "../Button/DrawerButton"; import { useDispatch, useSelector } from "react-redux"; import { RootState } from "../../features/redux/Store/Store"; import LogoutIcon from "../../icons/LogoutIcon/LogoutIcon"; -import { clear } from "../../features/redux/slices/AuthSlice/AuthSlice"; +import { logout } from "../../features/redux/slices/AuthSlice/AuthSlice"; import AsyncStorage from "@react-native-async-storage/async-storage"; export default function CustomDrawerContent(props: {}) { @@ -47,7 +47,7 @@ export default function CustomDrawerContent(props: {}) { { - dispatch(await clear()); + dispatch(logout()); await AsyncStorage.clear(); navigation.navigate("Login"); }} @@ -69,15 +69,6 @@ export default function CustomDrawerContent(props: {}) { Stud-E - { - navigation.navigate("Home"); - }} - > - - Home - { @@ -90,7 +81,6 @@ export default function CustomDrawerContent(props: {}) { { - dispatch(clear()); navigation.navigate("Register"); }} > diff --git a/src/features/redux/Store/Store.tsx b/src/features/redux/Store/Store.tsx index 55a6590..21d3f6d 100644 --- a/src/features/redux/Store/Store.tsx +++ b/src/features/redux/Store/Store.tsx @@ -1,9 +1,11 @@ import { configureStore } from "@reduxjs/toolkit"; import AuthReducer from "../slices/AuthSlice/AuthSlice"; +import UserReducer from "../slices/UserSlice/UserSlice"; const store = configureStore({ reducer: { auth: AuthReducer, + user: UserReducer, }, }); diff --git a/src/features/redux/slices/AuthSlice/AuthSlice.tsx b/src/features/redux/slices/AuthSlice/AuthSlice.tsx index b575579..a4a54f6 100644 --- a/src/features/redux/slices/AuthSlice/AuthSlice.tsx +++ b/src/features/redux/slices/AuthSlice/AuthSlice.tsx @@ -4,10 +4,6 @@ export const AuthSlice = createSlice({ name: "Auth", initialState: { creds: { - email: "", - uid: "", - username: "", - full_name: "", logged_in: false, }, }, @@ -15,28 +11,13 @@ export const AuthSlice = createSlice({ login: (state) => { state.creds.logged_in = true; }, - setUser: (state, action) => { - state.creds = { - email: action.payload.email, - uid: action.payload.uid, - username: action.payload.username, - full_name: action.payload.full_name, - logged_in: true, - }; - }, - clear: (state) => { - state.creds = { - email: "", - uid: "", - username: "", - full_name: "", - logged_in: false, - }; + logout: (state) => { + state.creds.logged_in = false; }, }, }); // Action creators are generated for each case reducer function -export const { login, setUser, clear } = AuthSlice.actions; +export const { login, logout } = AuthSlice.actions; export default AuthSlice.reducer; diff --git a/src/features/redux/slices/UserSlice/UserSlice.tsx b/src/features/redux/slices/UserSlice/UserSlice.tsx new file mode 100644 index 0000000..f840470 --- /dev/null +++ b/src/features/redux/slices/UserSlice/UserSlice.tsx @@ -0,0 +1,51 @@ +import { createSlice } from "@reduxjs/toolkit"; + +export const UserSlice = createSlice({ + name: "Auth", + initialState: { + creds: { + email: "", + uid: "", + username: "", + first_name: "", + last_name: "", + full_name: "", + year_level: "", + semester: " ", + course: "", + }, + }, + reducers: { + setUser: (state, action) => { + state.creds = { + email: action.payload.email, + uid: action.payload.uid, + username: action.payload.username, + first_name: action.payload.first_name, + last_name: action.payload.last_name, + full_name: action.payload.first_name + " " + action.payload.last_name, + year_level: action.payload.year_level, + semester: action.payload.semester, + course: action.payload.course, + }; + }, + clear: (state) => { + state.creds = { + email: "", + uid: "", + username: "", + first_name: "", + last_name: "", + full_name: "", + year_level: "", + semester: " ", + course: "", + }; + }, + }, +}); + +// Action creators are generated for each case reducer function +export const { setUser, clear } = UserSlice.actions; + +export default UserSlice.reducer; diff --git a/src/routes/Login/Login.tsx b/src/routes/Login/Login.tsx index ee3e4d4..40af8bb 100644 --- a/src/routes/Login/Login.tsx +++ b/src/routes/Login/Login.tsx @@ -17,7 +17,8 @@ import { RootDrawerParamList } from "../../interfaces/Interfaces"; import { UserInfo, UserLogin } from "../../components/Api/Api"; import { ParseLoginError } from "../../components/ParseError/ParseError"; import AnimatedContainer from "../../components/AnimatedContainer/AnimatedContainer"; -import { setUser as setStateUser } from "../../features/redux/slices/AuthSlice/AuthSlice"; +import { setUser as setStateUser } from "../../features/redux/slices/UserSlice/UserSlice"; +import { login } from "../../features/redux/slices/AuthSlice/AuthSlice"; export default function Login() { const navigation = useNavigation(); @@ -82,6 +83,7 @@ export default function Login() { if (result[0]) { setUser({ ...user, username: "", password: "", error: "" }); let user_info = await UserInfo(); + dispatch(login()); dispatch(setStateUser(user_info)); // Redirect to onboarding if no year level, course, or semester specified if ( diff --git a/src/routes/Revalidation/Revalidation.tsx b/src/routes/Revalidation/Revalidation.tsx index 0e0ccd2..cfac7d0 100644 --- a/src/routes/Revalidation/Revalidation.tsx +++ b/src/routes/Revalidation/Revalidation.tsx @@ -7,7 +7,8 @@ import { colors } from "../../styles"; import { useEffect, useState } from "react"; import { useNavigation } from "@react-navigation/native"; import { RootDrawerParamList } from "../../interfaces/Interfaces"; -import { setUser } from "../../features/redux/slices/AuthSlice/AuthSlice"; +import { login } from "../../features/redux/slices/AuthSlice/AuthSlice"; +import { setUser } from "../../features/redux/slices/UserSlice/UserSlice"; import AnimatedContainer from "../../components/AnimatedContainer/AnimatedContainer"; export default function Revalidation() { @@ -19,7 +20,8 @@ export default function Revalidation() { TokenRefresh().then(async (response) => { let user_info = await UserInfo(); if (response && user_info[0]) { - await dispatch(setUser(user_info)); + dispatch(login()); + dispatch(setUser(user_info)); if ( !( user_info[1].year_level ||