import { useEffect, useMemo, useState } from 'react'; import apiClient from '../api/client'; function getMissingProviderFields(profile) { const provider = profile?.provider || {}; const missing = []; if (!provider.providerName) missing.push('providerName'); if (!provider.senderId) missing.push('senderId'); if (!provider.dltEntityId) missing.push('dltEntityId'); return missing; } export default function WhitelistModal({ businessId, template, boundProfile, onClose, onSuccess }) { const [profile, setProfile] = useState(boundProfile); const [providerForm, setProviderForm] = useState({ providerName: '', senderId: '', dltEntityId: '' }); const [templateId, setTemplateId] = useState(''); const [toNumber, setToNumber] = useState(''); const [savingProvider, setSavingProvider] = useState(false); const [publishing, setPublishing] = useState(false); const [error, setError] = useState(''); const [step, setStep] = useState('provider'); useEffect(() => { setProfile(boundProfile); setProviderForm({ providerName: boundProfile?.provider?.providerName || '', senderId: boundProfile?.provider?.senderId || '', dltEntityId: boundProfile?.provider?.dltEntityId || '', }); }, [boundProfile]); const missingFields = useMemo(() => getMissingProviderFields(profile), [profile]); useEffect(() => { if (!boundProfile) { setError('The cURL profile bound to this template is missing. Re-select the template from Events before publishing.'); setStep('provider'); return; } setError(''); setStep(missingFields.length > 0 ? 'provider' : 'publish'); }, [boundProfile, missingFields]); async function handleProviderSubmit(e) { e.preventDefault(); if (!profile?.id) return; setSavingProvider(true); setError(''); try { const res = await apiClient.patch( `/api/businesses/${businessId}/global-sms/profiles/${profile.id}`, { provider: { providerName: providerForm.providerName, senderId: providerForm.senderId.toUpperCase(), dltEntityId: providerForm.dltEntityId, }, } ); setProfile(res.data); setProviderForm({ providerName: res.data?.provider?.providerName || '', senderId: res.data?.provider?.senderId || '', dltEntityId: res.data?.provider?.dltEntityId || '', }); setStep(getMissingProviderFields(res.data).length > 0 ? 'provider' : 'publish'); } catch (err) { setError(err.response?.data?.error || 'Failed to save provider details'); } finally { setSavingProvider(false); } } async function handlePublish(e) { e.preventDefault(); if (!templateId.trim() || !toNumber.trim()) return; setPublishing(true); setError(''); try { await apiClient.post(`/api/businesses/${businessId}/templates/${template.eventSlug}/publish`, { templateId: templateId.trim(), toNumber: toNumber.trim(), }); await Promise.resolve(onSuccess()); } catch (err) { if (err.response?.data?.missingFields?.length) { setError(`Missing provider fields: ${err.response.data.missingFields.join(', ')}`); setStep('provider'); } else { setError(err.response?.data?.error || 'Failed to publish template'); } } finally { setPublishing(false); } } const isProfileMissing = !profile?.id; return (

{step === 'provider' ? 'Complete Provider Details' : 'Publish Template'}

{step === 'provider' ? 'Save the missing mandatory provider fields on the bound cURL profile before publishing.' : 'Provide the DLT template ID and destination number to complete publish.'}

{template.eventLabel || template.eventSlug.replace(/_/g, ' ')}

{profile && (

Bound Profile: {profile.name}

)} {error && (
{error}
)} {step === 'provider' ? (
{missingFields.includes('providerName') && (
setProviderForm(prev => ({ ...prev, providerName: e.target.value }))} className="w-full px-4 py-2 rounded-lg bg-page-bg border border-border-main text-text-primary placeholder-placeholder-bg focus:outline-none focus:ring-2 focus:ring-primary-blue text-sm" placeholder="e.g. MSG91" autoFocus required />
)} {missingFields.includes('senderId') && (
setProviderForm(prev => ({ ...prev, senderId: e.target.value.toUpperCase() }))} className="w-full px-4 py-2 rounded-lg bg-page-bg border border-border-main font-mono uppercase text-text-primary placeholder-placeholder-bg focus:outline-none focus:ring-2 focus:ring-primary-blue text-sm" placeholder="6 CHARS" maxLength={6} required />
)} {missingFields.includes('dltEntityId') && (
setProviderForm(prev => ({ ...prev, dltEntityId: e.target.value }))} className="w-full px-4 py-2 rounded-lg bg-page-bg border border-border-main font-mono text-text-primary placeholder-placeholder-bg focus:outline-none focus:ring-2 focus:ring-primary-blue text-sm" placeholder="19-digit DLT PE ID" required />
)}
) : (
setTemplateId(e.target.value)} placeholder="e.g. 1234567890987654321" className="w-full px-4 py-2 rounded-lg bg-page-bg border border-border-main font-mono text-text-primary placeholder-placeholder-bg focus:outline-none focus:ring-2 focus:ring-primary-blue text-sm" autoFocus required />
setToNumber(e.target.value)} placeholder="e.g. 919876543210" className="w-full px-4 py-2 rounded-lg bg-page-bg border border-border-main font-mono text-text-primary placeholder-placeholder-bg focus:outline-none focus:ring-2 focus:ring-primary-blue text-sm" required />

This sends the publish-triggering SMS request.

)}
); }