2022-05-14 21:31:53 +00:00
|
|
|
import { Field, Form, Formik, FormikHelpers } from 'formik';
|
|
|
|
import { object, string } from 'yup';
|
|
|
|
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import Input, { Textarea } from '@/components/elements/Input';
|
2022-11-25 20:25:03 +00:00
|
|
|
import styled from 'styled-components';
|
2022-05-14 21:31:53 +00:00
|
|
|
import { useFlashKey } from '@/plugins/useFlash';
|
|
|
|
import { createSSHKey, useSSHKeys } from '@/api/account/ssh-keys';
|
|
|
|
|
|
|
|
interface Values {
|
|
|
|
name: string;
|
|
|
|
publicKey: string;
|
|
|
|
}
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const CustomTextarea = styled(Textarea)`
|
|
|
|
${tw`h-32`}
|
|
|
|
`;
|
2022-05-14 21:31:53 +00:00
|
|
|
|
|
|
|
export default () => {
|
|
|
|
const { clearAndAddHttpError } = useFlashKey('account');
|
|
|
|
const { mutate } = useSSHKeys();
|
|
|
|
|
|
|
|
const submit = (values: Values, { setSubmitting, resetForm }: FormikHelpers<Values>) => {
|
|
|
|
clearAndAddHttpError();
|
|
|
|
|
|
|
|
createSSHKey(values.name, values.publicKey)
|
2022-11-25 20:25:03 +00:00
|
|
|
.then(key => {
|
2022-05-14 21:31:53 +00:00
|
|
|
resetForm();
|
2022-11-25 20:25:03 +00:00
|
|
|
mutate(data => (data || []).concat(key));
|
2022-05-14 21:31:53 +00:00
|
|
|
})
|
2022-11-25 20:25:03 +00:00
|
|
|
.catch(error => clearAndAddHttpError(error))
|
2022-05-14 21:31:53 +00:00
|
|
|
.then(() => setSubmitting(false));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{ name: '', publicKey: '' }}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
name: string().required(),
|
|
|
|
publicKey: string().required(),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{({ isSubmitting }) => (
|
|
|
|
<Form>
|
2022-06-26 19:13:52 +00:00
|
|
|
<SpinnerOverlay visible={isSubmitting} />
|
2022-05-14 21:31:53 +00:00
|
|
|
<FormikFieldWrapper label={'SSH Key Name'} name={'name'} css={tw`mb-6`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Field name={'name'} as={Input} />
|
2022-05-14 21:31:53 +00:00
|
|
|
</FormikFieldWrapper>
|
|
|
|
<FormikFieldWrapper
|
|
|
|
label={'Public Key'}
|
|
|
|
name={'publicKey'}
|
|
|
|
description={'Enter your public SSH key.'}
|
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Field name={'publicKey'} as={CustomTextarea} />
|
2022-05-14 21:31:53 +00:00
|
|
|
</FormikFieldWrapper>
|
|
|
|
<div css={tw`flex justify-end mt-6`}>
|
|
|
|
<Button>Save</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|