import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; const TEMPLATE_ICONS = { 'blank-1col': ( ), 'blank-2col': ( ), 'blank-3col': ( ), 'charity-basic': ( ), }; export default function TemplateSelectModal( { onClose } ) { const data = window.betterPaymentCampaignData || {}; const adminData = window.betterPaymentAdmin || {}; const templates = data.templates || adminData.campaignTemplates || []; const restUrl = data.restUrl || adminData.apiUrl || ''; const nonce = data.nonce || adminData.nonce || ''; const adminUrl = data.adminUrl || adminData.adminBaseUrl || ''; const [ creating, setCreating ] = useState( false ); const [ error, setError ] = useState( null ); const handleSelect = async ( template ) => { setCreating( true ); setError( null ); try { const res = await fetch( `${ restUrl }campaigns`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': nonce, }, body: JSON.stringify( { title: template.default_title || template.label || __( 'New Campaign', 'better-payment' ), post_status: 'draft', bpc_fields_layout: JSON.stringify( { layout: template.layout, columns: template.columns, } ), } ), } ); if ( ! res.ok ) { throw new Error( __( 'Could not create campaign. Please try again.', 'better-payment' ) ); } const campaign = await res.json(); window.location.href = `${ adminUrl }admin.php?page=bp-campaign-builder&campaign_id=${ campaign.id }`; } catch ( err ) { setError( err.message ); setCreating( false ); } }; const handleOverlayClick = ( e ) => { if ( e.target === e.currentTarget ) { onClose(); } }; return (

{ __( 'Choose a Starting Layout', 'better-payment' ) }

{ __( 'Select a template to begin building your fundraising campaign.', 'better-payment' ) }

{ error && (
{ error }
) } { creating ? (
{ __( 'Creating campaign…', 'better-payment' ) }
) : (
{ templates.map( ( tpl ) => ( ) ) }
) }
); }