import {
Button,
FormInputValidation,
FormLabel,
Gridicon,
SegmentedControl,
} from '@automattic/components';
import { useTranslate } from 'i18n-calypso';
import { FunctionComponent, useState, FormEventHandler } from 'react';
import FormInputCheckbox from 'calypso/components/forms/form-checkbox';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormPasswordInput from 'calypso/components/forms/form-password-input';
import FormSelect from 'calypso/components/forms/form-select';
import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation';
import FormTextInput from 'calypso/components/forms/form-text-input';
import FormTextArea from 'calypso/components/forms/form-textarea';
import InfoPopover from 'calypso/components/info-popover';
import { useDispatch } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { FormState, FormMode, FormErrors, INITIAL_FORM_INTERACTION } from '../form';
import { getHostInfoFromId } from '../host-info';
import InlineInfo from './inline-info';
import './style.scss';
interface Props {
formErrors: FormErrors;
formMode: FormMode;
formModeSwitcher?: 'segmented' | 'simple';
disabled?: boolean;
formState: FormState;
onFormStateChange: ( newFormState: FormState ) => void;
onModeChange: ( fromMode: FormMode ) => void;
host: string;
role: string;
withHeader?: boolean;
children?: React.ReactNode;
allowFtp?: boolean;
}
const ServerCredentialsForm: FunctionComponent< Props > = ( {
children,
disabled = false,
formState,
formErrors,
formMode,
formModeSwitcher = 'segmented',
onFormStateChange,
onModeChange,
host,
role = 'main',
withHeader = true,
allowFtp = true,
} ) => {
const translate = useTranslate();
const dispatch = useDispatch();
const [ interactions, setFormInteractions ] = useState( INITIAL_FORM_INTERACTION );
const hostInfo = getHostInfoFromId( host );
const isAlternate = role === 'alternate';
formState.role = role;
const handleFormChange: FormEventHandler< HTMLInputElement | HTMLSelectElement > = ( {
currentTarget,
} ) => {
switch ( currentTarget.name ) {
case 'protocol':
onFormStateChange( {
...formState,
protocol: currentTarget.value as 'ftp' | 'ssh' | 'dynamic-ssh',
port: ( () => {
let port = parseInt( String( formState.port ) ) || 22;
if ( formState.port === 22 && currentTarget.value === 'ftp' ) {
port = 21;
} else if ( formState.port === 21 && currentTarget.value === 'ssh' ) {
port = 22;
}
return port;
} )(),
} );
onModeChange( FormMode.Password );
break;
case 'host':
setFormInteractions( { ...interactions, host: true } );
onFormStateChange( { ...formState, host: currentTarget.value } );
break;
case 'site_url':
setFormInteractions( { ...interactions, site_url: true } );
onFormStateChange( { ...formState, site_url: currentTarget.value } );
break;
case 'port':
setFormInteractions( { ...interactions, port: true } );
onFormStateChange( {
...formState,
port: isNaN( parseInt( currentTarget.value ) ) ? '' : parseInt( currentTarget.value ),
} );
break;
case 'user':
setFormInteractions( { ...interactions, user: true } );
onFormStateChange( { ...formState, user: currentTarget.value } );
break;
case 'pass':
setFormInteractions( { ...interactions, pass: true } );
onFormStateChange( { ...formState, pass: currentTarget.value } );
break;
case 'path':
setFormInteractions( { ...interactions, path: true } );
onFormStateChange( { ...formState, path: currentTarget.value } );
break;
case 'kpri':
setFormInteractions( { ...interactions, kpri: true } );
onFormStateChange( { ...formState, kpri: currentTarget.value } );
break;
case 'save_as_staging':
setFormInteractions( { ...interactions, save_as_staging: true } );
onFormStateChange( {
...formState,
save_as_staging: ( currentTarget as HTMLInputElement ).checked,
} );
break;
}
};
const renderServerUsernameForm = () => (
{ getSubHeaderText() }
} { withHeader && renderCredentialLinks() } { ! allowFtp && } { allowFtp && () } { formMode === FormMode.PrivateKey && (
{ translate( 'Only non-encrypted private keys are supported.' ) }{ ' ' }
) } > ) } { isAlternate && (