import React, { useEffect, useState } from "react"; import { View, Text, ScrollView, TouchableOpacity, ActivityIndicator, useColorScheme, } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; import { Ionicons } from "@expo/vector-icons"; import { Subject, subjectsApi } from "../services/api"; import { useAuth } from "../context/AuthContext"; export default function ManageSubscriptions({ onClose }: { onClose: () => void }) { const dark = useColorScheme() === "dark"; const { user, isSubscribed, subscribe, unsubscribe } = useAuth(); const accent = dark ? "#E07B45" : "#C4622D"; const [subjects, setSubjects] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); const [toggling, setToggling] = useState(null); useEffect(() => { subjectsApi.getAll(user?.token) .then(setSubjects) .catch(() => setError("Couldn't load subjects.")) .finally(() => setLoading(false)); }, []); const toggle = async (subject: Subject) => { setToggling(subject.id); try { if (isSubscribed(subject.id)) await unsubscribe(subject.id); else await subscribe(subject.id); } finally { setToggling(null); } }; return ( {/* Header */} Manage Subscriptions {/* Body */} {loading ? ( ) : error ? ( ⚠️ {error} ) : ( {subjects.map((subject, index) => { const subscribed = isSubscribed(subject.id); const busy = toggling === subject.id; const isLast = index === subjects.length - 1; return ( {/* Subscribed indicator dot */} {subject.name} toggle(subject)} disabled={!!toggling} className="px-3 py-1.5 rounded-xl" style={{ backgroundColor: subscribed ? dark ? "#2C2A27" : "#F0EDE8" : accent + "20", }} activeOpacity={0.7} > {busy ? ( ) : ( {subscribed ? "Unsubscribe" : "Subscribe"} )} ); })} )} ); }