vega-frontend-monorepo/libs/wallet/src/rest-connector-form.tsx

95 lines
2.4 KiB
TypeScript
Raw Normal View History

import { t } from '@vegaprotocol/react-helpers';
import { Button, FormGroup, Input, InputError } from '@vegaprotocol/ui-toolkit';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import type { RestConnector } from '.';
interface FormFields {
wallet: string;
passphrase: string;
}
interface RestConnectorFormProps {
connector: RestConnector;
onAuthenticate: () => void;
}
export function RestConnectorForm({
connector,
onAuthenticate,
}: RestConnectorFormProps) {
const [error, setError] = useState('');
const {
register,
handleSubmit,
formState: { errors },
} = useForm<FormFields>();
async function onSubmit(fields: FormFields) {
try {
setError('');
const res = await connector.authenticate({
wallet: fields.wallet,
passphrase: fields.passphrase,
});
if (res.success) {
onAuthenticate();
} else {
throw res.error;
}
} catch (err) {
if (err instanceof TypeError) {
setError(t('Wallet not running at http://localhost:1789'));
} else if (err instanceof Error) {
setError(t('Authentication failed'));
} else {
setError(t('Something went wrong'));
}
}
}
return (
<form onSubmit={handleSubmit(onSubmit)} data-testid="rest-connector-form">
<FormGroup label={t('Wallet')} labelFor="wallet">
<Input
{...register('wallet', { required: t('Required') })}
id="wallet"
type="text"
autoFocus={true}
/>
{errors.wallet?.message && (
Test/deal ticket tests (#161) * scaffold dealticket package, remove trading views from react-helpers * add deal ticket component, add intent utils, expand dialog and form group styles * add splash component, show market not found message if market doesnt exist * tidy up error handling * add handleError method for vega tx hook * add better testname for provider test, flesh out tests a bit more for deal ticket * Add unit tests for useVegaTransaction and useOrderSubmit hooks * add wrapper component for order dialog styles * add vega styled loader to ui toolkit and use in order dialog * add title prop to order dialog * split limit and market tickets into own files * add button radio component * revert dialog styles * move splash component to ui-toolkit, add story * convert intent to enum * Make button always type=button unless type prop is passed * inline filter logic for tif selector * add date-fns, add datetime to helpers * add order types to wallet package, make price undefined if order type is market * use enums in deal ticket logic * tidy up order state by moving submit and transaction hooks out of deal ticket * add comment for dialog styles * remove decimal from price input * add types package, delete old generated types from trading project * rename types package to graphql * update generate command to point to correct locations * fix use order submit test * BDD and navigation tests passing * Remove commented steps * Steps up to placing order * Date picker and date-fns update * Vega connector wallet tests * Passing up to request sent, updated date picker * Tests for sell orders and errors * Update market feature * Fix failing tests * Update wallet login * Readded tx hash assertion and remaining tests * Add CI wallet import * Update .github/workflows/cypress.yml Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * Resolved PR comments * Fix yaml error * Attempt to fix failing tests in CI * Run Cypress in Chrome * Add reload if public key error displayed * Fix wallet name * Add force click and waits * Increase timeout for deal ticket page * Removed network list from yaml and using input error id * Increase timeout to 8 seconds * Re add deleted test id Co-authored-by: Matthew Russell <mattrussell36@gmail.com> Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>
2022-04-04 15:11:27 +00:00
<InputError
data-testid="input-wallet-error"
intent="danger"
className="mt-4"
>
{errors.wallet.message}
</InputError>
)}
</FormGroup>
<FormGroup label={t('Passphrase')} labelFor="passphrase">
<Input
{...register('passphrase', { required: t('Required') })}
id="passphrase"
type="password"
/>
{errors.passphrase?.message && (
<InputError intent="danger" className="mt-4">
{errors.passphrase.message}
</InputError>
)}
</FormGroup>
{error && (
<p className="text-intent-danger mb-12" data-testid="form-error">
{error}
</p>
)}
<Button variant="primary" type="submit">
{t('Connect')}
</Button>
</form>
);
}