import { View, Text, ScrollView, ActivityIndicator, Animated } from 'react-native' import React, { useState, useEffect, useRef } from 'react' import { TouchableOpacity } from 'react-native'; import { ChevronDownIcon, ChevronUpIcon } from 'react-native-heroicons/outline'; import { getFAQs, getFAQCategories } from '../api'; export default function FaqCenter() { const [categories, setCategories] = useState(['All', 'Services', 'General', 'Account']); const [activeCategory, setActiveCategory] = useState('All'); const [faqs, setFaqs] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [expandedFaqs, setExpandedFaqs] = useState({}); const animations = useRef({}); useEffect(() => { fetchCategories(); }, []); useEffect(() => { fetchFAQs(); }, [activeCategory]); const fetchCategories = async () => { try { const data = await getFAQCategories(); setCategories(data); } catch (err) { console.error('Error fetching categories:', err); } }; const fetchFAQs = async () => { try { setLoading(true); const data = await getFAQs(activeCategory); setFaqs(data); const initialExpandedState = {}; data.forEach(faq => { initialExpandedState[faq.id] = false; animations.current[faq.id] = new Animated.Value(0); }); setExpandedFaqs(initialExpandedState); } catch (err) { setError('Failed to load FAQs'); console.error('Error fetching FAQs:', err); } finally { setLoading(false); } }; const toggleFaq = (faqId) => { const isExpanded = !expandedFaqs[faqId]; setExpandedFaqs(prev => ({ ...prev, [faqId]: isExpanded })); Animated.timing(animations.current[faqId], { toValue: isExpanded ? 1 : 0, duration: 300, useNativeDriver: false, }).start(); }; return ( {categories.map((category, index) => { const isActive = category === activeCategory; return ( setActiveCategory(category)} className={` border border-gray-200 bg-gray-200 px-5 mr-4 rounded-full py-3 ${isActive ? "bg-primary" : ""} `}> {category} ); })} {loading ? ( ) : error ? ( {error} ) : faqs.length > 0 ? ( faqs.map((faq) => ( toggleFaq(faq.id)} className="flex-row justify-between items-center p-4"> {faq.question} {expandedFaqs[faq.id] ? ( ) : ( )} {faq.answer} )) ) : ( No FAQs available for this category )} ); }