From 6b1ea0698fe9e45e05afb33bff3fb1f991640073 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sat, 11 Jul 2026 18:20:30 +0000 Subject: [PATCH] Changes Co-authored-by: wolfoftyreso-debug <250630591+wolfoftyreso-debug@users.noreply.github.com> --- src/pages/Account.tsx | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/pages/Account.tsx b/src/pages/Account.tsx index 6de65d8..0a40fdc 100644 --- a/src/pages/Account.tsx +++ b/src/pages/Account.tsx @@ -100,9 +100,57 @@ const Account = () => { if (user) { fetchProfile(); + fetchSubscription(); } }, [user, authLoading, navigate]); + const fetchSubscription = async () => { + if (!user) return; + try { + const env = hasPaymentsConfigured() ? getStripeEnvironment() : "sandbox"; + const { data } = await supabase + .from("subscriptions") + .select("id, status, kg_per_week, monthly_amount, current_period_end, cancel_at_period_end, collection_method") + .eq("user_id", user.id) + .eq("environment", env) + .order("created_at", { ascending: false }) + .limit(1) + .maybeSingle(); + setSubscription((data as SubscriptionRow | null) ?? null); + } catch (e) { + console.error("fetchSubscription error:", e); + } + }; + + const handleManageSubscription = async () => { + if (!hasPaymentsConfigured()) { + toast({ title: "Betalningar inte konfigurerade", variant: "destructive" }); + return; + } + setOpeningPortal(true); + try { + const { data, error } = await supabase.functions.invoke("create-portal-session", { + body: { + return_url: `${window.location.origin}/account`, + environment: getStripeEnvironment(), + }, + }); + if (error) throw error; + if ((data as any)?.error) throw new Error((data as any).error); + const url = (data as any)?.url; + if (!url) throw new Error("Ingen portal-URL mottagen"); + window.open(url, "_blank", "noopener,noreferrer"); + } catch (e: any) { + toast({ + title: "Kunde inte öppna kundportalen", + description: e?.message ?? String(e), + variant: "destructive", + }); + } finally { + setOpeningPortal(false); + } + }; + const fetchProfile = async () => { try { const { data, error } = await supabase