mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-14 00:19:16 +01:00
feat: show token after creation (#614)
* feat: show token after creation * feat: replace snackbar with toast
This commit is contained in:
parent
90231cc230
commit
62d7f2d947
@ -90,7 +90,7 @@ export const useCommonStyles = makeStyles(theme => ({
|
||||
right: '40px',
|
||||
bottom: '40px',
|
||||
transform: 'translateY(400px)',
|
||||
zIndex: 300,
|
||||
zIndex: 1400,
|
||||
position: 'fixed',
|
||||
},
|
||||
fadeInBottomEnter: {
|
||||
|
@ -0,0 +1,35 @@
|
||||
import { Typography } from '@material-ui/core';
|
||||
import { useCommonStyles } from '../../../../common.styles';
|
||||
import Dialogue from '../../../common/Dialogue';
|
||||
import UserToken from './UserToken/UserToken';
|
||||
|
||||
interface IConfirmUserLink {
|
||||
open: boolean;
|
||||
closeConfirm: () => void;
|
||||
token: string;
|
||||
}
|
||||
|
||||
const ConfirmToken = ({
|
||||
open,
|
||||
closeConfirm,
|
||||
token,
|
||||
}: IConfirmUserLink) => {
|
||||
const commonStyles = useCommonStyles();
|
||||
return (
|
||||
<Dialogue
|
||||
open={open}
|
||||
onClick={closeConfirm}
|
||||
primaryButtonText="Close"
|
||||
title="New token created"
|
||||
>
|
||||
<div className={commonStyles.contentSpacingYLarge}>
|
||||
<Typography variant="body1">
|
||||
Your new token has been created successfully.
|
||||
</Typography>
|
||||
<UserToken token={token} />
|
||||
</div>
|
||||
</Dialogue>
|
||||
);
|
||||
};
|
||||
|
||||
export default ConfirmToken;
|
@ -0,0 +1,58 @@
|
||||
import { IconButton } from '@material-ui/core';
|
||||
import CopyIcon from '@material-ui/icons/FileCopy';
|
||||
import useToast from '../../../../../hooks/useToast';
|
||||
|
||||
interface IUserTokenProps {
|
||||
token: string;
|
||||
}
|
||||
|
||||
const UserToken = ({ token }: IUserTokenProps) => {
|
||||
const { setToastData } = useToast();
|
||||
|
||||
const handleCopy = () => {
|
||||
try {
|
||||
return navigator.clipboard
|
||||
.writeText(token)
|
||||
.then(() => {
|
||||
setToastData({
|
||||
type: 'success',
|
||||
title: 'Token copied',
|
||||
text: `Token is copied to clipboard`,
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
setError();
|
||||
});
|
||||
} catch (e) {
|
||||
setError();
|
||||
}
|
||||
};
|
||||
|
||||
const setError = () =>
|
||||
setToastData({
|
||||
type: 'error',
|
||||
title: 'Could not copy token',
|
||||
});
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
backgroundColor: '#efefef',
|
||||
padding: '2rem',
|
||||
borderRadius: '3px',
|
||||
margin: '1rem 0',
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
alignItems: 'center',
|
||||
wordBreak: 'break-all',
|
||||
}}
|
||||
>
|
||||
{token}
|
||||
<IconButton onClick={handleCopy}>
|
||||
<CopyIcon />
|
||||
</IconButton>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default UserToken;
|
@ -7,12 +7,18 @@ import useToast from '../../../../hooks/useToast';
|
||||
import useApiTokensApi from '../../../../hooks/api/actions/useApiTokensApi/useApiTokensApi';
|
||||
import PermissionButton from '../../../common/PermissionButton/PermissionButton';
|
||||
import { ADMIN } from '../../../providers/AccessProvider/permissions';
|
||||
import ConfirmToken from '../ConfirmToken/ConfirmToken';
|
||||
import { useState } from 'react';
|
||||
import { scrollToTop } from '../../../common/util';
|
||||
|
||||
const CreateApiToken = () => {
|
||||
/* @ts-ignore */
|
||||
const { setToastData, setToastApiError } = useToast();
|
||||
const { setToastApiError } = useToast();
|
||||
const { uiConfig } = useUiConfig();
|
||||
const history = useHistory();
|
||||
const [showConfirm, setShowConfirm] = useState(false);
|
||||
const [token, setToken] = useState('');
|
||||
|
||||
const {
|
||||
getApiTokenPayload,
|
||||
username,
|
||||
@ -36,19 +42,24 @@ const CreateApiToken = () => {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await createToken(getApiTokenPayload());
|
||||
history.push('/admin/api');
|
||||
setToastData({
|
||||
type: 'success',
|
||||
title: 'Created token',
|
||||
text: 'Successfully created API token',
|
||||
confetti: true,
|
||||
});
|
||||
const payload = getApiTokenPayload();
|
||||
await createToken(payload)
|
||||
.then(res => res.json())
|
||||
.then(api => {
|
||||
scrollToTop();
|
||||
setToken(api.secret);
|
||||
setShowConfirm(true);
|
||||
});
|
||||
} catch (e: any) {
|
||||
setToastApiError(e.toString());
|
||||
}
|
||||
};
|
||||
|
||||
const closeConfirm = () => {
|
||||
setShowConfirm(false);
|
||||
history.push('/admin/api');
|
||||
};
|
||||
|
||||
const formatApiCode = () => {
|
||||
return `curl --location --request POST '${
|
||||
uiConfig.unleashUrl
|
||||
@ -93,6 +104,11 @@ const CreateApiToken = () => {
|
||||
Create token
|
||||
</PermissionButton>
|
||||
</ApiTokenForm>
|
||||
<ConfirmToken
|
||||
open={showConfirm}
|
||||
closeConfirm={closeConfirm}
|
||||
token={token}
|
||||
/>
|
||||
</FormTemplate>
|
||||
);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user