11/* @flow strict-local */
2- import React , { PureComponent } from 'react' ;
2+ import React , { useState , useCallback } from 'react' ;
33import type { Node } from 'react' ;
44import { Keyboard } from 'react-native' ;
55
@@ -21,12 +21,6 @@ type Props = $ReadOnly<{|
2121 route : RouteProp < 'realm-input' , { | initial : boolean | void | } > ,
2222| } > ;
2323
24- type State = { |
25- realmInputValue : string ,
26- error : string | null ,
27- progress : boolean ,
28- | } ;
29-
3024const urlFromInputValue = ( realmInputValue : string ) : URL | void => {
3125 const withScheme = / ^ h t t p s ? : \/ \/ / . test ( realmInputValue )
3226 ? realmInputValue
@@ -35,86 +29,79 @@ const urlFromInputValue = (realmInputValue: string): URL | void => {
3529 return tryParseUrl ( withScheme ) ;
3630} ;
3731
38- export default class RealmInputScreen extends PureComponent < Props , State > {
39- state : State = {
40- progress : false ,
41- realmInputValue : '' ,
42- error : null ,
43- } ;
44-
45- tryRealm : ( ) = > Promise < void > = async ( ) => {
46- const { realmInputValue } = this . state ;
32+ export default function RealmInputScreen ( props : Props ) : Node {
33+ const [ progress , setProgress ] = useState < boolean > ( false ) ;
34+ const [ realmInputValue , setRealmInputValue ] = useState < string > ( '' ) ;
35+ const [ error , setError ] = useState < string | null > ( null ) ;
4736
37+ const tryRealm = useCallback ( async ( ) => {
4838 const parsedRealm = urlFromInputValue ( realmInputValue ) ;
4939 if ( ! parsedRealm ) {
50- this . setState ( { error : 'Please enter a valid URL' } ) ;
40+ setError ( 'Please enter a valid URL' ) ;
5141 return ;
5242 }
5343 if ( parsedRealm . username !== '' ) {
54- this . setState ( { error : 'Please enter the server URL, not your email' } ) ;
44+ setError ( 'Please enter the server URL, not your email' ) ;
5545 return ;
5646 }
5747
58- this . setState ( {
59- progress : true ,
60- error : null ,
61- } ) ;
48+ setProgress ( true ) ;
49+ setError ( null ) ;
6250 try {
6351 const serverSettings : ApiResponseServerSettings = await api . getServerSettings ( parsedRealm ) ;
6452 NavigationService . dispatch ( navigateToAuth ( serverSettings ) ) ;
6553 Keyboard . dismiss ( ) ;
6654 } catch ( err ) {
67- this . setState ( { error : 'Cannot connect to server' } ) ;
55+ setError ( 'Cannot connect to server' ) ;
6856 /* eslint-disable no-console */
6957 console . warn ( 'RealmInputScreen: failed to connect to server:' , err ) ;
7058 console . warn ( err . stack ) ;
7159 } finally {
72- this . setState ( { progress : false } ) ;
60+ setProgress ( false ) ;
7361 }
74- } ;
62+ } , [ realmInputValue ] ) ;
7563
76- handleRealmChange : string => void = value => this . setState ( { realmInputValue : value } ) ;
64+ const handleRealmChange = useCallback ( value => {
65+ setRealmInputValue ( value ) ;
66+ } , [ ] ) ;
7767
78- render ( ) : Node {
79- const { navigation } = this . props ;
80- const { progress , error , realmInputValue } = this . state ;
68+ const { navigation } = props ;
8169
82- const styles = {
83- input : { marginTop : 16 , marginBottom : 8 } ,
84- hintText : { paddingLeft : 2 , fontSize : 12 } ,
85- button : { marginTop : 8 } ,
86- } ;
70+ const styles = {
71+ input : { marginTop : 16 , marginBottom : 8 } ,
72+ hintText : { paddingLeft : 2 , fontSize : 12 } ,
73+ button : { marginTop : 8 } ,
74+ } ;
8775
88- return (
89- < Screen
90- title = "Welcome"
91- canGoBack = { ! this . props . route . params . initial }
92- padding
93- centerContent
94- keyboardShouldPersistTaps = "always"
95- shouldShowLoadingBanner = { false }
96- >
97- < ZulipTextIntl text = "Enter your Zulip server URL:" />
98- < SmartUrlInput
99- style = { styles . input }
100- navigation = { navigation }
101- onChangeText = { this . handleRealmChange }
102- onSubmitEditing = { this . tryRealm }
103- enablesReturnKeyAutomatically
104- />
105- { error !== null ? (
106- < ErrorMsg error = { error } />
107- ) : (
108- < ZulipTextIntl text = "e.g. zulip.example.com" style = { styles . hintText } />
109- ) }
110- < ZulipButton
111- style = { styles . button }
112- text = "Enter"
113- progress = { progress }
114- onPress = { this . tryRealm }
115- disabled = { urlFromInputValue ( realmInputValue ) === undefined }
116- />
117- </ Screen >
118- ) ;
119- }
76+ return (
77+ < Screen
78+ title = "Welcome"
79+ canGoBack = { ! props . route . params . initial }
80+ padding
81+ centerContent
82+ keyboardShouldPersistTaps = "always"
83+ shouldShowLoadingBanner = { false }
84+ >
85+ < ZulipTextIntl text = "Enter your Zulip server URL:" />
86+ < SmartUrlInput
87+ style = { styles . input }
88+ navigation = { navigation }
89+ onChangeText = { handleRealmChange }
90+ onSubmitEditing = { tryRealm }
91+ enablesReturnKeyAutomatically
92+ />
93+ { error !== null ? (
94+ < ErrorMsg error = { error } />
95+ ) : (
96+ < ZulipTextIntl text = "e.g. zulip.example.com" style = { styles . hintText } />
97+ ) }
98+ < ZulipButton
99+ style = { styles . button }
100+ text = "Enter"
101+ progress = { progress }
102+ onPress = { tryRealm }
103+ disabled = { urlFromInputValue ( realmInputValue ) === undefined }
104+ />
105+ </ Screen >
106+ ) ;
120107}
0 commit comments