2022-07-25 16:50:25 +03:00
|
|
|
import { useEffect, useState } from "react";
|
2022-08-13 09:35:33 +03:00
|
|
|
import useI18n from "./hooks/useI18n";
|
2021-12-08 18:43:52 +03:00
|
|
|
import { appRouterSwitch } from "./routers";
|
2022-08-13 09:35:33 +03:00
|
|
|
import { globalService, locationService } from "./services";
|
2022-05-21 07:21:06 +03:00
|
|
|
import { useAppSelector } from "./store";
|
2022-08-13 09:35:33 +03:00
|
|
|
import * as storage from "./helpers/storage";
|
2021-12-08 18:43:52 +03:00
|
|
|
|
|
|
|
function App() {
|
2022-08-13 09:35:33 +03:00
|
|
|
const { setLocale } = useI18n();
|
2022-08-27 03:57:29 +03:00
|
|
|
const user = useAppSelector((state) => state.user.user);
|
2022-08-13 09:35:33 +03:00
|
|
|
const global = useAppSelector((state) => state.global);
|
2022-05-21 07:21:06 +03:00
|
|
|
const pathname = useAppSelector((state) => state.location.pathname);
|
2022-07-25 16:50:25 +03:00
|
|
|
const [isLoading, setLoading] = useState(true);
|
2021-12-08 18:43:52 +03:00
|
|
|
|
2022-07-25 16:50:25 +03:00
|
|
|
useEffect(() => {
|
|
|
|
locationService.updateStateWithLocation();
|
|
|
|
window.onpopstate = () => {
|
|
|
|
locationService.updateStateWithLocation();
|
|
|
|
};
|
2022-08-13 09:35:33 +03:00
|
|
|
globalService.initialState().then(() => {
|
|
|
|
setLoading(false);
|
|
|
|
});
|
2022-07-25 16:50:25 +03:00
|
|
|
}, []);
|
|
|
|
|
2022-08-27 03:57:29 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (user?.setting.locale) {
|
|
|
|
globalService.setLocale(user.setting.locale);
|
|
|
|
}
|
|
|
|
}, [user?.setting.locale]);
|
|
|
|
|
2022-08-13 09:35:33 +03:00
|
|
|
useEffect(() => {
|
|
|
|
setLocale(global.locale);
|
|
|
|
storage.set({
|
|
|
|
locale: global.locale,
|
|
|
|
});
|
2022-08-27 03:57:29 +03:00
|
|
|
}, [global.locale]);
|
2022-08-13 09:35:33 +03:00
|
|
|
|
2022-07-25 16:50:25 +03:00
|
|
|
return <>{isLoading ? null : appRouterSwitch(pathname)}</>;
|
2021-12-08 18:43:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|