From 25cc2227c5eef7eb40bee6b7e9fd076e71e7936f Mon Sep 17 00:00:00 2001 From: AyrisAI Date: Thu, 14 May 2026 22:45:52 +0300 Subject: [PATCH] Implement fetch-on-demand QR logic for WhatsApp --- app/[lang]/dashboard/settings/page.tsx | 45 +++++++++++++++++--------- app/api/whatsapp/qr/route.ts | 19 +++++++++++ 2 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 app/api/whatsapp/qr/route.ts diff --git a/app/[lang]/dashboard/settings/page.tsx b/app/[lang]/dashboard/settings/page.tsx index 8b3784c..cf059ae 100644 --- a/app/[lang]/dashboard/settings/page.tsx +++ b/app/[lang]/dashboard/settings/page.tsx @@ -11,6 +11,7 @@ export default function SettingsPage() { const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [saving, setSaving] = useState(false); + const [fetchingQr, setFetchingQr] = useState(false); const dict = useDictionary(); useEffect(() => { @@ -28,13 +29,9 @@ export default function SettingsPage() { try { const res = await fetch("/api/users/profile"); const data = await res.json(); - if (data.error) { - setError(data.error); - } else if (!data) { - setError("Kullanıcı profili bulunamadı."); - } else { - setProfile(data); - } + if (data.error) setError(data.error); + else if (!data) setError("Kullanıcı profili bulunamadı."); + else setProfile(data); } catch (e: any) { setError(e.message); } finally { @@ -52,6 +49,19 @@ export default function SettingsPage() { } }; + const handleConnectWa = async () => { + setFetchingQr(true); + try { + const res = await fetch("/api/whatsapp/qr"); + const data = await res.json(); + setWaStatus(data); + } catch (e) { + console.error(e); + } finally { + setFetchingQr(false); + } + }; + const handleSave = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); @@ -61,9 +71,8 @@ export default function SettingsPage() { headers: { "Content-Type": "application/json" }, body: JSON.stringify(profile) }); - if (res.ok) { - alert("Ayarlar kaydedildi!"); - } else { + if (res.ok) alert("Ayarlar kaydedildi!"); + else { const data = await res.json(); alert("Hata: " + data.error); } @@ -110,9 +119,6 @@ export default function SettingsPage() { onChange={e => setProfile({...profile, telegramId: e.target.value})} placeholder="Örn: 5009005027" /> -

- Botu başlatın ve ID'nizi buraya yazın. -

@@ -146,7 +152,7 @@ export default function SettingsPage() { {waStatus?.status === 'connected' ? (
- Bağlı + Bağlı ✅
) : (
@@ -154,11 +160,20 @@ export default function SettingsPage() {
Bağlı Değil
- {waStatus?.qr && ( + {waStatus?.qr ? (
QR Code

WhatsApp'tan okutun

+ ) : ( + )}
)} diff --git a/app/api/whatsapp/qr/route.ts b/app/api/whatsapp/qr/route.ts new file mode 100644 index 0000000..a48cac7 --- /dev/null +++ b/app/api/whatsapp/qr/route.ts @@ -0,0 +1,19 @@ +import { NextResponse } from "next/server"; +import { auth } from "@/auth"; + +export async function GET() { + const session = await auth(); + if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + + const userId = session.user.id; + const workerUrl = process.env.WHATSAPP_WORKER_URL; + const secret = process.env.WHATSAPP_SECRET; + + try { + const res = await fetch(`${workerUrl}/get-qr?userId=${userId}&secret=${secret}`); + const data = await res.json(); + return NextResponse.json(data); + } catch (error: any) { + return NextResponse.json({ status: 'error', error: error.message }); + } +}