import { useCallback, useEffect, useState } from "react"; import { router } from "expo-router"; import { ActivityIndicator, Alert, Pressable, ScrollView, Text, View } from "react-native"; import { Ionicons } from "@expo/vector-icons"; import type { CustomerInfo, PurchasesOffering, PurchasesPackage } from "react-native-purchases"; import Purchases from "react-native-purchases"; import { getActiveRestaurant } from "@/lib/active-restaurant"; import { configurePurchases, getCurrentOffering, getCustomerInfo, isProActive, PRO_ENTITLEMENT_ID } from "@/lib/purchases"; export default function SubscriptionScreen() { const [loading, setLoading] = useState(true); const [offering, setOffering] = useState(null); const [customerInfo, setCustomerInfo] = useState(null); const [purchasingPackageId, setPurchasingPackageId] = useState(null); const [restoring, setRestoring] = useState(false); const load = useCallback(async () => { const active = await getActiveRestaurant(); if (!active) { router.back(); return; } configurePurchases(active.restaurantId); try { const [offer, info] = await Promise.all([getCurrentOffering(), getCustomerInfo()]); setOffering(offer); setCustomerInfo(info); } catch (err) { Alert.alert("Hata", err instanceof Error ? err.message : "Abonelik bilgisi yüklenemedi."); } finally { setLoading(false); } }, []); useEffect(() => { load(); }, [load]); async function onPurchase(pkg: PurchasesPackage) { setPurchasingPackageId(pkg.identifier); try { const { customerInfo: updated } = await Purchases.purchasePackage(pkg); setCustomerInfo(updated); if (isProActive(updated)) { Alert.alert("Teşekkürler!", "Menulio Pro aktif edildi."); } } catch (err) { const rcError = err as { userCancelled?: boolean; message?: string }; if (!rcError.userCancelled) { Alert.alert("Satın alma başarısız", rcError.message ?? "Bir hata oluştu."); } } finally { setPurchasingPackageId(null); } } async function onRestore() { setRestoring(true); try { const info = await Purchases.restorePurchases(); setCustomerInfo(info); Alert.alert( isProActive(info) ? "Abonelik bulundu" : "Aktif abonelik yok", isProActive(info) ? "Menulio Pro aboneliğiniz geri yüklendi." : "Bu hesaba bağlı aktif bir abonelik bulunamadı.", ); } catch (err) { Alert.alert("Hata", err instanceof Error ? err.message : "Abonelik geri yüklenemedi."); } finally { setRestoring(false); } } const isPro = customerInfo ? isProActive(customerInfo) : false; const expiresAt = customerInfo?.entitlements.active[PRO_ENTITLEMENT_ID]?.expirationDate; if (loading) { return ( ); } return ( router.back()} style={{ padding: 4 }}> Abonelik {isPro ? "Menulio Pro aktif" : "Ücretsiz plan"} {isPro && expiresAt ? ( Yenileme: {new Date(expiresAt).toLocaleDateString("tr-TR")} ) : null} {!offering || offering.availablePackages.length === 0 ? ( Şu anda satın alınabilir bir paket bulunamadı. ) : ( {offering.availablePackages.map((pkg) => ( onPurchase(pkg)} disabled={purchasingPackageId !== null} style={{ backgroundColor: "#FFFFFF", borderRadius: 16, padding: 16, borderWidth: 1.5, borderColor: "#C8A96B", flexDirection: "row", justifyContent: "space-between", alignItems: "center", opacity: purchasingPackageId && purchasingPackageId !== pkg.identifier ? 0.5 : 1, }} > {pkg.packageType === "ANNUAL" ? "Yıllık" : "Aylık"} {pkg.product.priceString} / {pkg.packageType === "ANNUAL" ? "yıl" : "ay"} {purchasingPackageId === pkg.identifier ? ( ) : ( )} ))} )} {restoring ? ( ) : ( Satın Alımları Geri Yükle )} ); }