From 6733c22f3f5f0cb668dc7c958c97485ef49c4748 Mon Sep 17 00:00:00 2001
From: Ilyas Foo
Date: Tue, 20 Aug 2024 11:17:17 +0800
Subject: [PATCH] Add site visibility settings confirmation modal (#50759)
* Add confirmation modal for site visibility when changing from live to coming soon mode
* Changelog
* Remove unnecessary space
* Update tests
* Lint
---
.../components/confirmation-modal.jsx | 116 +++++++++++
.../components/confirmation-modal.scss | 24 +++
.../test/confirmation-modal.test.js | 183 ++++++++++++++++++
.../launch-your-store/settings/slotfill.js | 24 +++
.../update-site-visibility-confirmation-modal | 4 +
5 files changed, 351 insertions(+)
create mode 100644 plugins/woocommerce-admin/client/launch-your-store/settings/components/confirmation-modal.jsx
create mode 100644 plugins/woocommerce-admin/client/launch-your-store/settings/components/confirmation-modal.scss
create mode 100644 plugins/woocommerce-admin/client/launch-your-store/settings/components/test/confirmation-modal.test.js
create mode 100644 plugins/woocommerce/changelog/update-site-visibility-confirmation-modal
diff --git a/plugins/woocommerce-admin/client/launch-your-store/settings/components/confirmation-modal.jsx b/plugins/woocommerce-admin/client/launch-your-store/settings/components/confirmation-modal.jsx
new file mode 100644
index 00000000000..3d1435af5a4
--- /dev/null
+++ b/plugins/woocommerce-admin/client/launch-your-store/settings/components/confirmation-modal.jsx
@@ -0,0 +1,116 @@
+/**
+ * External dependencies
+ */
+import React from 'react';
+import { Button, Modal } from '@wordpress/components';
+import { useState, useEffect } from '@wordpress/element';
+import { __ } from '@wordpress/i18n';
+
+/**
+ * Internal dependencies
+ */
+import './confirmation-modal.scss';
+
+/**
+ * Confirmation modal for the site visibility settings.
+ *
+ * @return {React.ReactNode} The confirmation modal component.
+ */
+export const ConfirmationModal = ( {
+ formRef,
+ saveButtonRef,
+ currentSetting,
+} ) => {
+ const [ pendingSubmitEvent, setPendingSubmitEvent ] = useState( null );
+ const [ isConfirmModalOpen, setIsConfirmModalOpen ] = useState( false );
+ const currentComingSoon = currentSetting?.woocommerce_coming_soon ?? 'no';
+
+ // Hooks into settings' "mainform" to show a confirmation modal when the form is submitted.
+ useEffect( () => {
+ const form = formRef.current;
+ const handleFormSubmit = ( event ) => {
+ const formData = new FormData( form );
+
+ // Only block submission when switching to coming soon mode from live.
+ if (
+ currentComingSoon === 'no' &&
+ formData.get( 'woocommerce_coming_soon' ) === 'yes'
+ ) {
+ event.preventDefault();
+ setIsConfirmModalOpen( true );
+ setPendingSubmitEvent( event );
+ }
+ };
+ if ( form ) {
+ form.addEventListener( 'submit', handleFormSubmit );
+ }
+
+ return () => {
+ if ( form ) {
+ form.removeEventListener( 'submit', handleFormSubmit );
+ }
+ };
+ }, [ currentSetting, formRef ] );
+
+ const cancelSubmit = () => {
+ setPendingSubmitEvent( null ); // Clear the pending submit
+ setIsConfirmModalOpen( false ); // Close the modal
+
+ if ( saveButtonRef.current ) {
+ saveButtonRef.current.classList.remove( 'is-busy' );
+ }
+ };
+
+ const confirmSubmit = () => {
+ if ( pendingSubmitEvent ) {
+ // WooCommerce checks for the "save" input.
+ if ( saveButtonRef.current && formRef.current ) {
+ const hiddenInput = document.createElement( 'input' );
+ hiddenInput.type = 'hidden';
+ hiddenInput.name = saveButtonRef.current.name || 'save';
+ hiddenInput.value =
+ saveButtonRef.current.value ||
+ __( 'Save changes', 'woocommerce' );
+ formRef.current.appendChild( hiddenInput );
+ }
+
+ pendingSubmitEvent.target.submit();
+ setPendingSubmitEvent( null );
+ }
+ setIsConfirmModalOpen( false ); // Close the modal
+ };
+
+ return isConfirmModalOpen ? (
+
+
+ { __(
+ "Are you sure you want to switch from live to coming soon mode? Your site will not be visible, and customers won't be able to make purchases during this time.",
+ 'woocommerce'
+ ) }
+