diff --git a/lib/shared/keyserver-utils.js b/lib/shared/keyserver-utils.js new file mode 100644 --- /dev/null +++ b/lib/shared/keyserver-utils.js @@ -0,0 +1,41 @@ +// @flow + +import * as React from 'react'; + +import { + getVersion, + getVersionActionTypes, +} from '../actions/device-actions.js'; +import { + useServerCall, + useDispatchActionPromise, +} from '../utils/action-utils.js'; + +function useIsKeyserverURLValid(keyserverURL: string): () => Promise { + const serverCallParamOverride = React.useMemo( + () => ({ + urlPrefix: keyserverURL, + }), + [keyserverURL], + ); + + console.log(serverCallParamOverride); + + const getVersionCall = useServerCall(getVersion, serverCallParamOverride); + const dispatchActionPromise = useDispatchActionPromise(); + + return React.useCallback(async () => { + const getVersionPromise = getVersionCall(); + dispatchActionPromise(getVersionActionTypes, getVersionPromise); + + // We don't care about the result; just need to make sure this doesn't throw + try { + await getVersionPromise; + return true; + } catch (e) { + return false; + } + }, [dispatchActionPromise, getVersionCall]); +} + +export { useIsKeyserverURLValid }; diff --git a/native/account/registration/keyserver-selection.react.js b/native/account/registration/keyserver-selection.react.js --- a/native/account/registration/keyserver-selection.react.js +++ b/native/account/registration/keyserver-selection.react.js @@ -4,15 +4,9 @@ import * as React from 'react'; import { Text, View } from 'react-native'; -import { - getVersion, - getVersionActionTypes, -} from 'lib/actions/device-actions.js'; +import { getVersionActionTypes } from 'lib/actions/device-actions.js'; import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors.js'; -import { - useServerCall, - useDispatchActionPromise, -} from 'lib/utils/action-utils.js'; +import { useIsKeyserverURLValid } from 'lib/shared/keyserver-utils.js'; import RegistrationButtonContainer from './registration-button-container.react.js'; import RegistrationButton from './registration-button.react.js'; @@ -97,7 +91,7 @@ setError(undefined); }, []); - let keyserverURL; + let keyserverURL = ''; if (currentSelection === 'ashoat') { keyserverURL = defaultURLPrefix; } else if (currentSelection === 'custom' && customKeyserver) { @@ -110,30 +104,18 @@ buttonState = 'loading'; } - const serverCallParamOverride = React.useMemo( - () => ({ - urlPrefix: keyserverURL, - }), - [keyserverURL], - ); - const getVersionCall = useServerCall(getVersion, serverCallParamOverride); + const isKeyserverURLValidPromiseCallback = + useIsKeyserverURLValid(keyserverURL); - const dispatchActionPromise = useDispatchActionPromise(); const { navigate } = props.navigation; const { coolOrNerdMode } = props.route.params.userSelections; + const onSubmit = React.useCallback(async () => { setError(undefined); - if (!keyserverURL) { - return; - } - const getVersionPromise = getVersionCall(); - dispatchActionPromise(getVersionActionTypes, getVersionPromise); + const isKeyserverURLValid = await isKeyserverURLValidPromiseCallback(); - // We don't care about the result; just need to make sure this doesn't throw - try { - await getVersionPromise; - } catch { + if (!isKeyserverURLValid) { setError('cant_reach_keyserver'); return; } @@ -147,12 +129,11 @@ params: { userSelections: { coolOrNerdMode, keyserverURL } }, }); }, [ - navigate, - coolOrNerdMode, keyserverURL, + isKeyserverURLValidPromiseCallback, setCachedSelections, - dispatchActionPromise, - getVersionCall, + navigate, + coolOrNerdMode, ]); const styles = useStyles(unboundStyles);