File size: 1,907 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
73
74
75
76
77
78
79
80
81
import { Dialog } from '@automattic/components';
import { localize } from 'i18n-calypso';
import { useEffect, useState } from 'react';
import { connect } from 'react-redux';
import {
	withAnalytics,
	composeAnalytics,
	recordTracksEvent,
	recordGoogleEvent,
	bumpStat,
} from 'calypso/state/analytics/actions';
import { restoreDatabasePassword } from 'calypso/state/hosting/actions';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';

const RestorePasswordDialog = ( {
	isVisible,
	onRestore,
	onCancel,
	siteId,
	translate,
	restore,
} ) => {
	const [ shouldRestore, setShouldRestore ] = useState( false );
	useEffect( () => {
		if ( shouldRestore ) {
			restore( siteId );
			onRestore();
		}
	}, [ shouldRestore, siteId ] );

	useEffect( () => {
		if ( isVisible ) {
			setShouldRestore( false ); // Reset state first when dialog is opened.
		}
	}, [ isVisible ] );

	const buttons = [
		{
			action: 'cancel',
			label: translate( 'Cancel' ),
			onClick: onCancel,
		},
		{
			action: 'restore',
			label: translate( 'Restore' ),
			onClick: () => setShouldRestore( true ),
			isPrimary: true,
		},
	];
	return (
		<Dialog isVisible={ isVisible } buttons={ buttons } onClose={ onCancel } showCloseIcon>
			<h1>{ translate( 'Restore database password' ) }</h1>
			<p>
				{ translate( 'Are you sure you want to restore the default password of your database?' ) }
			</p>
		</Dialog>
	);
};

const restore = ( siteId ) =>
	withAnalytics(
		composeAnalytics(
			recordGoogleEvent(
				'Hosting Configuration',
				'Clicked "Restore" Button in Database Access card'
			),
			recordTracksEvent( 'calypso_hosting_configuration_restore_db_password' ),
			bumpStat( 'hosting-config', 'restore-db-password' )
		),
		restoreDatabasePassword( siteId )
	);

export default connect(
	( state ) => ( {
		siteId: getSelectedSiteId( state ),
	} ),
	{
		restore,
	}
)( localize( RestorePasswordDialog ) );