import React, { useState, useEffect, useCallback } from 'react'; import { useHistory } from 'react-router-dom'; import { sprintf, __ } from '@wordpress/i18n'; import apiFetch from '@wordpress/api-fetch'; import ReactHtmlParser from 'react-html-parser'; import { useStateValue } from '../utils/StateProvider'; import { ExclamationCircleIcon, ArrowRightIcon, } from '@heroicons/react/24/outline'; function OptinStep() { const history = useHistory(); const [ requestError, setRequestError ] = useState( false ); const [ { action_button }, dispatch ] = useStateValue(); const [ formErrors, setFormErrors ] = useState( { name_error_message: '', email_error_message: '', error_class: '', } ); const { name_error_message, email_error_message, error_class } = formErrors; const changeOptinButtonText = useCallback( ( data ) => { dispatch( { status: 'SET_NEXT_STEP', action_button: data, } ); }, [] ); const default_button_text = __( 'Save & Continue', 'cartflows' ); useEffect( () => { // Set Foooter button text. changeOptinButtonText( { button_text: default_button_text, button_class: 'wcf-enrol-optin', } ); }, [ changeOptinButtonText ] ); const validateForm = function ( posted_data ) { let is_errors = false; const errors = {}; if ( ! posted_data.name || '' === posted_data.name ) { errors.name_error_message = __( 'Please Enter Name', 'cartflows' ); is_errors = true; } if ( ! posted_data.email || '' === posted_data.email ) { errors.email_error_message = __( 'Please Enter Email ID', 'cartflows' ); is_errors = true; } else if ( ! /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test( posted_data.email ) ) { errors.email_error_message = __( 'Entered email address is not a valid email', 'cartflows' ); is_errors = true; } if ( is_errors ) { setFormErrors( { name_error_message: errors.name_error_message ? errors.name_error_message : '', email_error_message: errors.email_error_message ? errors.email_error_message : '', error_class: '!border-red-500 focus:!ring-red-100 focus:!border-red-500', } ); } return is_errors; }; const submitOptinForm = function ( e ) { e.preventDefault(); changeOptinButtonText( { button_text: __( 'Processing', 'cartflows' ), button_class: 'wcf-enrol-optin', } ); dispatch( { status: 'PROCESSING', } ); const ajaxData = new window.FormData(); ajaxData.append( 'action', 'cartflows_user_onboarding' ); ajaxData.append( 'security', cartflows_wizard.user_onboarding_nonce ); const name = document.getElementById( 'wcf-user-name' ).value; const email = document.getElementById( 'wcf-user-email' ).value; if ( validateForm( { name, email } ) ) { changeOptinButtonText( { button_text: default_button_text, button_class: 'wcf-enrol-optin', } ); dispatch( { status: 'RESET', } ); return; } ajaxData.append( 'user_fname', name ); ajaxData.append( 'user_email', email ); apiFetch( { url: ajaxurl, method: 'POST', body: ajaxData, } ).then( ( response ) => { if ( response.data.success ) { history.push( { pathname: 'index.php', search: `?page=cartflow-setup&step=ready`, } ); dispatch( { status: 'RESET', } ); } else { setRequestError( response.data.message ); } changeOptinButtonText( { button_text: default_button_text, button_class: 'wcf-enrol-optin', } ); } ); }; return (
{ ReactHtmlParser(
sprintf(
/* translators: %1$s: html tag, %2$s: html tag*/
__(
'Let CartFlows take the guesswork out of your checkout results. Each week your store will send %1$s you an email report with key metrics and insights. You also will receive emails from us to %2$s help your store sell more.',
'cartflows'
),
'
',
'
'
)
) }
{ __( 'By clicking "Notify me", you agree to receive our newsletters as part of this course.', 'cartflows' ) }
*/ } { /* { 'processing' !== settingsProcess && () } */ }