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,92 +21,79 @@ 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-
30- export default class RealmInputScreen extends PureComponent < Props , State > {
31- state : State = {
32- progress : false ,
33- realmInputValue : '' ,
34- error : null ,
35- } ;
36-
37- tryRealm : ( ) = > Promise < void > = async ( ) => {
38- const { realmInputValue } = this . state ;
24+ export default function RealmInputScreen ( props : Props ) : Node {
25+ const [ progress , setProgress ] = useState < boolean > ( false ) ;
26+ const [ realmInputValue , setRealmInputValue ] = useState < string > ( '' ) ;
27+ const [ error , setError ] = useState < string | null > ( null ) ;
3928
29+ const tryRealm = useCallback ( async ( ) => {
4030 const parsedRealm = tryParseUrl ( realmInputValue ) ;
4131 if ( ! parsedRealm ) {
42- this . setState ( { error : 'Please enter a valid URL' } ) ;
32+ setError ( 'Please enter a valid URL' ) ;
4333 return ;
4434 }
4535 if ( parsedRealm . username !== '' ) {
46- this . setState ( { error : 'Please enter the server URL, not your email' } ) ;
36+ setError ( 'Please enter the server URL, not your email' ) ;
4737 return ;
4838 }
4939
50- this . setState ( {
51- progress : true ,
52- error : null ,
53- } ) ;
40+ setProgress ( true ) ;
41+ setError ( null ) ;
5442 try {
5543 const serverSettings : ApiResponseServerSettings = await api . getServerSettings ( parsedRealm ) ;
5644 NavigationService . dispatch ( navigateToAuth ( serverSettings ) ) ;
5745 Keyboard . dismiss ( ) ;
5846 } catch ( err ) {
59- this . setState ( { error : 'Cannot connect to server' } ) ;
47+ setError ( 'Cannot connect to server' ) ;
6048 /* eslint-disable no-console */
6149 console . warn ( 'RealmInputScreen: failed to connect to server:' , err ) ;
6250 console . warn ( err . stack ) ;
6351 } finally {
64- this . setState ( { progress : false } ) ;
52+ setProgress ( false ) ;
6553 }
66- } ;
54+ } , [ realmInputValue ] ) ;
6755
68- handleRealmChange : string => void = value => this . setState ( { realmInputValue : value } ) ;
56+ const handleRealmChange = useCallback ( value => {
57+ setRealmInputValue ( value ) ;
58+ } , [ ] ) ;
6959
70- render ( ) : Node {
71- const { navigation } = this . props ;
72- const { progress , error , realmInputValue } = this . state ;
60+ const { navigation } = props ;
7361
74- const styles = {
75- input : { marginTop : 16 , marginBottom : 8 } ,
76- hintText : { paddingLeft : 2 , fontSize : 12 } ,
77- button : { marginTop : 8 } ,
78- } ;
62+ const styles = {
63+ input : { marginTop : 16 , marginBottom : 8 } ,
64+ hintText : { paddingLeft : 2 , fontSize : 12 } ,
65+ button : { marginTop : 8 } ,
66+ } ;
7967
80- return (
81- < Screen
82- title = "Welcome"
83- canGoBack = { ! this . props . route . params . initial }
84- padding
85- centerContent
86- keyboardShouldPersistTaps = "always"
87- shouldShowLoadingBanner = { false }
88- >
89- < ZulipTextIntl text = "Enter your Zulip server URL:" />
90- < SmartUrlInput
91- style = { styles . input }
92- navigation = { navigation }
93- onChangeText = { this . handleRealmChange }
94- onSubmitEditing = { this . tryRealm }
95- enablesReturnKeyAutomatically
96- />
97- { error !== null ? (
98- < ErrorMsg error = { error } />
99- ) : (
100- < ZulipTextIntl text = "e.g. zulip.example.com" style = { styles . hintText } />
101- ) }
102- < ZulipButton
103- style = { styles . button }
104- text = "Enter"
105- progress = { progress }
106- onPress = { this . tryRealm }
107- disabled = { tryParseUrl ( realmInputValue ) === undefined }
108- />
109- </ Screen >
110- ) ;
111- }
68+ return (
69+ < Screen
70+ title = "Welcome"
71+ canGoBack = { ! props . route . params . initial }
72+ padding
73+ centerContent
74+ keyboardShouldPersistTaps = "always"
75+ shouldShowLoadingBanner = { false }
76+ >
77+ < ZulipTextIntl text = "Enter your Zulip server URL:" />
78+ < SmartUrlInput
79+ style = { styles . input }
80+ navigation = { navigation }
81+ onChangeText = { handleRealmChange }
82+ onSubmitEditing = { tryRealm }
83+ enablesReturnKeyAutomatically
84+ />
85+ { error !== null ? (
86+ < ErrorMsg error = { error } />
87+ ) : (
88+ < ZulipTextIntl text = "e.g. zulip.example.com" style = { styles . hintText } />
89+ ) }
90+ < ZulipButton
91+ style = { styles . button }
92+ text = "Enter"
93+ progress = { progress }
94+ onPress = { tryRealm }
95+ disabled = { tryParseUrl ( realmInputValue ) === undefined }
96+ />
97+ </ Screen >
98+ ) ;
11299}
0 commit comments