feat: signup logic is now fixed (#349)

This commit is contained in:
pal-sig 2021-10-22 17:05:10 +05:30 committed by GitHub
parent 992644dff7
commit 73e3e061e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 26 deletions

View File

@ -1,45 +1,41 @@
import { Layout } from 'antd'; import { Layout } from 'antd';
import get from 'api/browser/localstorage/get';
import ROUTES from 'constants/routes'; import ROUTES from 'constants/routes';
import TopNav from 'container/Header'; import TopNav from 'container/Header';
import SideNav from 'container/SideNav'; import SideNav from 'container/SideNav';
import history from 'lib/history'; import history from 'lib/history';
import React, { ReactNode, useEffect } from 'react'; import React, { ReactNode, useEffect, useState } from 'react';
import { useSelector } from 'react-redux'; import { useSelector } from 'react-redux';
import { AppState } from 'store/reducers'; import { AppState } from 'store/reducers';
import AppReducer from 'types/reducer/app'; import AppReducer from 'types/reducer/app';
const { Content, Footer } = Layout; const { Content, Footer } = Layout;
interface BaseLayoutProps {
children: ReactNode;
}
const BaseLayout: React.FC<BaseLayoutProps> = ({ children }) => { const AppLayout: React.FC<AppLayoutProps> = ({ children }) => {
const { isLoggedIn } = useSelector<AppState, AppReducer>((state) => state.app); const { isLoggedIn } = useSelector<AppState, AppReducer>((state) => state.app);
const isLoggedInLocalStorage = get('isLoggedIn');
const [isSignUpPage, setIsSignUpPage] = useState(
ROUTES.SIGN_UP === location.pathname,
);
useEffect(() => { useEffect(() => {
if (isLoggedIn && history.location.pathname === '/') { if (!isLoggedIn) {
history.push(ROUTES.APPLICATION); setIsSignUpPage(true);
} history.push(ROUTES.SIGN_UP);
if (!isLoggedIn && isLoggedInLocalStorage !== null) {
history.push(ROUTES.APPLICATION);
} else { } else {
if (isLoggedInLocalStorage === null) { if (isSignUpPage) {
history.push(ROUTES.SIGN_UP); setIsSignUpPage(false);
} }
} }
}, [isLoggedIn, isLoggedInLocalStorage]); }, [isLoggedIn, isSignUpPage]);
const currentYear = new Date().getFullYear(); const currentYear = new Date().getFullYear();
return ( return (
<Layout style={{ minHeight: '100vh' }}> <Layout style={{ minHeight: '100vh' }}>
<SideNav /> {!isSignUpPage && <SideNav />}
<Layout className="site-layout"> <Layout className="site-layout">
<Content style={{ margin: '0 16px' }}> <Content style={{ margin: '0 16px' }}>
<TopNav /> {!isSignUpPage && <TopNav />}
{children} {children}
</Content> </Content>
<Footer style={{ textAlign: 'center', fontSize: 10 }}> <Footer style={{ textAlign: 'center', fontSize: 10 }}>
@ -50,4 +46,8 @@ const BaseLayout: React.FC<BaseLayoutProps> = ({ children }) => {
); );
}; };
export default BaseLayout; interface AppLayoutProps {
children: ReactNode;
}
export default AppLayout;

View File

@ -1,18 +1,17 @@
import { Button, Input, Typography } from 'antd'; import { Button, Input, Typography } from 'antd';
import signup from 'api/user/signup'; import signup from 'api/user/signup';
import { IS_LOGGED_IN } from 'constants/auth';
import ROUTES from 'constants/routes'; import ROUTES from 'constants/routes';
import history from 'lib/history'; import history from 'lib/history';
import React, { useState } from 'react'; import React, { useState } from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
import { ThunkDispatch } from 'redux-thunk'; import { ThunkDispatch } from 'redux-thunk';
import { GlobalTimeLoading } from 'store/actions'; import { GlobalTimeLoading, UserLoggedIn } from 'store/actions';
import AppActions from 'types/actions'; import AppActions from 'types/actions';
import { ButtonContainer, Container, FormWrapper, Title } from './styles'; import { ButtonContainer, Container, FormWrapper, Title } from './styles';
const Signup = ({ globalLoading }: SignupProps): JSX.Element => { const Signup = ({ globalLoading, loggedIn }: SignupProps): JSX.Element => {
const [state, setState] = useState({ submitted: false }); const [state, setState] = useState({ submitted: false });
const [formState, setFormState] = useState({ const [formState, setFormState] = useState({
firstName: { value: '' }, firstName: { value: '' },
@ -53,9 +52,9 @@ const Signup = ({ globalLoading }: SignupProps): JSX.Element => {
}); });
if (response.statusCode === 200) { if (response.statusCode === 200) {
localStorage.setItem(IS_LOGGED_IN, 'yes'); loggedIn();
history.push(ROUTES.APPLICATION);
globalLoading(); globalLoading();
history.push(ROUTES.APPLICATION);
} else { } else {
// @TODO throw a error notification here // @TODO throw a error notification here
} }
@ -121,12 +120,14 @@ const Signup = ({ globalLoading }: SignupProps): JSX.Element => {
interface DispatchProps { interface DispatchProps {
globalLoading: () => void; globalLoading: () => void;
loggedIn: () => void;
} }
const mapDispatchToProps = ( const mapDispatchToProps = (
dispatch: ThunkDispatch<unknown, unknown, AppActions>, dispatch: ThunkDispatch<unknown, unknown, AppActions>,
): DispatchProps => ({ ): DispatchProps => ({
globalLoading: bindActionCreators(GlobalTimeLoading, dispatch), globalLoading: bindActionCreators(GlobalTimeLoading, dispatch),
loggedIn: bindActionCreators(UserLoggedIn, dispatch),
}); });
type SignupProps = DispatchProps; type SignupProps = DispatchProps;

View File

@ -1 +1,2 @@
export * from './toggleDarkMode'; export * from './toggleDarkMode';
export * from './userLoggedIn';

View File

@ -0,0 +1,13 @@
import { IS_LOGGED_IN } from 'constants/auth';
import { Dispatch } from 'redux';
import AppActions from 'types/actions';
export const UserLoggedIn = (): ((dispatch: Dispatch<AppActions>) => void) => {
return (dispatch: Dispatch<AppActions>): void => {
localStorage.setItem(IS_LOGGED_IN, 'yes');
dispatch({
type: 'LOGGED_IN',
});
};
};

View File

@ -1,5 +1,5 @@
import { IS_LOGGED_IN } from 'constants/auth'; import { IS_LOGGED_IN } from 'constants/auth';
import { AppAction, SWITCH_DARK_MODE } from 'types/actions/app'; import { AppAction, LOGGED_IN, SWITCH_DARK_MODE } from 'types/actions/app';
import InitialValueTypes from 'types/reducer/app'; import InitialValueTypes from 'types/reducer/app';
const InitialValue: InitialValueTypes = { const InitialValue: InitialValueTypes = {
@ -19,6 +19,13 @@ const appReducer = (
}; };
} }
case LOGGED_IN: {
return {
...state,
isLoggedIn: true,
};
}
default: default:
return state; return state;
} }

View File

@ -1,7 +1,12 @@
export const SWITCH_DARK_MODE = 'SWITCH_DARK_MODE'; export const SWITCH_DARK_MODE = 'SWITCH_DARK_MODE';
export const LOGGED_IN = 'LOGGED_IN';
export interface SwitchDarkMode { export interface SwitchDarkMode {
type: typeof SWITCH_DARK_MODE; type: typeof SWITCH_DARK_MODE;
} }
export type AppAction = SwitchDarkMode; export interface LoggedInUser {
type: typeof LOGGED_IN;
}
export type AppAction = SwitchDarkMode | LoggedInUser;