1919import { Button , Form , Input } from 'antd' ;
2020import { AuthForm } from 'app/components' ;
2121import usePrefixI18N from 'app/hooks/useI18NPrefix' ;
22- import {
23- selectLoggedInUser ,
24- selectLoginLoading ,
25- selectOauth2Clients ,
26- } from 'app/slice/selectors' ;
27- import { getOauth2Clients } from 'app/slice/thunks' ;
28- import React , { useCallback , useEffect , useState } from 'react' ;
29- import { useDispatch , useSelector } from 'react-redux' ;
22+ import { User } from 'app/slice/types' ;
23+ import { StorageKeys } from 'globalConstants' ;
24+ import React , { useCallback , useState } from 'react' ;
3025import { Link , useHistory } from 'react-router-dom' ;
3126import styled from 'styled-components/macro' ;
3227import {
3328 BORDER_RADIUS ,
3429 LINE_HEIGHT_ICON_LG ,
30+ LINE_HEIGHT_ICON_XXL ,
3531 SPACE_MD ,
32+ SPACE_XS ,
3633} from 'styles/StyleConstants' ;
3734import { getToken } from 'utils/auth' ;
35+ import persistence from 'utils/persistence' ;
36+ import { AUTH_CLIENT_ICON_MAPPING } from './constants' ;
37+
38+ interface LoginFormProps {
39+ loading : boolean ;
40+ loggedInUser ?: User | null ;
41+ oauth2Clients : Array < { name : string ; value : string } > ;
42+ registerEnable ?: boolean ;
43+ inShare ?: boolean ;
44+ onLogin ?: ( value ) => void ;
45+ }
3846
3947export function LoginForm ( {
48+ loading,
49+ loggedInUser,
50+ oauth2Clients,
4051 registerEnable = true ,
41- modal = false ,
52+ inShare = false ,
4253 onLogin,
43- } : {
44- registerEnable ?: boolean ;
45- modal ?: boolean ;
46- onLogin ?: ( value ) => void ;
47- } ) {
54+ } : LoginFormProps ) {
4855 const [ switchUser , setSwitchUser ] = useState ( false ) ;
49- const dispatch = useDispatch ( ) ;
5056 const history = useHistory ( ) ;
51- const loading = useSelector ( selectLoginLoading ) ;
52- const loggedInUser = useSelector ( selectLoggedInUser ) ;
5357 const [ form ] = Form . useForm ( ) ;
5458 const logged = ! ! getToken ( ) ;
5559 const t = usePrefixI18N ( 'login' ) ;
5660 const tg = usePrefixI18N ( 'global' ) ;
57- const oauth2Clients = useSelector ( selectOauth2Clients ) ;
5861
5962 const toApp = useCallback ( ( ) => {
6063 history . replace ( '/' ) ;
6164 } , [ history ] ) ;
6265
63- useEffect ( ( ) => {
64- dispatch ( getOauth2Clients ( ) ) ;
65- } , [ dispatch ] ) ;
66-
6766 const onSwitch = useCallback ( ( ) => {
6867 setSwitchUser ( true ) ;
6968 } , [ ] ) ;
7069
71- let Oauth2BtnList = oauth2Clients . map ( client => {
72- return (
73- < Oauth2Button key = { client . value } href = { client . value } >
74- { client . name }
75- </ Oauth2Button >
76- ) ;
77- } ) ;
70+ const toAuthClient = useCallback (
71+ clientUrl => ( ) => {
72+ if ( inShare ) {
73+ persistence . session . save (
74+ StorageKeys . AuthRedirectUrl ,
75+ window . location . href ,
76+ ) ;
77+ }
78+ window . location . href = clientUrl ;
79+ } ,
80+ [ inShare ] ,
81+ ) ;
7882
7983 return (
8084 < AuthForm >
81- { logged && ! switchUser && ! modal ? (
85+ { logged && ! switchUser && ! inShare ? (
8286 < >
8387 < h2 > { t ( 'alreadyLoggedIn' ) } </ h2 >
8488 < UserPanel onClick = { toApp } >
@@ -132,7 +136,7 @@ export function LoginForm({
132136 </ Button >
133137 ) }
134138 </ Form . Item >
135- { ! modal && (
139+ { ! inShare && (
136140 < Links >
137141 < LinkButton to = "/forgetPassword" >
138142 { t ( 'forgotPassword' ) }
@@ -142,8 +146,22 @@ export function LoginForm({
142146 ) }
143147 </ Links >
144148 ) }
145-
146- { Oauth2BtnList }
149+ { oauth2Clients . length > 0 && (
150+ < >
151+ < AuthTitle > { t ( 'authTitle' ) } </ AuthTitle >
152+ { oauth2Clients . map ( ( { name, value } ) => (
153+ < AuthButton
154+ key = { value }
155+ size = "large"
156+ icon = { AUTH_CLIENT_ICON_MAPPING [ name . toLowerCase ( ) ] }
157+ onClick = { toAuthClient ( value ) }
158+ block
159+ >
160+ { name }
161+ </ AuthButton >
162+ ) ) }
163+ </ >
164+ ) }
147165 </ Form >
148166 ) }
149167 </ AuthForm >
@@ -154,16 +172,6 @@ const Links = styled.div`
154172 display: flex;
155173` ;
156174
157- const Oauth2Button = styled . a `
158- display: block;
159- height: 36px;
160- font-weight: bold;
161- line-height: 36px;
162- color: #fff;
163- text-align: center;
164- background-color: blue;
165- ` ;
166-
167175const LinkButton = styled ( Link ) `
168176 flex: 1;
169177 line-height: ${ LINE_HEIGHT_ICON_LG } ;
@@ -173,6 +181,20 @@ const LinkButton = styled(Link)`
173181 }
174182` ;
175183
184+ const AuthTitle = styled . p `
185+ line-height: ${ LINE_HEIGHT_ICON_XXL } ;
186+ color: ${ p => p . theme . textColorLight } ;
187+ text-align: center;
188+ ` ;
189+
190+ const AuthButton = styled ( Button ) `
191+ margin-bottom: ${ SPACE_XS } ;
192+
193+ &:last-child {
194+ margin-bottom: 0;
195+ }
196+ ` ;
197+
176198const UserPanel = styled . div `
177199 display: flex;
178200 padding: ${ SPACE_MD } ;
0 commit comments