2020-03-23 01:25:29 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-03-23 01:15:38 +00:00
|
|
|
import ContentBox from '@/components/elements/ContentBox';
|
|
|
|
import CreateApiKeyForm from '@/components/dashboard/forms/CreateApiKeyForm';
|
2020-03-23 01:25:29 +00:00
|
|
|
import getApiKeys, { ApiKey } from '@/api/account/getApiKeys';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2020-07-05 01:46:09 +00:00
|
|
|
import { faKey, faTrashAlt } from '@fortawesome/free-solid-svg-icons';
|
2020-03-23 02:10:49 +00:00
|
|
|
import ConfirmationModal from '@/components/elements/ConfirmationModal';
|
|
|
|
import deleteApiKey from '@/api/account/deleteApiKey';
|
2020-09-08 03:26:18 +00:00
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
2020-03-23 02:10:49 +00:00
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2020-07-05 01:46:09 +00:00
|
|
|
import { format } from 'date-fns';
|
2020-04-17 18:17:01 +00:00
|
|
|
import PageContentBlock from '@/components/elements/PageContentBlock';
|
2020-07-04 16:28:03 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import GreyRowBox from '@/components/elements/GreyRowBox';
|
2020-03-23 01:15:38 +00:00
|
|
|
|
|
|
|
export default () => {
|
2020-03-23 02:10:49 +00:00
|
|
|
const [ deleteIdentifier, setDeleteIdentifier ] = useState('');
|
2020-03-23 01:25:29 +00:00
|
|
|
const [ keys, setKeys ] = useState<ApiKey[]>([]);
|
|
|
|
const [ loading, setLoading ] = useState(true);
|
2020-03-23 02:10:49 +00:00
|
|
|
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2020-03-23 01:25:29 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-03-23 02:10:49 +00:00
|
|
|
clearFlashes('account');
|
2020-03-23 01:25:29 +00:00
|
|
|
getApiKeys()
|
|
|
|
.then(keys => setKeys(keys))
|
|
|
|
.then(() => setLoading(false))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2020-03-23 02:10:49 +00:00
|
|
|
addError({ key: 'account', message: httpErrorToHuman(error) });
|
2020-03-23 01:25:29 +00:00
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2020-03-23 02:10:49 +00:00
|
|
|
const doDeletion = (identifier: string) => {
|
|
|
|
setLoading(true);
|
|
|
|
clearFlashes('account');
|
|
|
|
deleteApiKey(identifier)
|
|
|
|
.then(() => setKeys(s => ([
|
|
|
|
...(s || []).filter(key => key.identifier !== identifier),
|
|
|
|
])))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
addError({ key: 'account', message: httpErrorToHuman(error) });
|
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
};
|
|
|
|
|
2020-03-23 01:15:38 +00:00
|
|
|
return (
|
2020-09-08 03:26:18 +00:00
|
|
|
<PageContentBlock title={'Account API'}>
|
2020-07-04 16:28:03 +00:00
|
|
|
<FlashMessageRender byKey={'account'} css={tw`mb-4`}/>
|
|
|
|
<div css={tw`flex`}>
|
|
|
|
<ContentBox title={'Create API Key'} css={tw`flex-1`}>
|
2020-03-28 23:06:36 +00:00
|
|
|
<CreateApiKeyForm onKeyCreated={key => setKeys(s => ([ ...s!, key ]))}/>
|
|
|
|
</ContentBox>
|
2020-07-04 16:28:03 +00:00
|
|
|
<ContentBox title={'API Keys'} css={tw`ml-10 flex-1`}>
|
2020-03-28 23:06:36 +00:00
|
|
|
<SpinnerOverlay visible={loading}/>
|
|
|
|
<ConfirmationModal
|
2020-08-18 04:35:11 +00:00
|
|
|
visible={!!deleteIdentifier}
|
2020-03-28 23:06:36 +00:00
|
|
|
title={'Confirm key deletion'}
|
|
|
|
buttonText={'Yes, delete key'}
|
|
|
|
onConfirmed={() => {
|
|
|
|
doDeletion(deleteIdentifier);
|
|
|
|
setDeleteIdentifier('');
|
|
|
|
}}
|
2020-08-18 04:35:11 +00:00
|
|
|
onModalDismissed={() => setDeleteIdentifier('')}
|
2020-03-28 23:06:36 +00:00
|
|
|
>
|
|
|
|
Are you sure you wish to delete this API key? All requests using it will immediately be
|
|
|
|
invalidated and will fail.
|
|
|
|
</ConfirmationModal>
|
|
|
|
{
|
|
|
|
keys.length === 0 ?
|
2020-07-04 16:28:03 +00:00
|
|
|
<p css={tw`text-center text-sm`}>
|
2020-03-28 23:06:36 +00:00
|
|
|
{loading ? 'Loading...' : 'No API keys exist for this account.'}
|
|
|
|
</p>
|
|
|
|
:
|
2020-07-04 16:28:03 +00:00
|
|
|
keys.map((key, index) => (
|
|
|
|
<GreyRowBox
|
2020-03-28 23:06:36 +00:00
|
|
|
key={key.identifier}
|
2020-07-04 16:28:03 +00:00
|
|
|
css={[ tw`bg-neutral-600 flex items-center`, index > 0 && tw`mt-2` ]}
|
2020-03-28 23:06:36 +00:00
|
|
|
>
|
2020-07-04 16:28:03 +00:00
|
|
|
<FontAwesomeIcon icon={faKey} css={tw`text-neutral-300`}/>
|
|
|
|
<div css={tw`ml-4 flex-1`}>
|
|
|
|
<p css={tw`text-sm`}>{key.description}</p>
|
|
|
|
<p css={tw`text-2xs text-neutral-300 uppercase`}>
|
|
|
|
Last used:
|
|
|
|
{key.lastUsedAt ? format(key.lastUsedAt, 'MMM do, yyyy HH:mm') : 'Never'}
|
2020-03-28 23:06:36 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
2020-07-04 16:28:03 +00:00
|
|
|
<p css={tw`text-sm ml-4`}>
|
|
|
|
<code css={tw`font-mono py-1 px-2 bg-neutral-900 rounded`}>
|
2020-03-28 23:06:36 +00:00
|
|
|
{key.identifier}
|
|
|
|
</code>
|
2020-03-23 02:10:49 +00:00
|
|
|
</p>
|
2020-03-28 23:06:36 +00:00
|
|
|
<button
|
2020-07-04 16:28:03 +00:00
|
|
|
css={tw`ml-4 p-2 text-sm`}
|
2020-03-28 23:06:36 +00:00
|
|
|
onClick={() => setDeleteIdentifier(key.identifier)}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faTrashAlt}
|
2020-07-04 16:28:03 +00:00
|
|
|
css={tw`text-neutral-400 hover:text-red-400 transition-colors duration-150`}
|
2020-03-28 23:06:36 +00:00
|
|
|
/>
|
|
|
|
</button>
|
2020-07-04 16:28:03 +00:00
|
|
|
</GreyRowBox>
|
2020-03-28 23:06:36 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
</ContentBox>
|
|
|
|
</div>
|
2020-04-17 18:17:01 +00:00
|
|
|
</PageContentBlock>
|
2020-03-23 01:15:38 +00:00
|
|
|
);
|
|
|
|
};
|