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,48 @@ +// @flow + +import * as React from 'react'; + +import { + useGetVersion, + getVersionActionTypes, +} from '../actions/device-actions.js'; +import { useDispatchActionPromise } from '../utils/action-utils.js'; + +function useIsKeyserverURLValid(keyserverURL?: string): () => Promise { + const serverCallParamOverride = React.useMemo(() => { + if (!keyserverURL) { + return undefined; + } + + return { + keyserverInfos: { + [keyserverURL]: { + urlPrefix: keyserverURL, + }, + }, + }; + }, [keyserverURL]); + + const getVersionCall = useGetVersion(serverCallParamOverride); + + const dispatchActionPromise = useDispatchActionPromise(); + + return React.useCallback(async () => { + if (!keyserverURL) { + return false; + } + + 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, keyserverURL]); +} + +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,12 +4,9 @@ import * as React from 'react'; import { Text, View } from 'react-native'; -import { - useGetVersion, - getVersionActionTypes, -} from 'lib/actions/device-actions.js'; +import { getVersionActionTypes } from 'lib/actions/device-actions.js'; import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors.js'; -import { 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'; @@ -107,34 +104,18 @@ buttonState = 'loading'; } - const serverCallParamOverride = React.useMemo( - () => ({ - keyserverInfos: { - [keyserverURL]: { - urlPrefix: keyserverURL, - }, - }, - }), - [keyserverURL], - ); - const getVersionCall = useGetVersion(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; } @@ -148,12 +129,11 @@ params: { userSelections: { coolOrNerdMode, keyserverURL } }, }); }, [ - navigate, - coolOrNerdMode, keyserverURL, + isKeyserverURLValidPromiseCallback, setCachedSelections, - dispatchActionPromise, - getVersionCall, + navigate, + coolOrNerdMode, ]); const styles = useStyles(unboundStyles);