Compare commits
No commits in common. "9e516a32f9e7b33c135cc472642095b7d85cb3e9" and "cdb6665ac7338e107e4af884366fae18bea9f031" have entirely different histories.
9e516a32f9
...
cdb6665ac7
|
|
@ -1,11 +1,13 @@
|
||||||
import React, { useCallback, useMemo, useState } from "react";
|
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
||||||
import {
|
import {
|
||||||
|
ActivityIndicator,
|
||||||
Alert,
|
Alert,
|
||||||
Linking,
|
Linking,
|
||||||
Platform,
|
Platform,
|
||||||
ScrollView,
|
ScrollView,
|
||||||
StyleSheet,
|
StyleSheet,
|
||||||
Text,
|
Text,
|
||||||
|
TextInput,
|
||||||
TouchableOpacity,
|
TouchableOpacity,
|
||||||
View,
|
View,
|
||||||
} from "react-native";
|
} from "react-native";
|
||||||
|
|
@ -17,12 +19,26 @@ import Constants from "expo-constants";
|
||||||
|
|
||||||
import { Colors } from "@/theme";
|
import { Colors } from "@/theme";
|
||||||
import { useAuthStore } from "@/store/auth.store";
|
import { useAuthStore } from "@/store/auth.store";
|
||||||
|
import { calculateCart } from "@/features/commerce";
|
||||||
|
import type { CartCalculationResponse } from "@/features/commerce";
|
||||||
import PrimaryButton from "@/components/buttons/PrimaryButton";
|
import PrimaryButton from "@/components/buttons/PrimaryButton";
|
||||||
|
|
||||||
function param(value: string | string[] | undefined): string {
|
function param(value: string | string[] | undefined): string {
|
||||||
return typeof value === "string" ? value : "";
|
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 {
|
function resolveStorefrontUrl(): string {
|
||||||
const envUrl = process.env.EXPO_PUBLIC_STOREFRONT_URL?.trim();
|
const envUrl = process.env.EXPO_PUBLIC_STOREFRONT_URL?.trim();
|
||||||
if (envUrl) return envUrl;
|
if (envUrl) return envUrl;
|
||||||
|
|
@ -62,10 +78,15 @@ export default function SubscriptionScreen() {
|
||||||
const moduleId = param(params.moduleId);
|
const moduleId = param(params.moduleId);
|
||||||
const moduleCode = param(params.moduleCode);
|
const moduleCode = param(params.moduleCode);
|
||||||
const title = param(params.title) || "Clinical Module";
|
const title = param(params.title) || "Clinical Module";
|
||||||
|
const category = param(params.category);
|
||||||
const description = param(params.desc);
|
const description = param(params.desc);
|
||||||
const planCode = param(params.planCode).trim().toUpperCase();
|
const planCode = param(params.planCode).trim().toUpperCase();
|
||||||
|
|
||||||
|
const [couponCode, setCouponCode] = useState("");
|
||||||
|
const [pricing, setPricing] = useState<CartCalculationResponse | null>(null);
|
||||||
|
const [loadingPricing, setLoadingPricing] = useState(false);
|
||||||
const [openingCheckout, setOpeningCheckout] = useState(false);
|
const [openingCheckout, setOpeningCheckout] = useState(false);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
const unlocked = useMemo(() => {
|
const unlocked = useMemo(() => {
|
||||||
if (!accessToken) return false;
|
if (!accessToken) return false;
|
||||||
|
|
@ -73,6 +94,35 @@ export default function SubscriptionScreen() {
|
||||||
return false;
|
return false;
|
||||||
}, [access, accessToken, moduleCode]);
|
}, [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(
|
useFocusEffect(
|
||||||
useCallback(() => {
|
useCallback(() => {
|
||||||
if (accessToken) {
|
if (accessToken) {
|
||||||
|
|
@ -110,6 +160,7 @@ export default function SubscriptionScreen() {
|
||||||
const query = new URLSearchParams();
|
const query = new URLSearchParams();
|
||||||
query.set("token", accessToken);
|
query.set("token", accessToken);
|
||||||
query.set("planCode", planCode);
|
query.set("planCode", planCode);
|
||||||
|
if (couponCode.trim()) query.set("couponCode", couponCode.trim().toUpperCase());
|
||||||
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
|
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
|
||||||
if (apiUrl) query.set("apiUrl", apiUrl);
|
if (apiUrl) query.set("apiUrl", apiUrl);
|
||||||
if (moduleId) query.set("moduleId", moduleId);
|
if (moduleId) query.set("moduleId", moduleId);
|
||||||
|
|
@ -129,60 +180,102 @@ export default function SubscriptionScreen() {
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SafeAreaView style={styles.safeArea} edges={["top", "bottom", "left", "right"]}>
|
<SafeAreaView style={styles.safeArea} edges={["top", "left", "right"]}>
|
||||||
<View style={styles.header}>
|
<View style={styles.header}>
|
||||||
<TouchableOpacity
|
<TouchableOpacity onPress={() => router.back()} style={styles.backButton}>
|
||||||
onPress={() => router.back()}
|
|
||||||
style={styles.backButton}
|
|
||||||
hitSlop={{ top: 12, bottom: 12, left: 12, right: 12 }}
|
|
||||||
>
|
|
||||||
<Ionicons name="chevron-back" size={24} color={Colors.textPrimary} />
|
<Ionicons name="chevron-back" size={24} color={Colors.textPrimary} />
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
<Text style={styles.headerTitle} numberOfLines={1}>
|
<Text style={styles.headerTitle}>Subscription</Text>
|
||||||
{title}
|
|
||||||
</Text>
|
|
||||||
<View style={styles.placeholder} />
|
<View style={styles.placeholder} />
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
<ScrollView
|
<ScrollView contentContainerStyle={styles.content} showsVerticalScrollIndicator={false}>
|
||||||
contentContainerStyle={styles.scrollContent}
|
<View style={styles.iconContainer}>
|
||||||
showsVerticalScrollIndicator={false}
|
<Ionicons name={unlocked ? "checkmark-circle-outline" : "card-outline"} size={58} color={Colors.primary} />
|
||||||
bounces={false}
|
|
||||||
>
|
|
||||||
<View style={styles.centerContainer}>
|
|
||||||
<View style={styles.iconCircle}>
|
|
||||||
<Ionicons
|
|
||||||
name={unlocked ? "checkmark-circle" : "lock-closed"}
|
|
||||||
size={moderateScale(32)}
|
|
||||||
color={Colors.primary}
|
|
||||||
/>
|
|
||||||
</View>
|
|
||||||
|
|
||||||
<Text style={styles.title}>
|
|
||||||
{unlocked ? `${title} is Unlocked` : `Unlock ${title}`}
|
|
||||||
</Text>
|
|
||||||
|
|
||||||
{description ? (
|
|
||||||
<Text style={styles.descriptionText}>{description}</Text>
|
|
||||||
) : null}
|
|
||||||
</View>
|
</View>
|
||||||
</ScrollView>
|
<Text style={styles.title}>{unlocked ? `${title} is unlocked` : `Unlock ${title}`}</Text>
|
||||||
|
<Text style={styles.subtitle}>
|
||||||
|
{unlocked
|
||||||
|
? "Your current entitlement already includes this module."
|
||||||
|
: "Pricing and discounts below are calculated by the current commerce API."}
|
||||||
|
</Text>
|
||||||
|
|
||||||
|
{(category || description) ? (
|
||||||
|
<View style={styles.infoBox}>
|
||||||
|
<Ionicons name="information-circle-outline" size={20} color={Colors.primary} />
|
||||||
|
<View style={styles.infoText}>
|
||||||
|
{category ? <Text style={styles.infoTitle}>{category}</Text> : null}
|
||||||
|
{description ? <Text style={styles.infoDescription}>{description}</Text> : null}
|
||||||
|
{planCode ? <Text style={styles.planCode}>Plan: {planCode}</Text> : null}
|
||||||
|
</View>
|
||||||
|
</View>
|
||||||
|
) : null}
|
||||||
|
|
||||||
<View style={styles.footer}>
|
|
||||||
{!unlocked ? (
|
{!unlocked ? (
|
||||||
<PrimaryButton
|
<>
|
||||||
title={accessToken ? "Continue to Checkout" : "Log In to Subscribe"}
|
<View style={styles.pricingCard}>
|
||||||
onPress={() => void handleCheckout()}
|
{loadingPricing ? (
|
||||||
disabled={!planCode}
|
<View style={styles.loadingRow}>
|
||||||
loading={openingCheckout}
|
<ActivityIndicator color={Colors.primary} />
|
||||||
/>
|
<Text style={styles.loadingText}>Loading current pricing…</Text>
|
||||||
|
</View>
|
||||||
|
) : pricing ? (
|
||||||
|
<>
|
||||||
|
{pricing.cart_items.map((item) => (
|
||||||
|
<View key={item.code} style={styles.priceRow}>
|
||||||
|
<Text style={styles.priceLabel}>{item.name}</Text>
|
||||||
|
<Text style={styles.priceValue}>{money(item.price, pricing.currency)}</Text>
|
||||||
|
</View>
|
||||||
|
))}
|
||||||
|
<View style={styles.divider} />
|
||||||
|
<View style={styles.priceRow}><Text style={styles.secondaryLabel}>Subtotal</Text><Text style={styles.secondaryValue}>{money(pricing.base_subtotal, pricing.currency)}</Text></View>
|
||||||
|
{pricing.total_discounts > 0 ? <View style={styles.priceRow}><Text style={styles.discountLabel}>Discounts</Text><Text style={styles.discountValue}>− {money(pricing.total_discounts, pricing.currency)}</Text></View> : null}
|
||||||
|
{pricing.convenience_fee > 0 ? <View style={styles.priceRow}><Text style={styles.secondaryLabel}>Convenience fee</Text><Text style={styles.secondaryValue}>{money(pricing.convenience_fee, pricing.currency)}</Text></View> : null}
|
||||||
|
<View style={styles.priceRow}><Text style={styles.secondaryLabel}>GST</Text><Text style={styles.secondaryValue}>{money(pricing.gst_amount, pricing.currency)}</Text></View>
|
||||||
|
<View style={styles.divider} />
|
||||||
|
<View style={styles.priceRow}><Text style={styles.totalLabel}>Total</Text><Text style={styles.totalValue}>{money(pricing.grand_total, pricing.currency)}</Text></View>
|
||||||
|
{pricing.coupon_applied ? <Text style={styles.appliedText}>Coupon applied: {pricing.coupon_applied}</Text> : null}
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<Text style={styles.errorText}>{error ?? "Pricing is unavailable."}</Text>
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
|
|
||||||
|
<View style={styles.couponRow}>
|
||||||
|
<TextInput
|
||||||
|
value={couponCode}
|
||||||
|
onChangeText={setCouponCode}
|
||||||
|
autoCapitalize="characters"
|
||||||
|
placeholder="Coupon code"
|
||||||
|
style={styles.couponInput}
|
||||||
|
/>
|
||||||
|
<TouchableOpacity
|
||||||
|
style={styles.applyButton}
|
||||||
|
onPress={() => void loadPricing(couponCode)}
|
||||||
|
disabled={loadingPricing}
|
||||||
|
>
|
||||||
|
<Text style={styles.applyText}>Apply</Text>
|
||||||
|
</TouchableOpacity>
|
||||||
|
</View>
|
||||||
|
|
||||||
|
{error && pricing ? <Text style={styles.inlineError}>{error}</Text> : null}
|
||||||
|
|
||||||
|
<View style={styles.buttonContainer}>
|
||||||
|
{openingCheckout ? <ActivityIndicator color={Colors.primary} /> : (
|
||||||
|
<PrimaryButton
|
||||||
|
title={accessToken ? "Continue to Checkout" : "Log In to Subscribe"}
|
||||||
|
onPress={() => void handleCheckout()}
|
||||||
|
disabled={loadingPricing || !planCode}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
|
</>
|
||||||
) : (
|
) : (
|
||||||
<PrimaryButton
|
<View style={styles.buttonContainer}>
|
||||||
title="Back to Modules"
|
<PrimaryButton title="Back to Modules" onPress={() => router.replace("/modules")} />
|
||||||
onPress={() => router.replace("/modules")}
|
</View>
|
||||||
/>
|
|
||||||
)}
|
)}
|
||||||
</View>
|
</ScrollView>
|
||||||
</SafeAreaView>
|
</SafeAreaView>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -190,73 +283,48 @@ export default function SubscriptionScreen() {
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
safeArea: { flex: 1, backgroundColor: "#FFFFFF" },
|
safeArea: { flex: 1, backgroundColor: "#FFFFFF" },
|
||||||
header: {
|
header: {
|
||||||
height: moderateScale(56),
|
minHeight: 64,
|
||||||
flexDirection: "row",
|
flexDirection: "row",
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
justifyContent: "space-between",
|
justifyContent: "space-between",
|
||||||
paddingHorizontal: moderateScale(16),
|
paddingHorizontal: moderateScale(16),
|
||||||
|
paddingTop: Platform.OS === "web" ? 14 : moderateScale(8),
|
||||||
|
paddingBottom: moderateScale(8),
|
||||||
borderBottomWidth: 1,
|
borderBottomWidth: 1,
|
||||||
borderBottomColor: "#F1F5F9",
|
borderBottomColor: "#ECECEC",
|
||||||
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",
|
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) },
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue