2020-03-23 01:15:38 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { Field, Form, Formik, FormikHelpers } from 'formik';
|
|
|
|
import { object, string } from 'yup';
|
|
|
|
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
|
|
|
|
import Modal from '@/components/elements/Modal';
|
|
|
|
import createApiKey from '@/api/account/createApiKey';
|
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
2020-03-23 02:10:49 +00:00
|
|
|
import { ApiKey } from '@/api/account/getApiKeys';
|
2020-07-04 16:28:03 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import Input, { Textarea } from '@/components/elements/Input';
|
2020-03-23 01:15:38 +00:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
description: string;
|
|
|
|
allowedIps: string;
|
|
|
|
}
|
|
|
|
|
2020-03-23 02:10:49 +00:00
|
|
|
export default ({ onKeyCreated }: { onKeyCreated: (key: ApiKey) => void }) => {
|
2020-03-23 01:15:38 +00:00
|
|
|
const [ apiKey, setApiKey ] = useState('');
|
|
|
|
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
|
|
|
|
const submit = (values: Values, { setSubmitting, resetForm }: FormikHelpers<Values>) => {
|
|
|
|
clearFlashes('account');
|
|
|
|
createApiKey(values.description, values.allowedIps)
|
2020-03-23 02:10:49 +00:00
|
|
|
.then(({ secretToken, ...key }) => {
|
2020-03-23 01:15:38 +00:00
|
|
|
resetForm();
|
|
|
|
setSubmitting(false);
|
2020-03-23 02:10:49 +00:00
|
|
|
setApiKey(`${key.identifier}${secretToken}`);
|
|
|
|
onKeyCreated(key);
|
2020-03-23 01:15:38 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
addError({ key: 'account', message: httpErrorToHuman(error) });
|
|
|
|
setSubmitting(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Modal
|
|
|
|
visible={apiKey.length > 0}
|
|
|
|
onDismissed={() => setApiKey('')}
|
|
|
|
closeOnEscape={false}
|
|
|
|
closeOnBackground={false}
|
|
|
|
>
|
2020-07-04 16:28:03 +00:00
|
|
|
<h3 css={tw`mb-6`}>Your API Key</h3>
|
|
|
|
<p css={tw`text-sm mb-6`}>
|
2020-03-23 01:15:38 +00:00
|
|
|
The API key you have requested is shown below. Please store this in a safe location, it will not be
|
|
|
|
shown again.
|
|
|
|
</p>
|
2020-07-04 16:28:03 +00:00
|
|
|
<pre css={tw`text-sm bg-neutral-900 rounded py-2 px-4 font-mono`}>
|
|
|
|
<code css={tw`font-mono`}>{apiKey}</code>
|
2020-03-23 01:15:38 +00:00
|
|
|
</pre>
|
2020-07-04 16:28:03 +00:00
|
|
|
<div css={tw`flex justify-end mt-6`}>
|
|
|
|
<Button
|
2020-03-23 01:15:38 +00:00
|
|
|
type={'button'}
|
|
|
|
onClick={() => setApiKey('')}
|
|
|
|
>
|
|
|
|
Close
|
2020-07-04 16:28:03 +00:00
|
|
|
</Button>
|
2020-03-23 01:15:38 +00:00
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{
|
|
|
|
description: '',
|
|
|
|
allowedIps: '',
|
|
|
|
}}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
allowedIps: string(),
|
|
|
|
description: string().required().min(4),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{({ isSubmitting }) => (
|
|
|
|
<Form>
|
|
|
|
<SpinnerOverlay visible={isSubmitting}/>
|
|
|
|
<FormikFieldWrapper
|
|
|
|
label={'Description'}
|
|
|
|
name={'description'}
|
|
|
|
description={'A description of this API key.'}
|
2020-07-04 16:28:03 +00:00
|
|
|
css={tw`mb-6`}
|
2020-03-23 01:15:38 +00:00
|
|
|
>
|
2020-07-04 16:28:03 +00:00
|
|
|
<Field name={'description'} as={Input}/>
|
2020-03-23 01:15:38 +00:00
|
|
|
</FormikFieldWrapper>
|
|
|
|
<FormikFieldWrapper
|
|
|
|
label={'Allowed IPs'}
|
|
|
|
name={'allowedIps'}
|
|
|
|
description={'Leave blank to allow any IP address to use this API key, otherwise provide each IP address on a new line.'}
|
|
|
|
>
|
2020-07-04 16:28:03 +00:00
|
|
|
<Field as={Textarea} name={'allowedIps'} css={tw`h-32`}/>
|
2020-03-23 01:15:38 +00:00
|
|
|
</FormikFieldWrapper>
|
2020-07-04 16:28:03 +00:00
|
|
|
<div css={tw`flex justify-end mt-6`}>
|
|
|
|
<Button>Create</Button>
|
2020-03-23 01:15:38 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|