File size: 3,491 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { translate } from 'i18n-calypso';
import { useEffect, useState } from 'react';
import { connect } from 'react-redux';
import Notice from 'calypso/components/notice';
import { useDispatch, useSelector } from 'calypso/state';
import { fetchAutomatedTransferStatus } from 'calypso/state/automated-transfer/actions';
import {
	TransferStates,
	transferInProgress,
	transferStates,
} from 'calypso/state/automated-transfer/constants';
import { getAutomatedTransferStatus } from 'calypso/state/automated-transfer/selectors';
import { initiateThemeTransfer } from 'calypso/state/themes/actions';
import { getSelectedSiteId, getSelectedSiteSlug } from 'calypso/state/ui/selectors';
import { AppState } from 'calypso/types';
import { activateContext } from './components/activate-context';
import { HostingErrorStatus } from './components/hosting-error-status';
import './style.scss';

interface HostingActivateStatusProps {
	context: activateContext;
	siteId: number | null;
	keepAlive?: boolean;
	forceEnable?: boolean;
	onTick?: (
		isTransferring?: boolean,
		wasTransferring?: boolean,
		isTransferCompleted?: boolean
	) => void;
}

const endStates: TransferStates[] = [
	transferStates.NONE,
	transferStates.COMPLETE,
	transferStates.COMPLETED,
	transferStates.FAILURE,
	transferStates.ERROR,
	transferStates.REVERTED,
	transferStates.NULL,
];

const HostingActivateStatus = ( {
	context,
	siteId,
	onTick,
	keepAlive,
	forceEnable,
}: HostingActivateStatusProps ) => {
	const transferStatus = useSelector( ( state ) => getAutomatedTransferStatus( state, siteId ) );

	const isTransferring =
		! endStates.includes( transferStatus as TransferStates ) &&
		transferStatus !== transferStates.INQUIRING;

	const dispatch = useDispatch();
	const isTransferCompleted = endStates.includes(
		transferStatus as ( typeof transferInProgress )[ number ]
	);
	const [ wasTransferring, setWasTransferring ] = useState( false );

	useEffect( () => {
		if ( isTransferring && ! wasTransferring ) {
			setWasTransferring( true );
		}
		if ( ! isTransferring && wasTransferring && isTransferCompleted ) {
			setWasTransferring( false );
		}
		if ( ! isTransferCompleted ) {
			dispatch( fetchAutomatedTransferStatus( siteId ?? 0 ) );
		}
		onTick?.( isTransferring, wasTransferring, isTransferCompleted );
	}, [ isTransferCompleted, isTransferring, onTick, wasTransferring ] );

	const getLoadingText = () => {
		switch ( context ) {
			case 'theme':
				return translate( 'Please wait while we activate the theme features.' );
			case 'plugin':
				return translate( 'Please wait while we activate the plugin features.' );
			default:
				return translate( 'Please wait while we activate the hosting features.' );
		}
	};

	if ( transferStatus === transferStates.ERROR ) {
		return <HostingErrorStatus context={ context } />;
	}

	if ( isTransferCompleted && ! forceEnable ) {
		return null;
	}

	if ( isTransferring || keepAlive || forceEnable ) {
		return (
			<Notice
				className="hosting__activating-notice"
				status="is-info"
				showDismiss={ false }
				text={ getLoadingText() }
				icon="sync"
			/>
		);
	}
};

const mapStateToProps = ( state: AppState ) => {
	const siteId = getSelectedSiteId( state );
	const siteSlug = getSelectedSiteSlug( state );

	return {
		siteId,
		siteSlug,
	};
};

const mapDispatchToProps = {
	initiateTransfer: initiateThemeTransfer,
	fetchAutomatedTransferStatus,
};

export default connect( mapStateToProps, mapDispatchToProps )( HostingActivateStatus );