From 242d1e6cc5dda32a5997fcb503a596dfd42df368 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 10:40:15 +0000 Subject: [PATCH] Changes Co-authored-by: wolfoftyreso-debug <250630591+wolfoftyreso-debug@users.noreply.github.com> --- src/App.tsx | 3 +- src/components/Header.tsx | 4 +- src/components/PlansSection.tsx | 110 ++++++++++++++++++++++---------- 3 files changed, 79 insertions(+), 38 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index dfaaed0..8fcb5ce 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -12,12 +12,11 @@ import AccountConfirmation from "./pages/AccountConfirmation"; import Contact from "./pages/Contact"; import Admin from "./pages/Admin"; import FAQ from "./pages/FAQ"; -import { useCartSync } from "./hooks/useCartSync"; const queryClient = new QueryClient(); function AppContent() { - useCartSync(); + return ( diff --git a/src/components/Header.tsx b/src/components/Header.tsx index 708ecbd..392c822 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -1,5 +1,4 @@ import AccountMenu from "./AccountMenu"; -import CartDrawer from "./CartDrawer"; const Header = () => { return ( @@ -13,7 +12,6 @@ const Header = () => { />
-
@@ -21,4 +19,4 @@ const Header = () => { ); }; -export default Header; \ No newline at end of file +export default Header; diff --git a/src/components/PlansSection.tsx b/src/components/PlansSection.tsx index e520482..e984e85 100644 --- a/src/components/PlansSection.tsx +++ b/src/components/PlansSection.tsx @@ -1,7 +1,11 @@ import { useState, useMemo } from "react"; -import { Loader2 } from "lucide-react"; +import { Loader2, CreditCard, FileText } from "lucide-react"; import { useShopifyProducts } from "@/hooks/useShopifyProducts"; -import { useCartStore } from "@/stores/cartStore"; +import { + storefrontApiRequest, + CART_CREATE_MUTATION, + formatCheckoutUrl, +} from "@/lib/shopify"; import { toast } from "sonner"; const PRICE_PER_KG = 325; @@ -19,12 +23,15 @@ const pickPlan = (kg: number) => { return match || availablePlans[availablePlans.length - 1]; }; +type PaymentMethod = "card" | "invoice"; + const PlansSection = () => { const [employees, setEmployees] = useState(10); - const [checkoutLoading, setCheckoutLoading] = useState(false); + const [loadingMethod, setLoadingMethod] = useState(null); - const { products, isLoading: productsLoading } = useShopifyProducts("product_type:Abonnemang"); - const { addItem, clearCart, isLoading: cartLoading } = useCartStore(); + const { products, isLoading: productsLoading } = useShopifyProducts( + "product_type:Abonnemang" + ); const recommendedKg = useMemo( () => Math.max(1, Math.round(employees * KG_PER_EMPLOYEE)), @@ -32,7 +39,7 @@ const PlansSection = () => { ); const weeklyPrice = recommendedKg * PRICE_PER_KG; - const handleStart = async () => { + const handleStart = async (method: PaymentMethod) => { const plan = pickPlan(recommendedKg); const shopifyProduct = products.find((p) => p.node.handle === plan.handle); @@ -46,29 +53,46 @@ const PlansSection = () => { return; } - setCheckoutLoading(true); + setLoadingMethod(method); try { - clearCart(); - await addItem({ - product: shopifyProduct, - variantId: variant.id, - variantTitle: variant.title, - price: variant.price, - quantity: 1, - selectedOptions: variant.selectedOptions || [], - }); - toast.success(`Abonnemang tillagt (${plan.name} – ${plan.kg} kg/vecka)`, { - description: "Klicka på varukorgen för att gå till betalning", + const data = await storefrontApiRequest(CART_CREATE_MUTATION, { + input: { + lines: [{ quantity: 1, merchandiseId: variant.id }], + attributes: [ + { key: "Antal anställda", value: String(employees) }, + { key: "Rekommenderad mängd", value: `${recommendedKg} kg/vecka` }, + { key: "Veckopris (exkl. moms)", value: `${weeklyPrice} kr` }, + { + key: "Önskad betalningsmetod", + value: method === "card" ? "Automatiskt kort varje månad" : "Månadsfaktura", + }, + ], + }, }); + + const errors = data?.data?.cartCreate?.userErrors || []; + if (errors.length > 0) { + console.error("Cart creation failed:", errors); + toast.error("Kunde inte skapa checkout"); + return; + } + + const checkoutUrl = data?.data?.cartCreate?.cart?.checkoutUrl; + if (!checkoutUrl) { + toast.error("Ingen checkout-länk mottagen"); + return; + } + + window.open(formatCheckoutUrl(checkoutUrl), "_blank"); } catch (e) { console.error(e); toast.error("Något gick fel"); } finally { - setCheckoutLoading(false); + setLoadingMethod(null); } }; - const isLoading = productsLoading || cartLoading || checkoutLoading; + const isBusy = productsLoading || loadingMethod !== null; return (
@@ -139,20 +163,40 @@ const PlansSection = () => {
exklusive moms
- + ) : ( + <> + + Automatiskt varje månad + + )} + + + +

+ Du slutför beställningen säkert hos Shopify. +