Overhauled redux states and separated auth state from user info state

This commit is contained in:
Keannu Bernasol 2023-07-06 20:56:19 +08:00
parent acfb28ce3c
commit 9a246b45e1
6 changed files with 65 additions and 37 deletions

View file

@ -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: {}) {
<DrawerButton
color={colors.blue_2}
onPress={async () => {
dispatch(await clear());
dispatch(logout());
await AsyncStorage.clear();
navigation.navigate("Login");
}}
@ -69,15 +69,6 @@ export default function CustomDrawerContent(props: {}) {
<AppIcon size={32} />
<Text style={styles.text_white_medium}>Stud-E</Text>
</View>
<DrawerButton
color={colors.blue_2}
onPress={() => {
navigation.navigate("Home");
}}
>
<HomeIcon size={32} />
<Text style={styles.text_white_medium}>Home</Text>
</DrawerButton>
<DrawerButton
color={colors.blue_2}
onPress={() => {
@ -90,7 +81,6 @@ export default function CustomDrawerContent(props: {}) {
<DrawerButton
color={colors.blue_2}
onPress={() => {
dispatch(clear());
navigation.navigate("Register");
}}
>

View file

@ -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,
},
});

View file

@ -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;

View file

@ -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;

View file

@ -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<RootDrawerParamList>();
@ -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 (

View file

@ -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 ||