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 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';
|
2021-07-29 04:13:49 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-07-04 16:28:03 +00:00
|
|
|
import Button from '@/components/elements/Button';
|
2021-07-29 04:13:49 +00:00
|
|
|
import Input from '@/components/elements/Input';
|
2020-08-18 04:35:11 +00:00
|
|
|
import ApiKeyModal from '@/components/dashboard/ApiKeyModal';
|
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);
|
2021-07-29 03:53:54 +00:00
|
|
|
setApiKey(secretToken);
|
2020-03-23 02:10:49 +00:00
|
|
|
onKeyCreated(key);
|
2020-03-23 01:15:38 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
addError({ key: 'account', message: httpErrorToHuman(error) });
|
|
|
|
setSubmitting(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2020-08-18 04:35:11 +00:00
|
|
|
<ApiKeyModal
|
2020-03-23 01:15:38 +00:00
|
|
|
visible={apiKey.length > 0}
|
2020-08-18 04:35:11 +00:00
|
|
|
onModalDismissed={() => setApiKey('')}
|
|
|
|
apiKey={apiKey}
|
|
|
|
/>
|
2020-03-23 01:15:38 +00:00
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
2020-08-18 02:48:51 +00:00
|
|
|
initialValues={{ description: '', allowedIps: '' }}
|
2020-03-23 01:15:38 +00:00
|
|
|
validationSchema={object().shape({
|
|
|
|
description: string().required().min(4),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{({ isSubmitting }) => (
|
|
|
|
<Form>
|
|
|
|
<SpinnerOverlay visible={isSubmitting}/>
|
|
|
|
<FormikFieldWrapper
|
|
|
|
label={'Description'}
|
|
|
|
name={'description'}
|
2021-07-29 04:13:49 +00:00
|
|
|
description={'This API key will be able to act on your behalf against this Panel\'s API.'}
|
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>
|
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>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|