Skip to content

Commit

Permalink
🔥 Remove loaded from Redux
Browse files Browse the repository at this point in the history
  • Loading branch information
xdaruis committed Aug 24, 2024
1 parent b1d76be commit 1ff07c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
50 changes: 26 additions & 24 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios from 'axios';
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import React, { useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';
import { Route, BrowserRouter as Router, Routes } from 'react-router-dom';

import Layout from './components/Layout/index.js';
import Loader from './components/Loader.js';
import { loaded, login, logout } from './features/auth.js';
import { login, logout } from './features/auth.js';
import Home from './routes/Home.js';
import Login from './routes/Login.js';
import NotFound from './routes/NotFound.js';
Expand All @@ -20,33 +20,35 @@ axios.defaults.withCredentials = true;

const App = () => {
const dispatch = useDispatch();
const isLoading = useSelector((state) => state.auth.isLoading);

const [loaded, setLoaded] = useState(false);
useEffect(() => {
if (localStorage.token) {
axios
.get('/api/user/profile/')
.then((response) => {
const data = response.data;
if (data.username) {
dispatch(
login({ token: localStorage.token, username: data.username })
);
} else {
const setUserSession = async () => {
if (localStorage.token) {
await axios
.get('/api/user/profile/')
.then((response) => {
const data = response.data;
if (data.username) {
dispatch(
login({ token: localStorage.token, username: data.username })
);
} else {
dispatch(logout());
}
})
.catch((error) => {
dispatch(logout());
}
})
.catch((error) => {
dispatch(logout());
alert(error);
});
}
dispatch(loaded());
alert(error);
});
}
setLoaded(true);
};
setUserSession();
}, []);

if (!loaded) return <Loader />;
return (
<Router>
{isLoading && <Loader />}
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
Expand Down
8 changes: 1 addition & 7 deletions frontend/src/features/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import setAuthToken from '../utils/setAuthToken';
const initialState = {
token: localStorage.getItem('token'),
isAuthenticated: false,
username: null,
isLoading: true
username: null
};

const authSlice = createSlice({
Expand All @@ -18,19 +17,14 @@ const authSlice = createSlice({
setAuthToken(token);
state.token = token;
state.username = username;
state.isLoading = false;
state.isAuthenticated = true;
},
logout: (state) => {
setAuthToken();
state.token = null;
state.username = null;
state.isLoading = false;
state.isAuthenticated = false;
localStorage.removeItem('token');
},
loaded: (state) => {
state.isLoading = false;
}
}
});
Expand Down

0 comments on commit 1ff07c3

Please sign in to comment.