Skip to content

Commit

Permalink
fix(dashboard): refresh too much
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI committed Sep 26, 2023
1 parent 1ba5942 commit 0dfd88e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions dashboard/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import ScrollToTop from './components/ScrollToTop';
import TopBar from './components/TopBar';
import VersionOutdatedAlert from './components/VersionOutdatedAlert';
import ModalConfirm from './components/ModalConfirm';
import DataLoader, { useDataLoader } from './components/DataLoader';
import DataLoader, { initialLoadState, useDataLoader } from './components/DataLoader';
import { Bounce, cssTransition, ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import SentryRoute from './components/Sentryroute';
Expand Down Expand Up @@ -90,6 +90,7 @@ if (ENV === 'production') {
const App = ({ resetRecoil }) => {
const authToken = useRecoilValue(authTokenState);
const user = useRecoilValue(userState);
const initialLoad = useRecoilValue(initialLoadState);

const recoilResetKey = useRecoilValue(recoilResetKeyState);
useEffect(() => {
Expand All @@ -104,14 +105,22 @@ const App = ({ resetRecoil }) => {
const onWindowFocus = (e) => {
if (authToken && e.newState === 'active') {
API.get({ path: '/check-auth' }) // will force logout if session is expired
.then(() => refresh()); // will refresh data if session is still valid
.then(() => {
if (!!initialLoad) {
// if the app is already loaded
// will refresh data if session is still valid
refresh();
} else {
console.log('initial load not done');
}
});
}
};
lifecycle.addEventListener('statechange', onWindowFocus);
return () => {
lifecycle.removeEventListener('statechange', onWindowFocus);
};
}, [authToken, refresh]);
}, [authToken, refresh, initialLoad]);

return (
<div className="main-container">
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/components/DataLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const cacheEffect = ({ onSet }) => {

const loaderTriggerState = atom({ key: 'loaderTriggerState', default: false });
const isLoadingState = atom({ key: 'isLoadingState', default: false });
const initialLoadState = atom({ key: 'isInitialLoadState', default: false });
export const initialLoadState = atom({ key: 'isInitialLoadState', default: false });
const fullScreenState = atom({ key: 'fullScreenState', default: true });
export const lastLoadState = atom({ key: 'lastLoadState', default: null, effects: [cacheEffect] });
export const loadingTextState = atom({ key: 'loadingTextState', default: 'Chargement des données' });
Expand Down

0 comments on commit 0dfd88e

Please sign in to comment.