File size: 5,816 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* eslint-disable wpcalypso/jsx-gridicon-size */
import { Card, FormLabel } from '@automattic/components';
import { useHasEnTranslation } from '@automattic/i18n-utils';
import styled from '@emotion/styled';
import { useTranslate, localize } from 'i18n-calypso';
import { useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormRadio from 'calypso/components/forms/form-radio';
import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation';
import InfoPopover from 'calypso/components/info-popover';
import InlineSupportLink from 'calypso/components/inline-support-link';
import SettingsSectionHeader from 'calypso/my-sites/site-settings/settings-section-header';
import { useSelector } from 'calypso/state';
import { recordTracksEvent, recordGoogleEvent } from 'calypso/state/analytics/actions';
import {
	errorNotice,
	infoNotice,
	removeNotice,
	successNotice,
} from 'calypso/state/notices/actions';
import { getSiteOption } from 'calypso/state/sites/selectors';
import { useSiteInterfaceMutation } from './use-select-interface-mutation';
import './style.scss';

const changeLoadingNoticeId = 'admin-interface-change-loading';
const successNoticeId = 'admin-interface-change-success';
const failureNoticeId = 'admin-interface-change-failure';

const FormRadioStyled = styled( FormRadio )( {
	'&.form-radio:disabled:checked::before': {
		backgroundColor: 'var(--color-primary)',
		opacity: 0.6,
	},
} );

const SiteAdminInterface = ( { siteId, isHosting = false } ) => {
	const translate = useTranslate();
	const hasEnTranslation = useHasEnTranslation();
	const dispatch = useDispatch();
	const removeAllNotices = () => {
		dispatch( removeNotice( successNoticeId ) );
		dispatch( removeNotice( failureNoticeId ) );
		dispatch( removeNotice( changeLoadingNoticeId ) );
	};

	const adminInterface = useSelector(
		( state ) => getSiteOption( state, siteId, 'wpcom_admin_interface' ) || 'calypso'
	);

	const { setSiteInterface, isLoading: isUpdating } = useSiteInterfaceMutation( siteId, {
		onMutate: () => {
			removeAllNotices();
			dispatch(
				infoNotice( translate( 'Changing admin interface style…' ), {
					id: changeLoadingNoticeId,
					showDismiss: false,
					isLoading: true,
					icon: 'sync',
				} )
			);
		},
		onSuccess() {
			dispatch( removeNotice( changeLoadingNoticeId ) );
			dispatch(
				successNotice( translate( 'Admin interface style changed.' ), {
					id: successNoticeId,
				} )
			);
		},
		onError: () => {
			dispatch( removeNotice( changeLoadingNoticeId ) );
			dispatch(
				errorNotice( translate( 'Failed to change admin interface style.' ), {
					id: failureNoticeId,
				} )
			);
		},
	} );

	const [ selectedAdminInterface, setSelectedAdminInterface ] = useState( adminInterface );

	// When switching from Classic to Default, adminInterface will initially reflect the cached state 'wp-admin'.
	// It will then be updated to 'calypso', so we need to sync the change to selectedAdminInterface.
	useEffect( () => {
		setSelectedAdminInterface( adminInterface );
	}, [ adminInterface ] );

	const handleSubmitForm = ( value ) => {
		if ( isHosting ) {
			dispatch(
				recordTracksEvent( 'calypso_hosting_configuration_admin_interface_change', {
					interface: value,
				} )
			);
		} else {
			dispatch(
				recordTracksEvent( 'calypso_site_settings_admin_interface_updated', {
					interface: value,
				} )
			);
		}

		setSiteInterface( value );
	};

	const handleInputChange = async ( value ) => {
		setSelectedAdminInterface( value );
		if ( isHosting ) {
			handleSubmitForm( value );
		} else {
			dispatch( recordGoogleEvent( 'Site Settings', `Set wpcom_admin_interface to ${ value }` ) );
		}
	};

	const renderForm = () => {
		return (
			<>
				<FormFieldset>
					<FormLabel>
						<FormRadioStyled
							label={ translate( 'Classic style' ) }
							name="wpcom_admin_interface"
							value="wp-admin"
							checked={ selectedAdminInterface === 'wp-admin' }
							onChange={ ( event ) => handleInputChange( event.target.value ) }
							disabled={ isUpdating }
						/>
					</FormLabel>
					<FormSettingExplanation>
						{ translate( 'Use WP-Admin to manage your site.' ) }
					</FormSettingExplanation>
				</FormFieldset>
				<FormFieldset>
					<FormLabel>
						<FormRadioStyled
							label={ translate( 'Default style' ) }
							name="wpcom_admin_interface"
							value="calypso"
							checked={ selectedAdminInterface === 'calypso' }
							onChange={ ( event ) => handleInputChange( event.target.value ) }
							disabled={ isUpdating }
						/>
					</FormLabel>
					<FormSettingExplanation>
						{ hasEnTranslation( 'Use WordPress.com’s native dashboard to manage your site.' )
							? translate( 'Use WordPress.com’s native dashboard to manage your site.' )
							: translate( 'Use WordPress.com’s legacy dashboard to manage your site.' ) }
					</FormSettingExplanation>
				</FormFieldset>
			</>
		);
	};

	return (
		<>
			<SettingsSectionHeader
				id="admin-interface-style"
				disabled={ isUpdating }
				isSaving={ isUpdating }
				onButtonClick={ () => handleSubmitForm( selectedAdminInterface ) }
				showButton
				title={ translate(
					'Admin interface style {{infoPopover}} Set the admin interface style for all users. {{supportLink}}Learn more{{/supportLink}}. {{/infoPopover}}',
					{
						components: {
							supportLink: (
								<InlineSupportLink supportContext="admin-interface-style" showIcon={ false } />
							),
							infoPopover: <InfoPopover position="bottom right" />,
						},
						comment: 'The header of the Admin interface style setting',
					}
				) }
			/>
			<Card>{ renderForm() }</Card>
		</>
	);
};

export default localize( SiteAdminInterface );