mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02: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', |         right: '40px', | ||||||
|         bottom: '40px', |         bottom: '40px', | ||||||
|         transform: 'translateY(400px)', |         transform: 'translateY(400px)', | ||||||
|         zIndex: 300, |         zIndex: 1400, | ||||||
|         position: 'fixed', |         position: 'fixed', | ||||||
|     }, |     }, | ||||||
|     fadeInBottomEnter: { |     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 useApiTokensApi from '../../../../hooks/api/actions/useApiTokensApi/useApiTokensApi'; | ||||||
| import PermissionButton from '../../../common/PermissionButton/PermissionButton'; | import PermissionButton from '../../../common/PermissionButton/PermissionButton'; | ||||||
| import { ADMIN } from '../../../providers/AccessProvider/permissions'; | import { ADMIN } from '../../../providers/AccessProvider/permissions'; | ||||||
|  | import ConfirmToken from '../ConfirmToken/ConfirmToken'; | ||||||
|  | import { useState } from 'react'; | ||||||
|  | import { scrollToTop } from '../../../common/util'; | ||||||
| 
 | 
 | ||||||
| const CreateApiToken = () => { | const CreateApiToken = () => { | ||||||
|     /* @ts-ignore */ |     /* @ts-ignore */ | ||||||
|     const { setToastData, setToastApiError } = useToast(); |     const { setToastApiError } = useToast(); | ||||||
|     const { uiConfig } = useUiConfig(); |     const { uiConfig } = useUiConfig(); | ||||||
|     const history = useHistory(); |     const history = useHistory(); | ||||||
|  |     const [showConfirm, setShowConfirm] = useState(false); | ||||||
|  |     const [token, setToken] = useState(''); | ||||||
|  | 
 | ||||||
|     const { |     const { | ||||||
|         getApiTokenPayload, |         getApiTokenPayload, | ||||||
|         username, |         username, | ||||||
| @ -36,19 +42,24 @@ const CreateApiToken = () => { | |||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|         try { |         try { | ||||||
|             await createToken(getApiTokenPayload()); |             const payload = getApiTokenPayload(); | ||||||
|             history.push('/admin/api'); |             await createToken(payload) | ||||||
|             setToastData({ |                 .then(res => res.json()) | ||||||
|                 type: 'success', |                 .then(api => { | ||||||
|                 title: 'Created token', |                     scrollToTop(); | ||||||
|                 text: 'Successfully created API token', |                     setToken(api.secret); | ||||||
|                 confetti: true, |                     setShowConfirm(true); | ||||||
|                 }); |                 }); | ||||||
|         } catch (e: any) { |         } catch (e: any) { | ||||||
|             setToastApiError(e.toString()); |             setToastApiError(e.toString()); | ||||||
|         } |         } | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|  |     const closeConfirm = () => { | ||||||
|  |         setShowConfirm(false); | ||||||
|  |         history.push('/admin/api'); | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|     const formatApiCode = () => { |     const formatApiCode = () => { | ||||||
|         return `curl --location --request POST '${ |         return `curl --location --request POST '${ | ||||||
|             uiConfig.unleashUrl |             uiConfig.unleashUrl | ||||||
| @ -93,6 +104,11 @@ const CreateApiToken = () => { | |||||||
|                     Create token |                     Create token | ||||||
|                 </PermissionButton> |                 </PermissionButton> | ||||||
|             </ApiTokenForm> |             </ApiTokenForm> | ||||||
|  |             <ConfirmToken | ||||||
|  |                 open={showConfirm} | ||||||
|  |                 closeConfirm={closeConfirm} | ||||||
|  |                 token={token} | ||||||
|  |             /> | ||||||
|         </FormTemplate> |         </FormTemplate> | ||||||
|     ); |     ); | ||||||
| }; | }; | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user