import React, { useState, useEffect } from 'react'; import { XIcon } from './icons/XIcon'; import apiService from '../services/apiService'; import { useUser } from '../contexts/UserContext'; interface CreateCampaignModalProps { isOpen: boolean; onClose: () => void; onAddCampaign: (campaignData: { name: string; workfrontId: string; clientLead: string; agencyLead: string; brandGuidelines: string; }) => void; brandGuidelines?: string[]; } export const CreateCampaignModal: React.FC = ({ isOpen, onClose, onAddCampaign, brandGuidelines: brandGuidelineOptions = [] }) => { const { user } = useUser(); const [name, setName] = useState(''); const [selectedBrandGuideline, setSelectedBrandGuideline] = useState(''); const [workfrontId, setWorkfrontId] = useState(''); const [clientLead, setClientLead] = useState(''); const [agencyLead, setAgencyLead] = useState(''); const [error, setError] = useState(null); useEffect(() => { // Reset form when modal opens if (isOpen) { setName(''); setSelectedBrandGuideline(''); setWorkfrontId(''); setClientLead(''); setAgencyLead(''); setError(null); } }, [isOpen]); const validateWorkfrontId = (id: string): boolean => { // Empty is valid (field is optional) if (id.length === 0) { setError(null); return true; } const isValid = /^#WF_\d+$/.test(id); if (!isValid) { setError("Workfront Campaign ID must be in the format '#WF_12345'"); } else { setError(null); } return isValid; }; const handleWorkfrontIdChange = (e: React.ChangeEvent) => { const newId = e.target.value; setWorkfrontId(newId); validateWorkfrontId(newId); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!name.trim() || !clientLead.trim() || !agencyLead.trim() || !selectedBrandGuideline.trim()) { return; } // Validate workfrontId format only if provided if (workfrontId.trim() && !/^#WF_\d+$/.test(workfrontId)) { setError("Workfront Campaign ID must be in the format '#WF_12345'"); return; } setError(null); onAddCampaign({ name, workfrontId: workfrontId.trim(), clientLead, agencyLead, brandGuidelines: selectedBrandGuideline }); onClose(); }; if (!isOpen) return null; const isFormInvalid = !name.trim() || !clientLead.trim() || !agencyLead.trim() || !selectedBrandGuideline.trim(); return (
e.stopPropagation()} >

Create New Campaign

setName(e.target.value)} className="mt-1 block w-full p-2 border-2 border-oliver-azure rounded-[10px] shadow-sm focus:ring-oliver-azure focus:border-oliver-azure transition bg-white text-oliver-black" required />
{error &&

{error}

}
setClientLead(e.target.value)} className="mt-1 block w-full p-2 border-2 border-oliver-azure rounded-[10px] shadow-sm focus:ring-oliver-azure focus:border-oliver-azure transition bg-white text-oliver-black" required />
setAgencyLead(e.target.value)} className="mt-1 block w-full p-2 border-2 border-oliver-azure rounded-[10px] shadow-sm focus:ring-oliver-azure focus:border-oliver-azure transition bg-white text-oliver-black" required />
); };