2022-04-05 18:21:25 +05:30
|
|
|
import { notification } from 'antd';
|
|
|
|
|
import getLatestVersion from 'api/user/getLatestVersion';
|
|
|
|
|
import getVersion from 'api/user/getVersion';
|
2021-10-11 16:21:15 +05:30
|
|
|
import ROUTES from 'constants/routes';
|
2021-10-20 09:24:55 +05:30
|
|
|
import TopNav from 'container/Header';
|
|
|
|
|
import SideNav from 'container/SideNav';
|
2022-04-05 18:21:25 +05:30
|
|
|
import useFetch from 'hooks/useFetch';
|
2021-10-11 16:21:15 +05:30
|
|
|
import history from 'lib/history';
|
2022-04-05 18:21:25 +05:30
|
|
|
import React, { ReactNode, useEffect, useRef, useState } from 'react';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2022-03-24 12:06:57 +05:30
|
|
|
import { useLocation } from 'react-router-dom';
|
2022-04-05 18:21:25 +05:30
|
|
|
import { Dispatch } from 'redux';
|
2021-10-11 16:21:15 +05:30
|
|
|
import { AppState } from 'store/reducers';
|
2022-04-05 18:21:25 +05:30
|
|
|
import AppActions from 'types/actions';
|
|
|
|
|
import {
|
|
|
|
|
UPDATE_CURRENT_ERROR,
|
|
|
|
|
UPDATE_CURRENT_VERSION,
|
|
|
|
|
UPDATE_LATEST_VERSION,
|
|
|
|
|
UPDATE_LATEST_VERSION_ERROR,
|
|
|
|
|
} from 'types/actions/app';
|
2021-10-11 16:21:15 +05:30
|
|
|
import AppReducer from 'types/reducer/app';
|
2021-05-16 18:35:50 +05:30
|
|
|
|
2022-03-14 20:12:42 +05:30
|
|
|
import { Content, Layout } from './styles';
|
2021-05-16 18:35:50 +05:30
|
|
|
|
2022-03-24 12:06:57 +05:30
|
|
|
function AppLayout(props: AppLayoutProps): JSX.Element {
|
2021-10-11 16:21:15 +05:30
|
|
|
const { isLoggedIn } = useSelector<AppState, AppReducer>((state) => state.app);
|
2022-03-24 12:06:57 +05:30
|
|
|
const { pathname } = useLocation();
|
2022-04-05 18:21:25 +05:30
|
|
|
const { t } = useTranslation();
|
2021-05-23 14:15:13 +05:30
|
|
|
|
2022-03-24 12:06:57 +05:30
|
|
|
const [isSignUpPage, setIsSignUpPage] = useState(ROUTES.SIGN_UP === pathname);
|
|
|
|
|
|
2022-04-05 18:21:25 +05:30
|
|
|
const { payload: versionPayload, loading, error: getVersionError } = useFetch(
|
|
|
|
|
getVersion,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
payload: latestVersionPayload,
|
|
|
|
|
loading: latestLoading,
|
|
|
|
|
error: latestError,
|
|
|
|
|
} = useFetch(getLatestVersion);
|
|
|
|
|
|
2022-03-24 12:06:57 +05:30
|
|
|
const { children } = props;
|
2021-05-23 14:15:13 +05:30
|
|
|
|
2022-04-05 18:21:25 +05:30
|
|
|
const dispatch = useDispatch<Dispatch<AppActions>>();
|
|
|
|
|
|
2021-10-22 17:05:10 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isLoggedIn) {
|
|
|
|
|
setIsSignUpPage(true);
|
|
|
|
|
history.push(ROUTES.SIGN_UP);
|
2022-03-22 12:10:31 +05:30
|
|
|
} else if (isSignUpPage) {
|
|
|
|
|
setIsSignUpPage(false);
|
2021-10-11 16:21:15 +05:30
|
|
|
}
|
2021-10-22 17:05:10 +05:30
|
|
|
}, [isLoggedIn, isSignUpPage]);
|
2021-10-20 09:24:55 +05:30
|
|
|
|
2022-04-05 18:21:25 +05:30
|
|
|
const latestCurrentCounter = useRef(0);
|
|
|
|
|
const latestVersionCounter = useRef(0);
|
|
|
|
|
|
2022-04-01 15:47:39 +05:30
|
|
|
useEffect(() => {
|
2022-04-04 10:25:15 +05:30
|
|
|
if (isLoggedIn && pathname === ROUTES.SIGN_UP) {
|
2022-04-01 15:47:39 +05:30
|
|
|
history.push(ROUTES.APPLICATION);
|
|
|
|
|
}
|
2022-04-05 18:21:25 +05:30
|
|
|
|
|
|
|
|
if (!latestLoading && latestError && latestCurrentCounter.current === 0) {
|
|
|
|
|
latestCurrentCounter.current = 1;
|
|
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
|
type: UPDATE_LATEST_VERSION_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
isError: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
notification.error({
|
|
|
|
|
message: t('oops_something_went_wrong_version'),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!loading && getVersionError && latestVersionCounter.current === 0) {
|
|
|
|
|
latestVersionCounter.current = 1;
|
|
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
|
type: UPDATE_CURRENT_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
isError: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
notification.error({
|
|
|
|
|
message: t('oops_something_went_wrong_version'),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!latestLoading && versionPayload) {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: UPDATE_CURRENT_VERSION,
|
|
|
|
|
payload: {
|
|
|
|
|
currentVersion: versionPayload.version,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!loading && latestVersionPayload) {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: UPDATE_LATEST_VERSION,
|
|
|
|
|
payload: {
|
|
|
|
|
latestVersion: latestVersionPayload.name,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [
|
|
|
|
|
dispatch,
|
|
|
|
|
loading,
|
|
|
|
|
latestLoading,
|
|
|
|
|
versionPayload,
|
|
|
|
|
latestVersionPayload,
|
|
|
|
|
isLoggedIn,
|
|
|
|
|
pathname,
|
|
|
|
|
getVersionError,
|
|
|
|
|
latestError,
|
|
|
|
|
t,
|
|
|
|
|
]);
|
2022-04-01 15:47:39 +05:30
|
|
|
|
2021-05-16 18:35:50 +05:30
|
|
|
return (
|
2021-12-02 20:12:38 +05:30
|
|
|
<Layout>
|
2021-10-22 17:05:10 +05:30
|
|
|
{!isSignUpPage && <SideNav />}
|
2021-12-02 20:12:38 +05:30
|
|
|
<Layout>
|
|
|
|
|
<Content>
|
2021-10-22 17:05:10 +05:30
|
|
|
{!isSignUpPage && <TopNav />}
|
2021-05-16 18:35:50 +05:30
|
|
|
{children}
|
|
|
|
|
</Content>
|
|
|
|
|
</Layout>
|
|
|
|
|
</Layout>
|
|
|
|
|
);
|
2022-03-24 12:06:57 +05:30
|
|
|
}
|
2021-05-16 18:35:50 +05:30
|
|
|
|
2021-10-22 17:05:10 +05:30
|
|
|
interface AppLayoutProps {
|
|
|
|
|
children: ReactNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default AppLayout;
|