import { useEffect, useMemo, useState } from 'react'; import apiClient from '../api/client'; const BASE_PROFILE_KEYS = new Set(['providerName', 'senderId', 'dltEntityId']); function buildProfilePatchPayload(inputs = [], values = {}) { const provider = {}; const profileInputValues = {}; inputs.forEach((input) => { const rawValue = String(values[input.key] ?? '').trim(); if (!rawValue) return; if (BASE_PROFILE_KEYS.has(input.key)) { provider[input.key] = input.key === 'senderId' ? rawValue.toUpperCase() : rawValue; return; } profileInputValues[input.key] = rawValue; }); return { ...(Object.keys(provider).length > 0 ? { provider } : {}), ...(Object.keys(profileInputValues).length > 0 ? { profileInputValues } : {}), }; } function getInitialValues(inputs = []) { return inputs.reduce((accumulator, input) => { accumulator[input.key] = input.value || ''; return accumulator; }, {}); } export default function WhitelistModal({ businessId, template, boundProfile, onClose, onSuccess }) { const [profile, setProfile] = useState(boundProfile); const [profileForm, setProfileForm] = useState({}); const [templateId, setTemplateId] = useState(''); const [toNumber, setToNumber] = useState(''); const [savingProfile, setSavingProfile] = useState(false); const [publishing, setPublishing] = useState(false); const [error, setError] = useState(''); const [step, setStep] = useState('profile'); const missingInputs = useMemo( () => profile?.executionReadiness?.missingProfileInputs || [], [profile], ); useEffect(() => { setProfile(boundProfile); }, [boundProfile]); useEffect(() => { setProfileForm(getInitialValues(missingInputs)); }, [missingInputs]); useEffect(() => { if (!boundProfile) { setError('The cURL profile bound to this template is missing. Re-select the template from Events before publishing.'); setStep('profile'); return; } setError(''); setStep(missingInputs.length > 0 ? 'profile' : 'publish'); }, [boundProfile, missingInputs]); async function handleProfileSubmit(event) { event.preventDefault(); if (!profile?.id || missingInputs.length === 0) return; setSavingProfile(true); setError(''); try { const payload = buildProfilePatchPayload(missingInputs, profileForm); const res = await apiClient.patch( `/api/businesses/${businessId}/global-sms/profiles/${profile.id}`, payload, ); setProfile(res.data); setStep(res.data?.executionReadiness?.missingProfileInputs?.length > 0 ? 'profile' : 'publish'); } catch (err) { setError(err.response?.data?.error || 'Failed to save required profile fields'); } finally { setSavingProfile(false); } } async function handlePublish(event) { event.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 profile fields: ${err.response.data.missingFields.join(', ')}`); setStep('profile'); } else { setError(err.response?.data?.error || 'Failed to publish template'); } } finally { setPublishing(false); } } const isProfileMissing = !profile?.id; return (

{step === 'profile' ? 'Complete Profile Setup' : 'Publish Template'}

{step === 'profile' ? 'Complete the required 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 === 'profile' ? (
{missingInputs.map((input) => (
setProfileForm((current) => ({ ...current, [input.key]: input.key === 'senderId' ? event.target.value.toUpperCase() : event.target.value, }))} className="w-full rounded-lg border border-border-main bg-page-bg px-4 py-2 text-sm text-text-primary focus:outline-none focus:ring-2 focus:ring-primary-blue" placeholder={input.label} required={input.required !== false} autoFocus={input.key === missingInputs[0]?.key} />
))}
) : (
setTemplateId(event.target.value)} placeholder="e.g. 1234567890987654321" className="w-full rounded-lg border border-border-main bg-page-bg px-4 py-2 font-mono text-sm text-text-primary focus:outline-none focus:ring-2 focus:ring-primary-blue" autoFocus required />
setToNumber(event.target.value)} placeholder="e.g. 919876543210" className="w-full rounded-lg border border-border-main bg-page-bg px-4 py-2 font-mono text-sm text-text-primary focus:outline-none focus:ring-2 focus:ring-primary-blue" required />

This sends the publish-triggering SMS request.

)}
); }