Rev 3425 | Rev 3427 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useApi } from "@hooks";
import { getCredentials, getLanguages } from "@services/auth";
import { labelsAdapter } from "./utils/labels";
import { setIntlLabels } from "./redux/intl/intl.action";
import { setCrendentials } from "./redux/auth/auth.actions";
import AppRoutes from "./routes/routes";
import ErrorPage from "./pages/error/error-page";
import Spinner from "./components/UI/Spinner";
import "./styles/globals.scss";
export default function App() {
const {
data: credentials,
error: credentialsError,
loading: loadingCredentials,
execute: fetchCredentials,
} = useApi(getCredentials);
const {
data: labels,
error: labelsError,
loading: loadingLanguages,
} = useApi(getLanguages, { autoFetch: true });
const { is_logged_in } = useSelector((state) => state.auth);
const dispatch = useDispatch();
const loading = loadingCredentials || loadingLanguages;
const initialError = credentialsError || labelsError;
useEffect(() => {
dispatch(setIntlLabels(labelsAdapter(labels)));
}, [labels, dispatch]);
useEffect(() => {
if (credentials) {
window.localStorage.setItem("jwt", credentials.jwt);
dispatch(setCrendentials(credentials));
}
}, [credentials, dispatch]);
useEffect(() => {
fetchCredentials();
}, [is_logged_in, fetchCredentials]);
if (loading) {
return <Spinner />;
}
if (initialError) {
return <ErrorPage />;
}
return <AppRoutes />;
}