import React, { useState, useMemo } from 'react'; import { ExperimentCreateInput, ExperimentType, ExperimentMetric } from '../types'; import { createExperiment } from '../services/experiments'; interface ExperimentFormProps { onClose: () => void; onSuccess: () => void; initialAsins?: string[]; initialMarketplace?: string; } const ExperimentForm: React.FC = ({ onClose, onSuccess, initialAsins = [], initialMarketplace = 'DE' }) => { const [formData, setFormData] = useState({ name: '', description: '', type: 'pricing', asins: initialAsins, marketplace: initialMarketplace, start_date: new Date().toISOString().split('T')[0], end_date: '', hypothesis: '', primary_metric: 'units', target_lift_percent: 10, owner: '', }); const [loading, setLoading] = useState(false); const [asinInput, setAsinInput] = useState(''); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!formData.name || !formData.asins.length) { alert('Please fill in required fields'); return; } setLoading(true); try { await createExperiment(formData); onSuccess(); onClose(); } catch (error: any) { alert(`Error creating experiment: ${error.message}`); } finally { setLoading(false); } }; const handleAddAsin = () => { const newAsins = asinInput .split(',') .map(a => a.trim().toUpperCase()) .filter(a => a.length > 0); const uniqueAsins = Array.from(new Set([...formData.asins, ...newAsins])); setFormData({ ...formData, asins: uniqueAsins }); setAsinInput(''); }; const handleRemoveAsin = (asinToRemove: string) => { setFormData({ ...formData, asins: formData.asins.filter(a => a !== asinToRemove), }); }; const typeOptions: { value: ExperimentType; label: string; icon: string }[] = [ { value: 'pricing', label: 'Pricing', icon: '๐Ÿ’ฐ' }, { value: 'advertising', label: 'Advertising', icon: '๐Ÿ“ข' }, { value: 'content', label: 'Content', icon: '๐Ÿ“' }, { value: 'promotion', label: 'Promotion', icon: '๐Ÿท๏ธ' }, ]; const metricOptions: { value: ExperimentMetric; label: string }[] = [ { value: 'units', label: 'Units Sold' }, { value: 'revenue', label: 'Revenue' }, { value: 'acos', label: 'ACOS' }, { value: 'ctr', label: 'CTR' }, { value: 'cvr', label: 'Conversion Rate' }, { value: 'bsr', label: 'BSR' }, ]; const marketplaceOptions = ['DE', 'UK', 'FR', 'IT', 'ES']; return (
{/* Header */}

๐Ÿงช New Experiment

{/* Form */}
{/* Name */}
setFormData({ ...formData, name: e.target.value })} className="w-full bg-slate-800 border border-slate-600 rounded-lg px-3 py-1.5 text-sm text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-indigo-500" placeholder="e.g., Q1 Price Reduction Test" required />
{/* Type & Marketplace */}
{/* ASINs */}
setAsinInput(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && (e.preventDefault(), handleAddAsin())} className="flex-1 bg-slate-800 border border-slate-600 rounded-lg px-3 py-1.5 text-sm text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-indigo-500" placeholder="ASINs (comma-separated)" />
{formData.asins.length > 0 && (
{formData.asins.map((asin) => ( {asin} ))}
)}
{/* Timeline */}
setFormData({ ...formData, start_date: e.target.value })} className="w-full bg-slate-800 border border-slate-600 rounded-lg px-2 py-1.5 text-sm text-white focus:outline-none focus:ring-2 focus:ring-indigo-500" required />
setFormData({ ...formData, end_date: e.target.value })} className="w-full bg-slate-800 border border-slate-600 rounded-lg px-2 py-1.5 text-sm text-white focus:outline-none focus:ring-2 focus:ring-indigo-500" />
{/* Hypothesis */}