11import { KeyboardEvent , useEffect , useState } from 'react' ;
2+ import { useDispatch } from 'react-redux' ;
3+ import { useHistory } from 'react-router' ;
24import cn from 'bem-cn-lite' ;
3- import { connect } from 'react-redux' ;
5+
46import { Button , TextInput , Icon , Link as ExternalLink } from '@gravity-ui/uikit' ;
5- // @ts -ignore
7+
68import { authenticate } from '../../store/reducers/authentication' ;
9+ import { useTypedSelector } from '../../utils/hooks' ;
710
811import ydbLogoIcon from '../../assets/icons/ydb.svg' ;
912import showIcon from '../../assets/icons/show.svg' ;
1013import hideIcon from '../../assets/icons/hide.svg' ;
14+ import closeIcon from '../../assets/icons/close.svg' ;
1115
1216import './Authentication.scss' ;
1317
1418const b = cn ( 'authentication' ) ;
1519
16- function Authentication ( { authenticate, error} : any ) {
20+ interface AuthenticationProps {
21+ returnUrl ?: string ;
22+ closable ?: boolean ;
23+ }
24+
25+ function Authentication ( { returnUrl, closable = false } : AuthenticationProps ) {
26+ const dispatch = useDispatch ( ) ;
27+ const history = useHistory ( ) ;
28+
29+ const { error} = useTypedSelector ( ( state ) => state . authentication ) ;
30+
1731 const [ login , setLogin ] = useState ( '' ) ;
1832 const [ pass , setPass ] = useState ( '' ) ;
1933 const [ loginError , setLoginError ] = useState ( '' ) ;
@@ -40,7 +54,13 @@ function Authentication({authenticate, error}: any) {
4054 } ;
4155
4256 const onLoginClick = ( ) => {
43- authenticate ( login , pass ) ;
57+ // @ts -expect-error
58+ // typed dispatch required, remove error expectation after adding it
59+ dispatch ( authenticate ( login , pass ) ) . then ( ( ) => {
60+ if ( returnUrl ) {
61+ history . replace ( decodeURI ( returnUrl ) ) ;
62+ }
63+ } ) ;
4464 } ;
4565
4666 const onEnterClick = ( e : KeyboardEvent < HTMLInputElement | HTMLTextAreaElement > ) => {
@@ -49,6 +69,10 @@ function Authentication({authenticate, error}: any) {
4969 }
5070 } ;
5171
72+ const onClose = ( ) => {
73+ history . go ( - 1 ) ;
74+ } ;
75+
5276 const onTogglePasswordVisibility = ( ) => {
5377 setShowPassword ( ( prev ) => ! prev ) ;
5478 } ;
@@ -106,18 +130,13 @@ function Authentication({authenticate, error}: any) {
106130 Sign in
107131 </ Button >
108132 </ form >
133+ { closable && history . length > 1 && (
134+ < Button onClick = { onClose } className = { b ( 'close' ) } >
135+ < Icon data = { closeIcon } size = { 24 } />
136+ </ Button >
137+ ) }
109138 </ section >
110139 ) ;
111140}
112141
113- function mapStateToProps ( state : any ) {
114- return {
115- error : state . authentication . error ,
116- } ;
117- }
118-
119- const mapDispatchToProps = {
120- authenticate,
121- } ;
122-
123- export default connect ( mapStateToProps , mapDispatchToProps ) ( Authentication ) ;
142+ export default Authentication ;
0 commit comments