<commit_before>/**
 * DashboardSharingSettings Footer component.
 *
 * Site Kit by Google, Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * External dependencies
 */
import PropTypes from 'prop-types';

/**
 * WordPress dependencies
 */
import { __ } from '@wordpress/i18n';
import { useCallback, useState } from '@wordpress/element';

/**
 * Internal dependencies
 */
import Data from 'googlesitekit-data';
import Link from '../../Link';
import Button from '../../Button';
import Notice from './Notice';
import { CORE_MODULES } from '../../../googlesitekit/modules/datastore/constants';
import { CORE_UI } from '../../../googlesitekit/datastore/ui/constants';
import Spinner from '../../Spinner';
import {
	EDITING_MANAGEMENT_KEY,
	EDITING_USER_ROLE_SELECT_SLUG_KEY,
} from './constants';
import ErrorText from '../../ErrorText';
import { trackEvent } from '../../../util';
import useViewContext from '../../../hooks/useViewContext';
const { useSelect, useDispatch } = Data;

export default function Footer( { closeDialog } ) {
	const viewContext = useViewContext();

	const [ errorNotice, setErrorNotice ] = useState( null );
	const canSubmitSharingChanges = useSelect( ( select ) =>
		select( CORE_MODULES ).canSubmitSharingChanges()
	);
	const isSaving = useSelect( ( select ) =>
		select( CORE_MODULES ).isDoingSubmitSharingChanges()
	);

	const { saveSharingSettings } = useDispatch( CORE_MODULES );
	const { setValue } = useDispatch( CORE_UI );

	const onApply = useCallback( async () => {
		setErrorNotice( null );

		const { error } = await saveSharingSettings();
		if ( error ) {
			setErrorNotice( error.message );
			return;
		}

		trackEvent( `${ viewContext }_sharing`, 'settings_confirm' );

		// Reset the state to enable modules in when not editing or saving.
		setValue( EDITING_USER_ROLE_SELECT_SLUG_KEY, undefined );
		setValue( EDITING_MANAGEMENT_KEY, false );

		closeDialog();
	}, [ viewContext, saveSharingSettings, setValue, closeDialog ] );

	const onCancel = useCallback( () => {
		trackEvent( `${ viewContext }_sharing`, 'settings_cancel' );
		closeDialog();
	}, [ closeDialog, viewContext ] );

	return (
		<div className="googlesitekit-dashboard-sharing-settings__footer">
			<div className="googlesitekit-dashboard-sharing-settings__footer-notice">
				{ errorNotice && <ErrorText message={ errorNotice } /> }
				{ ! errorNotice && <Notice /> }
			</div>

			<div className="googlesitekit-dashboard-sharing-settings__footer-actions">
				<Link onClick={ onCancel }>
					{ __( 'Cancel', 'google-site-kit' ) }
				</Link>

				<Button
					onClick={ onApply }
					disabled={ isSaving || ! canSubmitSharingChanges }
				>
					{ __( 'Apply', 'google-site-kit' ) }
					{ isSaving && <Spinner isSaving={ isSaving } /> }
				</Button>
			</div>
		</div>
	);
}

Footer.propTypes = {
	closeDialog: PropTypes.func.isRequired,
};<commit_msg>Dispatch rollback action if settings have changed.<commit_after>/**
 * DashboardSharingSettings Footer component.
 *
 * Site Kit by Google, Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * External dependencies
 */
import PropTypes from 'prop-types';

/**
 * WordPress dependencies
 */
import { __ } from '@wordpress/i18n';
import { useCallback, useState } from '@wordpress/element';

/**
 * Internal dependencies
 */
import Data from 'googlesitekit-data';
import Link from '../../Link';
import Button from '../../Button';
import Notice from './Notice';
import { CORE_MODULES } from '../../../googlesitekit/modules/datastore/constants';
import { CORE_UI } from '../../../googlesitekit/datastore/ui/constants';
import Spinner from '../../Spinner';
import {
	EDITING_MANAGEMENT_KEY,
	EDITING_USER_ROLE_SELECT_SLUG_KEY,
} from './constants';
import ErrorText from '../../ErrorText';
import { trackEvent } from '../../../util';
import useViewContext from '../../../hooks/useViewContext';
const { useSelect, useDispatch } = Data;

export default function Footer( { closeDialog } ) {
	const viewContext = useViewContext();

	const [ errorNotice, setErrorNotice ] = useState( null );
	const canSubmitSharingChanges = useSelect( ( select ) =>
		select( CORE_MODULES ).canSubmitSharingChanges()
	);
	const isSaving = useSelect( ( select ) =>
		select( CORE_MODULES ).isDoingSubmitSharingChanges()
	);
	const haveSettingsChanged = useSelect( ( select ) =>
		select( CORE_MODULES ).haveSharingSettingsChanged()
	);

	const { saveSharingSettings, rollbackSharingSettings } = useDispatch(
		CORE_MODULES
	);
	const { setValue } = useDispatch( CORE_UI );

	const onApply = useCallback( async () => {
		setErrorNotice( null );

		const { error } = await saveSharingSettings();
		if ( error ) {
			setErrorNotice( error.message );
			return;
		}

		trackEvent( `${ viewContext }_sharing`, 'settings_confirm' );

		// Reset the state to enable modules in when not editing or saving.
		setValue( EDITING_USER_ROLE_SELECT_SLUG_KEY, undefined );
		setValue( EDITING_MANAGEMENT_KEY, false );

		closeDialog();
	}, [ viewContext, saveSharingSettings, setValue, closeDialog ] );

	const onCancel = useCallback( () => {
		trackEvent( `${ viewContext }_sharing`, 'settings_cancel' );
		closeDialog();

		if ( haveSettingsChanged ) {
			rollbackSharingSettings();
		}
	}, [
		closeDialog,
		haveSettingsChanged,
		rollbackSharingSettings,
		viewContext,
	] );

	return (
		<div className="googlesitekit-dashboard-sharing-settings__footer">
			<div className="googlesitekit-dashboard-sharing-settings__footer-notice">
				{ errorNotice && <ErrorText message={ errorNotice } /> }
				{ ! errorNotice && <Notice /> }
			</div>

			<div className="googlesitekit-dashboard-sharing-settings__footer-actions">
				<Link onClick={ onCancel }>
					{ __( 'Cancel', 'google-site-kit' ) }
				</Link>

				<Button
					onClick={ onApply }
					disabled={ isSaving || ! canSubmitSharingChanges }
				>
					{ __( 'Apply', 'google-site-kit' ) }
					{ isSaving && <Spinner isSaving={ isSaving } /> }
				</Button>
			</div>
		</div>
	);
}

Footer.propTypes = {
	closeDialog: PropTypes.func.isRequired,
};
{'fraction_non_alphanumeric': 0.13455872035919741, 'fraction_numerical': 0.002806229830223095, 'mean_word_length': 3.8721804511278197, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 38, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '18555', 'n_tokens_mistral': 2284, 'n_tokens_neox': 2117, 'n_words': 583}