diff --git a/web/modals/keyserver-selection/add-keyserver-modal.react.js b/web/modals/keyserver-selection/add-keyserver-modal.react.js index d507e679b..4307b37f7 100644 --- a/web/modals/keyserver-selection/add-keyserver-modal.react.js +++ b/web/modals/keyserver-selection/add-keyserver-modal.react.js @@ -1,131 +1,143 @@ // @flow import * as React from 'react'; import { addKeyserverActionType } from 'lib/actions/keyserver-actions.js'; import { useModalContext } from 'lib/components/modal-provider.react.js'; import { useIsKeyserverURLValid } from 'lib/shared/keyserver-utils.js'; import type { KeyserverInfo } from 'lib/types/keyserver-types.js'; import { defaultKeyserverInfo } from 'lib/types/keyserver-types.js'; import { useDispatch } from 'lib/utils/redux-utils.js'; import css from './add-keyserver-modal.css'; import Button, { buttonThemes } from '../../components/button.react.js'; import { useSelector } from '../../redux/redux-utils.js'; import { useStaffCanSee } from '../../utils/staff-utils.js'; import Input from '../input.react.js'; import Modal from '../modal.react.js'; +type KeyserverCheckStatus = + | { +status: 'inactive' } + | { +status: 'loading' } + | { +status: 'error' }; +const keyserverCheckStatusInactive = { status: 'inactive' }; +const keyserverCheckStatusLoading = { status: 'loading' }; +const keyserverCheckStatusError = { status: 'error' }; + function AddKeyserverModal(): React.Node { const { popModal } = useModalContext(); const dispatch = useDispatch(); const staffCanSee = useStaffCanSee(); const currentUserID = useSelector(state => state.currentUserInfo?.id); const customServer = useSelector(state => state.customServer); const [keyserverURL, setKeyserverURL] = React.useState( customServer && staffCanSee ? customServer : '', ); - const [showErrorMessage, setShowErrorMessage] = - React.useState(false); + const [status, setStatus] = React.useState( + keyserverCheckStatusInactive, + ); const onChangeKeyserverURL = React.useCallback( (event: SyntheticEvent) => setKeyserverURL(event.currentTarget.value), [], ); const isKeyserverURLValidCallback = useIsKeyserverURLValid(keyserverURL); const onClickAddKeyserver = React.useCallback(async () => { - setShowErrorMessage(false); if (!currentUserID || !keyserverURL) { return; } + setStatus(keyserverCheckStatusLoading); const keyserverVersionData = await isKeyserverURLValidCallback(); if (!keyserverVersionData) { - setShowErrorMessage(true); + setStatus(keyserverCheckStatusError); return; } + setStatus(keyserverCheckStatusInactive); const newKeyserverInfo: KeyserverInfo = defaultKeyserverInfo(keyserverURL); dispatch({ type: addKeyserverActionType, payload: { keyserverAdminUserID: keyserverVersionData.ownerID, newKeyserverInfo, }, }); popModal(); }, [ currentUserID, dispatch, keyserverURL, popModal, isKeyserverURLValidCallback, ]); + const showErrorMessage = status.status === 'error'; const errorMessage = React.useMemo(() => { let errorText; if (showErrorMessage) { errorText = 'Cannot connect to keyserver. Please check the URL or your ' + 'connection and try again.'; } return
{errorText}
; }, [showErrorMessage]); + const buttonDisabled = !keyserverURL || status.status === 'loading'; const addKeyserverButton = React.useMemo( () => ( ), - [keyserverURL, onClickAddKeyserver], + [buttonDisabled, onClickAddKeyserver], ); const addKeyserverModal = React.useMemo( () => (
Keyserver URL
{errorMessage}
), [ addKeyserverButton, errorMessage, keyserverURL, onChangeKeyserverURL, popModal, ], ); return addKeyserverModal; } export default AddKeyserverModal;