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';
|
|
|
|
import { faKey } from '@fortawesome/free-solid-svg-icons/faKey';
|
|
|
|
import { faTrashAlt } from '@fortawesome/free-solid-svg-icons/faTrashAlt';
|
2020-03-23 02:10:49 +00:00
|
|
|
import ConfirmationModal from '@/components/elements/ConfirmationModal';
|
|
|
|
import deleteApiKey from '@/api/account/deleteApiKey';
|
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
import format from 'date-fns/format';
|
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 (
|
|
|
|
<div className={'my-10 flex'}>
|
2020-03-23 02:10:49 +00:00
|
|
|
<FlashMessageRender byKey={'account'} className={'mb-4'}/>
|
|
|
|
<ContentBox title={'Create API Key'} className={'flex-1'}>
|
|
|
|
<CreateApiKeyForm onKeyCreated={key => setKeys(s => ([...s!, key]))}/>
|
2020-03-23 01:15:38 +00:00
|
|
|
</ContentBox>
|
|
|
|
<ContentBox title={'API Keys'} className={'ml-10 flex-1'}>
|
2020-03-23 01:25:29 +00:00
|
|
|
<SpinnerOverlay visible={loading}/>
|
2020-03-23 02:10:49 +00:00
|
|
|
{deleteIdentifier &&
|
|
|
|
<ConfirmationModal
|
|
|
|
title={'Confirm key deletion'}
|
|
|
|
buttonText={'Yes, delete key'}
|
|
|
|
visible={true}
|
|
|
|
onConfirmed={() => {
|
|
|
|
doDeletion(deleteIdentifier);
|
|
|
|
setDeleteIdentifier('');
|
|
|
|
}}
|
|
|
|
onCanceled={() => setDeleteIdentifier('')}
|
|
|
|
>
|
|
|
|
Are you sure you wish to delete this API key? All requests using it will immediately be
|
|
|
|
invalidated and will fail.
|
|
|
|
</ConfirmationModal>
|
|
|
|
}
|
2020-03-23 01:25:29 +00:00
|
|
|
{
|
2020-03-23 02:10:49 +00:00
|
|
|
keys.length === 0 ?
|
|
|
|
<p className={'text-center text-sm'}>
|
|
|
|
{loading ? 'Loading...' : 'No API keys exist for this account.'}
|
|
|
|
</p>
|
|
|
|
:
|
|
|
|
keys.map(key => (
|
|
|
|
<div key={key.identifier} className={'grey-row-box bg-neutral-600 mb-2 flex items-center'}>
|
|
|
|
<FontAwesomeIcon icon={faKey} className={'text-neutral-300'}/>
|
|
|
|
<div className={'ml-4 flex-1'}>
|
|
|
|
<p className={'text-sm'}>{key.description}</p>
|
|
|
|
<p className={'text-2xs text-neutral-300 uppercase'}>
|
|
|
|
Last
|
|
|
|
used: {key.lastUsedAt ? format(key.lastUsedAt, 'MMM Do, YYYY HH:mm') : 'Never'}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<p className={'text-sm ml-4'}>
|
|
|
|
<code className={'font-mono py-1 px-2 bg-neutral-900 rounded'}>
|
|
|
|
{key.identifier}
|
|
|
|
</code>
|
|
|
|
</p>
|
|
|
|
<button
|
|
|
|
className={'ml-4 p-2 text-sm'}
|
|
|
|
onClick={() => setDeleteIdentifier(key.identifier)}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faTrashAlt}
|
|
|
|
className={'text-neutral-400 hover:text-red-400 transition-color duration-150'}
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
))
|
2020-03-23 01:25:29 +00:00
|
|
|
}
|
2020-03-23 01:15:38 +00:00
|
|
|
</ContentBox>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|