2022-03-31 01:08:25 +00:00
|
|
|
import { t } from '@vegaprotocol/react-helpers';
|
2022-03-08 18:23:01 +00:00
|
|
|
import { Button, FormGroup, Input, InputError } from '@vegaprotocol/ui-toolkit';
|
2022-03-08 18:57:07 +00:00
|
|
|
import { useState } from 'react';
|
2022-02-22 06:40:55 +00:00
|
|
|
import { useForm } from 'react-hook-form';
|
2022-03-30 09:49:48 +00:00
|
|
|
import type { RestConnector } from '.';
|
2022-02-22 06:40:55 +00:00
|
|
|
|
|
|
|
interface FormFields {
|
|
|
|
wallet: string;
|
|
|
|
passphrase: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RestConnectorFormProps {
|
2022-02-22 23:06:35 +00:00
|
|
|
connector: RestConnector;
|
|
|
|
onAuthenticate: () => void;
|
2022-02-22 06:40:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 23:06:35 +00:00
|
|
|
export function RestConnectorForm({
|
|
|
|
connector,
|
|
|
|
onAuthenticate,
|
|
|
|
}: RestConnectorFormProps) {
|
2022-03-08 18:23:01 +00:00
|
|
|
const [error, setError] = useState('');
|
2022-02-28 22:58:34 +00:00
|
|
|
|
2022-02-22 23:06:35 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
formState: { errors },
|
2022-02-28 22:58:34 +00:00
|
|
|
} = useForm<FormFields>();
|
2022-02-22 06:40:55 +00:00
|
|
|
|
|
|
|
async function onSubmit(fields: FormFields) {
|
|
|
|
try {
|
2022-03-08 18:23:01 +00:00
|
|
|
setError('');
|
|
|
|
const res = await connector.authenticate({
|
2022-02-22 06:40:55 +00:00
|
|
|
wallet: fields.wallet,
|
|
|
|
passphrase: fields.passphrase,
|
|
|
|
});
|
|
|
|
|
2022-03-08 18:23:01 +00:00
|
|
|
if (res.success) {
|
2022-02-22 23:06:35 +00:00
|
|
|
onAuthenticate();
|
2022-02-22 06:40:55 +00:00
|
|
|
} else {
|
2022-03-08 18:23:01 +00:00
|
|
|
throw res.error;
|
2022-02-22 06:40:55 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2022-03-08 18:23:01 +00:00
|
|
|
if (err instanceof TypeError) {
|
2022-03-31 01:08:25 +00:00
|
|
|
setError(t('Wallet not running at http://localhost:1789'));
|
2022-03-08 18:23:01 +00:00
|
|
|
} else if (err instanceof Error) {
|
2022-03-31 01:08:25 +00:00
|
|
|
setError(t('Authentication failed'));
|
2022-02-28 22:58:34 +00:00
|
|
|
} else {
|
2022-03-31 01:08:25 +00:00
|
|
|
setError(t('Something went wrong'));
|
2022-02-28 22:58:34 +00:00
|
|
|
}
|
2022-02-22 06:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-03-10 23:42:55 +00:00
|
|
|
<form onSubmit={handleSubmit(onSubmit)} data-testid="rest-connector-form">
|
2022-03-31 01:08:25 +00:00
|
|
|
<FormGroup label={t('Wallet')} labelFor="wallet">
|
2022-03-08 18:13:52 +00:00
|
|
|
<Input
|
2022-03-31 01:08:25 +00:00
|
|
|
{...register('wallet', { required: t('Required') })}
|
2022-03-08 18:23:01 +00:00
|
|
|
id="wallet"
|
2022-02-22 06:40:55 +00:00
|
|
|
type="text"
|
2022-03-08 18:23:01 +00:00
|
|
|
autoFocus={true}
|
2022-02-22 06:40:55 +00:00
|
|
|
/>
|
2022-02-28 22:58:34 +00:00
|
|
|
{errors.wallet?.message && (
|
2022-06-10 19:00:02 +00:00
|
|
|
<InputError intent="danger" className="mt-4">
|
2022-03-10 23:42:55 +00:00
|
|
|
{errors.wallet.message}
|
|
|
|
</InputError>
|
2022-02-28 22:58:34 +00:00
|
|
|
)}
|
2022-03-08 18:23:01 +00:00
|
|
|
</FormGroup>
|
2022-03-31 01:08:25 +00:00
|
|
|
<FormGroup label={t('Passphrase')} labelFor="passphrase">
|
2022-03-08 18:13:52 +00:00
|
|
|
<Input
|
2022-03-31 01:08:25 +00:00
|
|
|
{...register('passphrase', { required: t('Required') })}
|
2022-03-08 18:23:01 +00:00
|
|
|
id="passphrase"
|
2022-02-28 22:58:34 +00:00
|
|
|
type="password"
|
2022-02-22 06:40:55 +00:00
|
|
|
/>
|
2022-02-28 22:58:34 +00:00
|
|
|
{errors.passphrase?.message && (
|
2022-03-10 23:42:55 +00:00
|
|
|
<InputError intent="danger" className="mt-4">
|
|
|
|
{errors.passphrase.message}
|
|
|
|
</InputError>
|
2022-02-28 22:58:34 +00:00
|
|
|
)}
|
2022-03-08 18:23:01 +00:00
|
|
|
</FormGroup>
|
2022-03-10 23:42:55 +00:00
|
|
|
{error && (
|
2022-06-10 15:07:44 +00:00
|
|
|
<p className="text-danger mb-12" data-testid="form-error">
|
2022-03-10 23:42:55 +00:00
|
|
|
{error}
|
|
|
|
</p>
|
|
|
|
)}
|
2022-03-08 18:13:52 +00:00
|
|
|
<Button variant="primary" type="submit">
|
2022-03-31 01:08:25 +00:00
|
|
|
{t('Connect')}
|
2022-03-08 18:13:52 +00:00
|
|
|
</Button>
|
2022-02-22 06:40:55 +00:00
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|