11import "./style.scss" ;
22
3+ import { allLanguages , LanguageEntity } from "@dzcode.io/models/dist/language" ;
34import { ErrorBoundary } from "@dzcode.io/ui/dist/error-boundary" ;
45import Container from "@material-ui/core/Container" ;
56import { ComponentType , FC , lazy , Suspense , useEffect } from "react" ;
6- import { Route , RouteProps , Switch , useLocation } from "react-router-dom" ;
7+ import { useDispatch , useSelector } from "react-redux" ;
8+ import { Route , RouteProps , Switch , useLocation , useRouteMatch } from "react-router-dom" ;
79import { Footer } from "src/apps/main/components/footer" ;
810import { Navbar } from "src/apps/main/components/navbar" ;
911import { Theme } from "src/apps/main/components/theme" ;
1012import { getEnv } from "src/common/utils" ;
13+ import { urlLanguageRegEx } from "src/common/utils/language" ;
1114import { Loading } from "src/components/loading" ;
1215
16+ import { Dispatch , StateInterface } from "../redux" ;
17+ import { SettingsState } from "../redux/reducers/settings" ;
18+
1319interface RouteInterface extends RouteProps {
1420 import : Promise < { default : ComponentType } > ;
1521}
@@ -50,17 +56,29 @@ const routes: RouteInterface[] = [
5056] ;
5157
5258export const App : FC = ( ) => {
53- if ( getEnv ( ) !== "development" ) {
54- const location = useLocation ( ) ;
59+ const location = useLocation ( ) ;
60+ const match = useRouteMatch < { lang ?: LanguageEntity [ "code" ] } > ( urlLanguageRegEx ) ;
61+ const { language } = useSelector < StateInterface , SettingsState > ( ( state ) => state . settings ) ;
62+ const dispatch = useDispatch < Dispatch < SettingsState > > ( ) ;
5563
56- useEffect ( ( ) => {
64+ useEffect ( ( ) => {
65+ if ( getEnv ( ) !== "development" ) {
5766 if ( window . ga ) {
5867 window . ga ( "set" , "page" , location . pathname ) ;
5968 window . ga ( "send" , "pageview" ) ;
6069 window . fbq ( "track" , "PageView" ) ;
6170 }
62- } , [ location ] ) ;
63- }
71+ }
72+
73+ const urlLanguage =
74+ allLanguages . find ( ( { code } ) => code === match ?. params . lang ) || allLanguages [ 0 ] ;
75+ if ( urlLanguage . code !== language . code ) {
76+ dispatch ( {
77+ type : "UPDATE_SETTINGS" ,
78+ payload : { language : urlLanguage } ,
79+ } ) ;
80+ }
81+ } , [ location ] ) ;
6482
6583 return (
6684 < Theme >
@@ -76,8 +94,13 @@ export const App: FC = () => {
7694 < Container maxWidth = "lg" style = { { paddingTop : "130px" } } >
7795 < Suspense fallback = { < Loading /> } >
7896 < Switch >
79- { routes . map ( ( route , index ) => (
80- < Route { ...route } key = { `route-${ index } ` } component = { lazy ( ( ) => route . import ) } />
97+ { routes . map ( ( { import : im , path, ...route } , index ) => (
98+ < Route
99+ { ...route }
100+ path = { path ? `${ urlLanguageRegEx } ${ path } ` : undefined }
101+ key = { `route-${ index } ` }
102+ component = { lazy ( ( ) => im ) }
103+ />
81104 ) ) }
82105 </ Switch >
83106 </ Suspense >
0 commit comments