|
| 1 | +/* eslint-disable react/no-unstable-nested-components */ |
| 2 | +import React, { useState, useCallback } from 'react'; |
| 3 | +import { View, ActivityIndicator, StyleSheet, RefreshControl, Text, Pressable } from 'react-native'; |
| 4 | +import { FlashList } from '@shopify/flash-list'; |
| 5 | +import axios from 'axios'; |
| 6 | +import { ArticleCard } from '../components/ArticleCard'; |
| 7 | +import type { Article } from '../types/api'; |
| 8 | +import { useFocusEffect } from '@react-navigation/native'; |
| 9 | + |
| 10 | +const ITEMS_PER_PAGE = 2; // Small limit to create more spans |
| 11 | +const AUTO_LOAD_LIMIT = 1; // One auto load at the end of the list then shows button |
| 12 | +const API_URL = 'https://api.spaceflightnewsapi.net/v4/articles'; |
| 13 | + |
| 14 | +export default function NewsScreen() { |
| 15 | + const [articles, setArticles] = useState<Article[]>([]); |
| 16 | + const [loading, setLoading] = useState(true); |
| 17 | + const [refreshing, setRefreshing] = useState(false); |
| 18 | + const [page, setPage] = useState(1); |
| 19 | + const [hasMore, setHasMore] = useState(true); |
| 20 | + const [autoLoadCount, setAutoLoadCount] = useState(0); |
| 21 | + |
| 22 | + const fetchArticles = async (pageNumber: number, refresh = false) => { |
| 23 | + try { |
| 24 | + const response = await axios.get(API_URL, { |
| 25 | + params: { |
| 26 | + limit: ITEMS_PER_PAGE, |
| 27 | + offset: (pageNumber - 1) * ITEMS_PER_PAGE, |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + const newArticles = response.data.results; |
| 32 | + setHasMore(response.data.next !== null); |
| 33 | + |
| 34 | + if (refresh) { |
| 35 | + setArticles(newArticles); |
| 36 | + setAutoLoadCount(0); |
| 37 | + } else { |
| 38 | + setArticles((prev) => [...prev, ...newArticles]); |
| 39 | + } |
| 40 | + } catch (error) { |
| 41 | + console.error('Error fetching articles:', error); |
| 42 | + } finally { |
| 43 | + setLoading(false); |
| 44 | + setRefreshing(false); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + useFocusEffect( |
| 49 | + useCallback(() => { |
| 50 | + fetchArticles(1, true); |
| 51 | + }, []) |
| 52 | + ); |
| 53 | + |
| 54 | + const handleLoadMore = () => { |
| 55 | + if (!loading && hasMore) { |
| 56 | + setPage((prev) => prev + 1); |
| 57 | + fetchArticles(page + 1); |
| 58 | + setAutoLoadCount((prev) => prev + 1); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + const handleManualLoadMore = () => { |
| 63 | + handleLoadMore(); |
| 64 | + }; |
| 65 | + |
| 66 | + const handleRefresh = () => { |
| 67 | + setRefreshing(true); |
| 68 | + setPage(1); |
| 69 | + fetchArticles(1, true); |
| 70 | + }; |
| 71 | + |
| 72 | + const handleEndReached = () => { |
| 73 | + if (autoLoadCount < AUTO_LOAD_LIMIT) { |
| 74 | + handleLoadMore(); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + const LoadMoreButton = () => { |
| 79 | + if (!hasMore) {return null;} |
| 80 | + if (loading) { |
| 81 | + return ( |
| 82 | + <View style={styles.loadMoreContainer}> |
| 83 | + <ActivityIndicator size="small" color="#007AFF" /> |
| 84 | + </View> |
| 85 | + ); |
| 86 | + } |
| 87 | + return ( |
| 88 | + <Pressable |
| 89 | + style={({ pressed }) => [ |
| 90 | + styles.loadMoreButton, |
| 91 | + pressed && styles.loadMoreButtonPressed, |
| 92 | + ]} |
| 93 | + onPress={handleManualLoadMore}> |
| 94 | + <Text style={styles.loadMoreText}>Load More Articles</Text> |
| 95 | + </Pressable> |
| 96 | + ); |
| 97 | + }; |
| 98 | + |
| 99 | + if (loading && !refreshing) { |
| 100 | + return ( |
| 101 | + <View style={styles.centered}> |
| 102 | + <ActivityIndicator size="large" color="#007AFF" /> |
| 103 | + </View> |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + return ( |
| 108 | + <View style={styles.container}> |
| 109 | + <FlashList |
| 110 | + data={articles} |
| 111 | + renderItem={({ item }) => <ArticleCard article={item} />} |
| 112 | + estimatedItemSize={350} |
| 113 | + onEndReached={handleEndReached} |
| 114 | + onEndReachedThreshold={0.5} |
| 115 | + ListFooterComponent={autoLoadCount >= AUTO_LOAD_LIMIT ? LoadMoreButton : null} |
| 116 | + refreshControl={ |
| 117 | + <RefreshControl refreshing={refreshing} onRefresh={handleRefresh} /> |
| 118 | + } |
| 119 | + /> |
| 120 | + </View> |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +const styles = StyleSheet.create({ |
| 125 | + container: { |
| 126 | + flex: 1, |
| 127 | + backgroundColor: '#f5f5f5', |
| 128 | + }, |
| 129 | + centered: { |
| 130 | + flex: 1, |
| 131 | + justifyContent: 'center', |
| 132 | + alignItems: 'center', |
| 133 | + }, |
| 134 | + loadMoreContainer: { |
| 135 | + paddingVertical: 20, |
| 136 | + alignItems: 'center', |
| 137 | + }, |
| 138 | + loadMoreButton: { |
| 139 | + backgroundColor: '#007AFF', |
| 140 | + paddingVertical: 12, |
| 141 | + paddingHorizontal: 24, |
| 142 | + borderRadius: 8, |
| 143 | + marginVertical: 20, |
| 144 | + marginHorizontal: 16, |
| 145 | + alignItems: 'center', |
| 146 | + shadowColor: '#000', |
| 147 | + shadowOffset: { width: 0, height: 2 }, |
| 148 | + shadowOpacity: 0.1, |
| 149 | + shadowRadius: 4, |
| 150 | + elevation: 3, |
| 151 | + }, |
| 152 | + loadMoreButtonPressed: { |
| 153 | + opacity: 0.8, |
| 154 | + transform: [{ scale: 0.98 }], |
| 155 | + }, |
| 156 | + loadMoreText: { |
| 157 | + color: '#fff', |
| 158 | + fontSize: 16, |
| 159 | + fontWeight: '600', |
| 160 | + }, |
| 161 | +}); |
0 commit comments