fix fetch data to work even with autologin true

This commit is contained in:
anovazzi1 2024-06-24 17:33:20 -03:00
commit dfd419e438
2 changed files with 12 additions and 9 deletions

View file

@ -103,13 +103,6 @@ export default function App() {
if (isAuthenticated) {
try {
await setupAxiosDefaults();
const res = await getGlobalVariables();
setGlobalVariables(res);
checkHasStore();
fetchApiData();
resolve();
} catch (error) {
console.error("Failed to fetch data:", error);

View file

@ -1,11 +1,13 @@
import { createContext, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import Cookies from "universal-cookie";
import { getLoggedUser, requestLogout } from "../controllers/API";
import { getGlobalVariables, getLoggedUser, requestLogout } from "../controllers/API";
import useAlertStore from "../stores/alertStore";
import { useFolderStore } from "../stores/foldersStore";
import { Users } from "../types/api";
import { AuthContextType } from "../types/contexts/auth";
import { useGlobalVariablesStore } from "../stores/globalVariablesStore/globalVariables";
import { useStoreStore } from "../stores/storeStore";
const initialValue: AuthContextType = {
isAdmin: false,
@ -45,6 +47,11 @@ export function AuthProvider({ children }): React.ReactElement {
);
const getFoldersApi = useFolderStore((state) => state.getFoldersApi);
const setGlobalVariables = useGlobalVariablesStore(
(state) => state.setGlobalVariables,
);
const checkHasStore = useStoreStore((state) => state.checkHasStore);
const fetchApiData = useStoreStore((state) => state.fetchApiData);
useEffect(() => {
const storedAccessToken = cookies.get("access_token_lf");
@ -66,8 +73,11 @@ export function AuthProvider({ children }): React.ReactElement {
setUserData(user);
const isSuperUser = user!.is_superuser;
setIsAdmin(isSuperUser);
getFoldersApi(true, true);
const res = await getGlobalVariables();
setGlobalVariables(res);
checkHasStore();
fetchApiData();
})
.catch((error) => {
setLoading(false);