File size: 2,139 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Button } from '@wordpress/components';
import { check, copy } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import { useState } from 'react';
import FormButton from 'calypso/components/forms/form-button';
import FormButtonsBar from 'calypso/components/forms/form-buttons-bar';
import { useDispatch } from 'calypso/state';
import { errorNotice, successNotice } from 'calypso/state/notices/actions';

export default function NewAppPassword( { appName, newAppPassword, onClickDone } ) {
	const dispatch = useDispatch();
	const translate = useTranslate();

	const [ copyStatus, setCopyStatus ] = useState( '' );

	const handleCopyStatus = ( status ) => {
		setCopyStatus( status );
		setTimeout( () => {
			setCopyStatus( '' );
		}, 2000 );
	};

	const copyToClipboard = () => {
		navigator.clipboard
			.writeText( newAppPassword )
			.then( () => {
				handleCopyStatus( 'success' );
				dispatch(
					successNotice( translate( 'Application password copied to clipboard.' ), {
						duration: 2000,
					} )
				);
			} )
			.catch( () => {
				handleCopyStatus( 'error' );
				dispatch(
					errorNotice( translate( 'There was a problem copying the password.' ), {
						duration: 2000,
					} )
				);
			} );
	};

	return (
		<div className="application-passwords__new-password">
			<div className="application-passwords__new-password-display">
				<div className="application-passwords__new-password-content">{ newAppPassword }</div>
				<Button
					className="application-passwords__new-password-copy"
					icon={ 'success' === copyStatus ? check : copy }
					label={ 'success' === copyStatus ? translate( 'Copied!' ) : translate( 'Copy' ) }
					onClick={ copyToClipboard }
				/>
			</div>

			<p className="application-passwords__new-password-help">
				{ translate(
					'Use this password to log in to {{strong}}%(appName)s{{/strong}}. Note: spaces are ignored.',
					{
						args: { appName },
						components: { strong: <strong /> },
					}
				) }
			</p>

			<FormButtonsBar>
				<FormButton onClick={ onClickDone }>{ translate( 'Done' ) }</FormButton>
			</FormButtonsBar>
		</div>
	);
}