22import { Button , Typography } from '@material-ui/core' ;
33import Paper from '@material-ui/core/Paper' ;
44import _isNil from 'ramda/src/isNil' ;
5- import React , { useRef } from 'react' ;
5+ import React , { useRef , useState } from 'react' ;
66import { useTranslation } from 'react-i18next' ;
77import { useSelector } from 'react-redux' ;
88import { Redirect , useHistory } from 'react-router' ;
@@ -11,14 +11,34 @@ import { State } from '../../redux/rootReducer';
1111import sumBy from '../../utils/helpers/sumBy' ;
1212import LoginForm from '../LoginForm' ;
1313import Modal from '../Modal' ;
14+ import RegisterForm from '../RegisterForm' ;
1415import useStyles from './styles' ;
1516
1617const CartItems = ( { items } : any ) => {
1718 const classes = useStyles ( ) ;
1819 const { t } = useTranslation ( ) ;
1920 const history = useHistory ( ) ;
2021 const noUser = useRef ( { } ) ;
21-
22+ const [ open , setOpen ] = useState ( false ) ;
23+ const [ authView , setAuthView ] = useState ( 'register' ) ;
24+
25+ const showRegister = ( ) => {
26+ setAuthView ( 'register' ) ;
27+ } ;
28+
29+ const showLogin = ( ) => {
30+ setAuthView ( 'login' ) ;
31+ } ;
32+
33+ const handleOpen = ( e : any ) => {
34+ e . preventDefault ( ) ;
35+ setOpen ( true ) ;
36+ } ;
37+
38+ const handleClose = ( ) => {
39+ setOpen ( false ) ;
40+ } ;
41+
2242 const { user } = useSelector ( ( { auth } : State ) => auth ) ;
2343
2444 if ( ! user ) {
@@ -37,37 +57,33 @@ const CartItems = ({ items }: any) => {
3757 return < Redirect push to = { CHECKOUT } /> ;
3858 }
3959
60+ const ctaProps = ! _isNil ( user )
61+ ? { onClick : goTo ( CHECKOUT ) }
62+ : { onClick : handleOpen } ;
63+
64+ const registerLinkProps = { to : undefined , onClick : showRegister } ;
65+ const loginLinkProps = { to : undefined , onClick : showLogin } ;
66+
4067 return (
4168 < Paper className = { classes . paper } square >
4269 < Typography >
4370 { t ( 'cart.total' ) } ({ items . length } { courses } ): £{ total . toFixed ( 2 ) }
4471 </ Typography >
4572
46- { ! _isNil ( user ) ? (
47- < Button
48- variant = "contained"
49- fullWidth
50- color = "secondary"
51- onClick = { goTo ( CHECKOUT ) }
52- >
53- { t ( 'cart.proceedToCheckout' ) }
54- </ Button >
55- ) : (
56- < Modal
57- renderCta = { ( { handleClickOpen } : any ) => (
58- < Button
59- variant = "contained"
60- fullWidth
61- color = "secondary"
62- onClick = { handleClickOpen }
63- >
64- { t ( 'cart.proceedToCheckout' ) }
65- </ Button >
66- ) }
67- >
68- < LoginForm />
69- </ Modal >
70- ) }
73+ < Button variant = "contained" fullWidth color = "secondary" { ...ctaProps } >
74+ { t ( 'cart.proceedToCheckout' ) }
75+ </ Button >
76+ < Modal open = { open } handleClose = { handleClose } >
77+ { authView === 'register' && (
78+ < RegisterForm loginLinkProps = { loginLinkProps } />
79+ ) }
80+ { authView === 'login' && (
81+ < LoginForm
82+ showRemindPassword = { false }
83+ registerLinkProps = { registerLinkProps }
84+ />
85+ ) }
86+ </ Modal >
7187 </ Paper >
7288 ) ;
7389} ;
0 commit comments