import { __ } from '@wordpress/i18n'; import { Button, Icon, SelectControl } from '@wordpress/components'; import { useEffect, useMemo, useState } from '@wordpress/element'; import { linkOff } from '@wordpress/icons'; import ConnectionStatus from './ConnectionStatus'; import LianaAPIFields from './LianaAPIFields'; import Steps from '../../shared/Steps'; import steps from './automationSteps'; import AutomationTogglerFields from './AutomationTogglerFields'; import { makeOptionsOfChannels } from '../utils'; type LianaAutomationSettingsProps = { channels: Array< { id: string; name: string } >; connected: boolean; cookieConsentPluginActive: boolean; loading: boolean; onReset: () => void; onSave: ( event: React.FormEvent< HTMLFormElement > ) => void; restEndpoint: string; setStep: ( updater: ( prevStep: number ) => number ) => void; settings: LianaAutomationSettings; step: number; }; export default function LianaAutomationSettings( props: LianaAutomationSettingsProps ) { const { channels, connected, cookieConsentPluginActive, loading, onReset, onSave, restEndpoint, setStep, settings, step, } = props; const [ stateSettings, setStateSettings ] = useState( settings ); // Sync stateSettings when props.settings changes on save useEffect( () => { setStateSettings( settings ); }, [ settings ] ); const onInputChange = ( name: string, value: string | boolean ) => { // Update the state or perform any action on input change const [ prefix, key ] = name.split( '_' ); setStateSettings( ( prevSettings ) => { return { ...prevSettings, [ prefix ]: { ...prevSettings[ prefix ], [ key ]: value, }, }; } ); }; const defaultChannelOptions = useMemo( () => makeOptionsOfChannels( channels, { value: '', label: __( 'Select a channel', 'liana-with-growthstack' ), } ), [ channels ] ); const channelOptions = useMemo( () => makeOptionsOfChannels( channels, { value: 'default', label: __( 'Default channel', 'liana-with-growthstack' ), } ), [ channels ] ); let submitButtonText = __( 'Save Settings', 'liana-with-growthstack' ); if ( loading ) { submitButtonText = __( 'Saving…', 'liana-with-growthstack' ); } else if ( step === 1 || step === 2 ) { submitButtonText = __( 'Continue', 'liana-with-growthstack' ); } return ( <> { step > 0 && }
{ ( step === 0 || step === 1 ) && (
{ step === 0 && (

{ __( 'LianaAutomation API Settings', 'liana-with-growthstack' ) }

) } { step === 1 && (

{ __( 'Request your API credentials from ', 'liana-with-growthstack' ) } { __( "Liana's support", 'liana-with-growthstack' ) }

) }
) } { ( step === 0 || step === 1 ) && ( ) } { step === 0 && (

{ __( 'Tracking options', 'liana-with-growthstack' ) }

) } { ( step === 0 || step === 2 ) && ( onInputChange( 'channel_id', value ) } options={ defaultChannelOptions } required variant="minimal" /> ) } { ( step === 0 || step === 3 ) && ( ) }
{ step > 1 && ( ) }
); }