From fd96946cc1d8a034f42e2134368a3ec5f0c480ff Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Mon, 28 Sep 2026 16:48:43 +0530 Subject: [PATCH] subscription page fix --- src/app/subscription/index.tsx | 286 +++++++++++++-------------------- 1 file changed, 109 insertions(+), 177 deletions(-) diff --git a/src/app/subscription/index.tsx b/src/app/subscription/index.tsx index 6416c77..3e90f1e 100644 --- a/src/app/subscription/index.tsx +++ b/src/app/subscription/index.tsx @@ -1,13 +1,11 @@ -import React, { useCallback, useEffect, useMemo, useState } from "react"; +import React, { useCallback, useMemo, useState } from "react"; import { - ActivityIndicator, Alert, Linking, Platform, ScrollView, StyleSheet, Text, - TextInput, TouchableOpacity, View, } from "react-native"; @@ -19,26 +17,12 @@ import Constants from "expo-constants"; import { Colors } from "@/theme"; import { useAuthStore } from "@/store/auth.store"; -import { calculateCart } from "@/features/commerce"; -import type { CartCalculationResponse } from "@/features/commerce"; import PrimaryButton from "@/components/buttons/PrimaryButton"; function param(value: string | string[] | undefined): string { return typeof value === "string" ? value : ""; } -function money(value: number, currency: string): string { - try { - return new Intl.NumberFormat("en-IN", { - style: "currency", - currency, - maximumFractionDigits: 2, - }).format(value); - } catch { - return `${currency} ${value.toFixed(2)}`; - } -} - function resolveStorefrontUrl(): string { const envUrl = process.env.EXPO_PUBLIC_STOREFRONT_URL?.trim(); if (envUrl) return envUrl; @@ -78,15 +62,10 @@ export default function SubscriptionScreen() { const moduleId = param(params.moduleId); const moduleCode = param(params.moduleCode); const title = param(params.title) || "Clinical Module"; - const category = param(params.category); const description = param(params.desc); const planCode = param(params.planCode).trim().toUpperCase(); - const [couponCode, setCouponCode] = useState(""); - const [pricing, setPricing] = useState(null); - const [loadingPricing, setLoadingPricing] = useState(false); const [openingCheckout, setOpeningCheckout] = useState(false); - const [error, setError] = useState(null); const unlocked = useMemo(() => { if (!accessToken) return false; @@ -94,35 +73,6 @@ export default function SubscriptionScreen() { return false; }, [access, accessToken, moduleCode]); - const loadPricing = useCallback(async (coupon?: string) => { - if (!planCode) { - setPricing(null); - setError("This module does not have a commerce plan configured."); - return; - } - - setLoadingPricing(true); - setError(null); - try { - const response = await calculateCart({ - cart_item_codes: [planCode], - coupon_code: coupon?.trim() || null, - currency: "INR", - }); - setPricing(response); - } catch (err: any) { - const detail = err?.response?.data?.detail; - setError(typeof detail === "string" ? detail : "Unable to load current subscription pricing."); - setPricing(null); - } finally { - setLoadingPricing(false); - } - }, [planCode]); - - useEffect(() => { - void loadPricing(); - }, [loadPricing]); - useFocusEffect( useCallback(() => { if (accessToken) { @@ -160,7 +110,6 @@ export default function SubscriptionScreen() { const query = new URLSearchParams(); query.set("token", accessToken); query.set("planCode", planCode); - if (couponCode.trim()) query.set("couponCode", couponCode.trim().toUpperCase()); const apiUrl = process.env.EXPO_PUBLIC_API_URL; if (apiUrl) query.set("apiUrl", apiUrl); if (moduleId) query.set("moduleId", moduleId); @@ -180,102 +129,60 @@ export default function SubscriptionScreen() { }; return ( - + - router.back()} style={styles.backButton}> + router.back()} + style={styles.backButton} + hitSlop={{ top: 12, bottom: 12, left: 12, right: 12 }} + > - Subscription + + {title} + - - - + + + + + + + + {unlocked ? `${title} is Unlocked` : `Unlock ${title}`} + + + {description ? ( + {description} + ) : null} - {unlocked ? `${title} is unlocked` : `Unlock ${title}`} - - {unlocked - ? "Your current entitlement already includes this module." - : "Pricing and discounts below are calculated by the current commerce API."} - - - {(category || description) ? ( - - - - {category ? {category} : null} - {description ? {description} : null} - {planCode ? Plan: {planCode} : null} - - - ) : null} - - {!unlocked ? ( - <> - - {loadingPricing ? ( - - - Loading current pricing… - - ) : pricing ? ( - <> - {pricing.cart_items.map((item) => ( - - {item.name} - {money(item.price, pricing.currency)} - - ))} - - Subtotal{money(pricing.base_subtotal, pricing.currency)} - {pricing.total_discounts > 0 ? Discounts− {money(pricing.total_discounts, pricing.currency)} : null} - {pricing.convenience_fee > 0 ? Convenience fee{money(pricing.convenience_fee, pricing.currency)} : null} - GST{money(pricing.gst_amount, pricing.currency)} - - Total{money(pricing.grand_total, pricing.currency)} - {pricing.coupon_applied ? Coupon applied: {pricing.coupon_applied} : null} - - ) : ( - {error ?? "Pricing is unavailable."} - )} - - - - - void loadPricing(couponCode)} - disabled={loadingPricing} - > - Apply - - - - {error && pricing ? {error} : null} - - - {openingCheckout ? : ( - void handleCheckout()} - disabled={loadingPricing || !planCode} - /> - )} - - - ) : ( - - router.replace("/modules")} /> - - )} + + + {!unlocked ? ( + void handleCheckout()} + disabled={!planCode} + loading={openingCheckout} + /> + ) : ( + router.replace("/modules")} + /> + )} + ); } @@ -283,48 +190,73 @@ export default function SubscriptionScreen() { const styles = StyleSheet.create({ safeArea: { flex: 1, backgroundColor: "#FFFFFF" }, header: { - minHeight: 64, + height: moderateScale(56), flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: moderateScale(16), - paddingTop: Platform.OS === "web" ? 14 : moderateScale(8), - paddingBottom: moderateScale(8), borderBottomWidth: 1, - borderBottomColor: "#ECECEC", + borderBottomColor: "#F1F5F9", + backgroundColor: "#FFFFFF", + }, + backButton: { + width: moderateScale(36), + height: moderateScale(36), + alignItems: "center", + justifyContent: "center", + }, + headerTitle: { + flex: 1, + fontSize: moderateScale(17), + fontWeight: "700", + color: Colors.textPrimary, + textAlign: "center", + marginHorizontal: moderateScale(8), + }, + placeholder: { width: moderateScale(36) }, + scrollContent: { + flexGrow: 1, + justifyContent: "center", + alignItems: "center", + paddingHorizontal: moderateScale(24), + paddingVertical: verticalScale(24), + }, + centerContainer: { + alignItems: "center", + justifyContent: "center", + maxWidth: moderateScale(360), + width: "100%", + }, + iconCircle: { + width: moderateScale(68), + height: moderateScale(68), + borderRadius: moderateScale(34), + backgroundColor: "#E8F5E9", + alignItems: "center", + justifyContent: "center", + marginBottom: verticalScale(20), + }, + title: { + fontSize: moderateScale(22), + fontWeight: "800", + color: Colors.textPrimary, + textAlign: "center", + lineHeight: moderateScale(28), + }, + descriptionText: { + fontSize: moderateScale(15), + color: Colors.textSecondary, + textAlign: "center", + lineHeight: moderateScale(23), + marginTop: verticalScale(12), + paddingHorizontal: moderateScale(8), + }, + footer: { + paddingHorizontal: moderateScale(20), + paddingTop: verticalScale(12), + paddingBottom: Platform.OS === "ios" ? verticalScale(8) : verticalScale(16), + borderTopWidth: 1, + borderTopColor: "#F1F5F9", backgroundColor: "#FFFFFF", }, - backButton: { padding: 4 }, - headerTitle: { fontSize: moderateScale(18), fontWeight: "700", color: Colors.textPrimary }, - placeholder: { width: 32 }, - content: { paddingHorizontal: moderateScale(24), paddingTop: verticalScale(28), paddingBottom: verticalScale(40) }, - iconContainer: { alignItems: "center", marginBottom: verticalScale(16) }, - title: { fontSize: moderateScale(24), fontWeight: "800", color: Colors.textPrimary, textAlign: "center" }, - subtitle: { fontSize: moderateScale(14), color: Colors.textSecondary, textAlign: "center", lineHeight: 20, marginTop: verticalScale(8), marginBottom: verticalScale(22) }, - infoBox: { flexDirection: "row", backgroundColor: "#EEF2FF", borderRadius: moderateScale(14), padding: moderateScale(14), marginBottom: verticalScale(18) }, - infoText: { flex: 1, marginLeft: moderateScale(10) }, - infoTitle: { fontSize: moderateScale(14), fontWeight: "700", color: Colors.textPrimary }, - infoDescription: { marginTop: 3, fontSize: moderateScale(12), color: Colors.textSecondary, lineHeight: 18 }, - planCode: { marginTop: 5, fontSize: moderateScale(11), color: Colors.primary, fontWeight: "600" }, - pricingCard: { backgroundColor: "#FFFFFF", borderRadius: moderateScale(16), borderWidth: 1, borderColor: "#E5E7EB", padding: moderateScale(16) }, - loadingRow: { flexDirection: "row", alignItems: "center", justifyContent: "center", paddingVertical: verticalScale(16) }, - loadingText: { marginLeft: moderateScale(10), color: Colors.textSecondary, fontSize: moderateScale(13) }, - priceRow: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", marginVertical: verticalScale(5) }, - priceLabel: { flex: 1, fontSize: moderateScale(14), fontWeight: "700", color: Colors.textPrimary, marginRight: 10 }, - priceValue: { fontSize: moderateScale(14), fontWeight: "700", color: Colors.textPrimary }, - secondaryLabel: { fontSize: moderateScale(13), color: Colors.textSecondary }, - secondaryValue: { fontSize: moderateScale(13), color: Colors.textPrimary }, - discountLabel: { fontSize: moderateScale(13), color: "#15803D" }, - discountValue: { fontSize: moderateScale(13), color: "#15803D", fontWeight: "600" }, - totalLabel: { fontSize: moderateScale(15), fontWeight: "800", color: Colors.textPrimary }, - totalValue: { fontSize: moderateScale(18), fontWeight: "800", color: Colors.primary }, - divider: { height: 1, backgroundColor: "#ECECEC", marginVertical: verticalScale(8) }, - appliedText: { marginTop: verticalScale(8), fontSize: moderateScale(12), color: "#15803D", fontWeight: "600" }, - errorText: { fontSize: moderateScale(13), color: "#B91C1C", textAlign: "center", paddingVertical: verticalScale(14) }, - couponRow: { flexDirection: "row", marginTop: verticalScale(14) }, - couponInput: { flex: 1, minHeight: 46, backgroundColor: "#FFFFFF", borderWidth: 1, borderColor: "#D1D5DB", borderRadius: moderateScale(12), paddingHorizontal: moderateScale(12), color: Colors.textPrimary }, - applyButton: { marginLeft: moderateScale(10), minWidth: 78, minHeight: 46, alignItems: "center", justifyContent: "center", backgroundColor: "#EEF2FF", borderRadius: moderateScale(12) }, - applyText: { fontSize: moderateScale(13), fontWeight: "700", color: Colors.primary }, - inlineError: { marginTop: verticalScale(8), fontSize: moderateScale(12), color: "#B91C1C" }, - buttonContainer: { marginTop: verticalScale(24) }, }); -- 2.34.1