|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useTransition } from "react"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import { updateCampaign, updateCreatives, uploadAdAsset } from "@/app/actions/ads"; |
| 6 | +import { AD_FORMATS, type AdCreative, type AdFormatId } from "@/lib/ads/formats"; |
| 7 | +import { AdPreview } from "@/components/ads/ad-preview"; |
| 8 | + |
| 9 | +type Campaign = { |
| 10 | + id: string; |
| 11 | + name: string; |
| 12 | + destinationUrl: string; |
| 13 | + dailyBudgetCents: number; |
| 14 | + bidCredits: number; |
| 15 | + status: string; |
| 16 | +}; |
| 17 | + |
| 18 | +export function EditCampaignForm({ |
| 19 | + campaign, |
| 20 | + creatives: initial, |
| 21 | +}: { |
| 22 | + campaign: Campaign; |
| 23 | + creatives: (AdCreative & { id: string })[]; |
| 24 | +}) { |
| 25 | + const router = useRouter(); |
| 26 | + const [saving, startSave] = useTransition(); |
| 27 | + const [error, setError] = useState<string | null>(null); |
| 28 | + const [saved, setSaved] = useState(false); |
| 29 | + |
| 30 | + const [name, setName] = useState(campaign.name); |
| 31 | + const [url, setUrl] = useState(campaign.destinationUrl); |
| 32 | + const [budget, setBudget] = useState(campaign.dailyBudgetCents / 100); |
| 33 | + const [bid, setBid] = useState((campaign.bidCredits * 5) / 100); // credits → $ |
| 34 | + const [creatives, setCreatives] = useState(initial); |
| 35 | + const [active, setActive] = useState<AdFormatId>(initial[0]?.format ?? "banner_300x250"); |
| 36 | + const [uploading, setUploading] = useState(false); |
| 37 | + |
| 38 | + const current = creatives.find((c) => c.format === active) ?? creatives[0]; |
| 39 | + |
| 40 | + function patchActive(patch: Partial<AdCreative>) { |
| 41 | + setCreatives((cs) => cs.map((c) => (c.format === active ? { ...c, ...patch } : c))); |
| 42 | + } |
| 43 | + function patchAll(patch: Partial<AdCreative>) { |
| 44 | + setCreatives((cs) => cs.map((c) => ({ ...c, ...patch }))); |
| 45 | + } |
| 46 | + |
| 47 | + async function onUploadLogo(e: React.ChangeEvent<HTMLInputElement>) { |
| 48 | + const file = e.target.files?.[0]; |
| 49 | + if (!file) return; |
| 50 | + setUploading(true); |
| 51 | + const fd = new FormData(); |
| 52 | + fd.set("file", file); |
| 53 | + const res = await uploadAdAsset(fd); |
| 54 | + setUploading(false); |
| 55 | + if (!res.ok) return setError(res.error); |
| 56 | + patchAll({ logoUrl: res.url }); |
| 57 | + } |
| 58 | + |
| 59 | + function save() { |
| 60 | + setError(null); |
| 61 | + setSaved(false); |
| 62 | + startSave(async () => { |
| 63 | + const s = await updateCampaign({ |
| 64 | + id: campaign.id, |
| 65 | + name, |
| 66 | + destinationUrl: url, |
| 67 | + dailyBudgetCents: Math.round(budget * 100), |
| 68 | + bidCredits: Math.max(1, Math.round((bid * 100) / 5)), |
| 69 | + }); |
| 70 | + if (!s.ok) return setError(s.error); |
| 71 | + const c = await updateCreatives({ |
| 72 | + campaignId: campaign.id, |
| 73 | + creatives: creatives.map((cr) => ({ ...cr })), |
| 74 | + }); |
| 75 | + if (!c.ok) return setError(c.error); |
| 76 | + setSaved(true); |
| 77 | + router.refresh(); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + return ( |
| 82 | + <div className="mt-6 space-y-6"> |
| 83 | + {error && <div className="card border-red-500/40 p-3 text-sm text-red-400">{error}</div>} |
| 84 | + {saved && <div className="card border-[var(--color-accent)]/40 p-3 text-sm text-[var(--color-accent)]">Saved.</div>} |
| 85 | + |
| 86 | + {/* Settings */} |
| 87 | + <div className="card space-y-4 p-5"> |
| 88 | + <h2 className="font-semibold">Settings</h2> |
| 89 | + <div className="grid gap-4 sm:grid-cols-2"> |
| 90 | + <label className="block"> |
| 91 | + <span className="text-xs uppercase tracking-wider text-[var(--color-muted)]">Name</span> |
| 92 | + <input className="input mt-1" value={name} onChange={(e) => setName(e.target.value)} /> |
| 93 | + </label> |
| 94 | + <label className="block"> |
| 95 | + <span className="text-xs uppercase tracking-wider text-[var(--color-muted)]">Destination URL</span> |
| 96 | + <input className="input mt-1" type="url" value={url} onChange={(e) => setUrl(e.target.value)} /> |
| 97 | + </label> |
| 98 | + <label className="block"> |
| 99 | + <span className="text-xs uppercase tracking-wider text-[var(--color-muted)]">Daily budget ($)</span> |
| 100 | + <input |
| 101 | + className="input mt-1" |
| 102 | + type="number" |
| 103 | + min={1} |
| 104 | + step={1} |
| 105 | + value={budget} |
| 106 | + onChange={(e) => setBudget(Math.max(0, Number(e.target.value)))} |
| 107 | + /> |
| 108 | + </label> |
| 109 | + <label className="block"> |
| 110 | + <span className="text-xs uppercase tracking-wider text-[var(--color-muted)]">Max bid / click ($)</span> |
| 111 | + <input |
| 112 | + className="input mt-1" |
| 113 | + type="number" |
| 114 | + min={0.05} |
| 115 | + step={0.05} |
| 116 | + value={bid} |
| 117 | + onChange={(e) => setBid(Math.max(0.05, Number(e.target.value)))} |
| 118 | + /> |
| 119 | + </label> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + |
| 123 | + {/* Creative editor */} |
| 124 | + {current && ( |
| 125 | + <div className="card space-y-4 p-5"> |
| 126 | + <div className="flex flex-wrap items-start gap-4"> |
| 127 | + {AD_FORMATS.map((f) => { |
| 128 | + const c = creatives.find((x) => x.format === f.id); |
| 129 | + if (!c) return null; |
| 130 | + return ( |
| 131 | + <button |
| 132 | + key={f.id} |
| 133 | + onClick={() => setActive(f.id)} |
| 134 | + className={`rounded-lg p-2 ${active === f.id ? "ring-2 ring-[var(--color-accent)]" : "opacity-80 hover:opacity-100"}`} |
| 135 | + style={{ maxWidth: f.w > 360 ? 360 : f.w + 16 }} |
| 136 | + > |
| 137 | + <div style={{ overflowX: "auto" }}> |
| 138 | + <AdPreview creative={c} /> |
| 139 | + </div> |
| 140 | + </button> |
| 141 | + ); |
| 142 | + })} |
| 143 | + </div> |
| 144 | + |
| 145 | + <div className="grid gap-4 sm:grid-cols-2"> |
| 146 | + <label className="block"> |
| 147 | + <span className="text-xs uppercase tracking-wider text-[var(--color-muted)]">Headline</span> |
| 148 | + <input className="input mt-1" value={current.headline} maxLength={80} onChange={(e) => patchActive({ headline: e.target.value })} /> |
| 149 | + </label> |
| 150 | + <label className="block"> |
| 151 | + <span className="text-xs uppercase tracking-wider text-[var(--color-muted)]">CTA</span> |
| 152 | + <input className="input mt-1" value={current.ctaText} maxLength={24} onChange={(e) => patchActive({ ctaText: e.target.value })} /> |
| 153 | + </label> |
| 154 | + <label className="block sm:col-span-2"> |
| 155 | + <span className="text-xs uppercase tracking-wider text-[var(--color-muted)]">Body</span> |
| 156 | + <input className="input mt-1" value={current.body} maxLength={140} onChange={(e) => patchActive({ body: e.target.value })} /> |
| 157 | + </label> |
| 158 | + </div> |
| 159 | + |
| 160 | + <div className="flex flex-wrap items-center gap-5"> |
| 161 | + <ColorField label="Background" value={current.bgColor} onChange={(v) => patchAll({ bgColor: v })} /> |
| 162 | + <ColorField label="Text" value={current.fgColor} onChange={(v) => patchAll({ fgColor: v })} /> |
| 163 | + <ColorField label="Accent" value={current.accentColor} onChange={(v) => patchAll({ accentColor: v })} /> |
| 164 | + <label className="btn cursor-pointer text-sm"> |
| 165 | + {uploading ? "Uploading…" : current.logoUrl ? "Replace logo" : "Upload logo"} |
| 166 | + <input type="file" accept="image/png,image/jpeg,image/webp,image/svg+xml" className="hidden" onChange={onUploadLogo} disabled={uploading} /> |
| 167 | + </label> |
| 168 | + </div> |
| 169 | + </div> |
| 170 | + )} |
| 171 | + |
| 172 | + <div className="flex justify-end"> |
| 173 | + <button className="btn btn-primary" onClick={save} disabled={saving}> |
| 174 | + {saving ? "Saving…" : "Save changes"} |
| 175 | + </button> |
| 176 | + </div> |
| 177 | + </div> |
| 178 | + ); |
| 179 | +} |
| 180 | + |
| 181 | +function ColorField({ label, value, onChange }: { label: string; value: string; onChange: (v: string) => void }) { |
| 182 | + return ( |
| 183 | + <label className="flex items-center gap-2"> |
| 184 | + <input type="color" value={value} onChange={(e) => onChange(e.target.value)} className="h-8 w-8 cursor-pointer rounded border border-[var(--color-border)] bg-transparent" aria-label={label} /> |
| 185 | + <span className="text-xs text-[var(--color-muted)]"> |
| 186 | + {label} |
| 187 | + <br /> |
| 188 | + <span className="font-mono">{value}</span> |
| 189 | + </span> |
| 190 | + </label> |
| 191 | + ); |
| 192 | +} |
0 commit comments