22import React , { useState , useCallback } from 'react' ;
33import { View , ActivityIndicator , StyleSheet , RefreshControl , Text , Pressable } from 'react-native' ;
44import { FlashList } from '@shopify/flash-list' ;
5- import axios from 'axios' ;
65import { ArticleCard } from '../components/ArticleCard' ;
76import type { Article } from '../types/api' ;
87import { useFocusEffect } from '@react-navigation/native' ;
@@ -13,11 +12,7 @@ const API_URL = 'https://api.spaceflightnewsapi.net/v4/articles';
1312
1413export const preloadArticles = async ( ) => {
1514 // Not actually preloading, just fetching for testing purposes
16- await axios . get ( API_URL , {
17- params : {
18- limit : ITEMS_PER_PAGE ,
19- } ,
20- } ) ;
15+ await fetch ( `${ API_URL } /?limit=${ ITEMS_PER_PAGE } ` ) ;
2116} ;
2217
2318export default function NewsScreen ( ) {
@@ -30,21 +25,21 @@ export default function NewsScreen() {
3025
3126 const fetchArticles = async ( pageNumber : number , refresh = false ) => {
3227 try {
33- const response = await axios . get ( API_URL , {
34- params : {
35- limit : ITEMS_PER_PAGE ,
36- offset : ( pageNumber - 1 ) * ITEMS_PER_PAGE ,
37- } ,
38- } ) ;
28+ const response = await fetch (
29+ ` ${ API_URL } /?limit= ${ ITEMS_PER_PAGE } &offset= $ {
30+ ( pageNumber - 1 ) * ITEMS_PER_PAGE
31+ } ` ,
32+ ) ;
33+ const data = await response . json ( ) ;
3934
40- const newArticles = response . data . results ;
41- setHasMore ( response . data . next !== null ) ;
35+ const newArticles = data . results ;
36+ setHasMore ( data . next !== null ) ;
4237
4338 if ( refresh ) {
4439 setArticles ( newArticles ) ;
4540 setAutoLoadCount ( 0 ) ;
4641 } else {
47- setArticles ( ( prev ) => [ ...prev , ...newArticles ] ) ;
42+ setArticles ( prev => [ ...prev , ...newArticles ] ) ;
4843 }
4944 } catch ( error ) {
5045 console . error ( 'Error fetching articles:' , error ) ;
@@ -62,14 +57,14 @@ export default function NewsScreen() {
6257 }
6358
6459 fetchArticles ( 1 , true ) ;
65- } , [ articles ] )
60+ } , [ articles ] ) ,
6661 ) ;
6762
6863 const handleLoadMore = ( ) => {
6964 if ( ! loading && hasMore ) {
70- setPage ( ( prev ) => prev + 1 ) ;
65+ setPage ( prev => prev + 1 ) ;
7166 fetchArticles ( page + 1 ) ;
72- setAutoLoadCount ( ( prev ) => prev + 1 ) ;
67+ setAutoLoadCount ( prev => prev + 1 ) ;
7368 }
7469 } ;
7570
@@ -90,7 +85,9 @@ export default function NewsScreen() {
9085 } ;
9186
9287 const LoadMoreButton = ( ) => {
93- if ( ! hasMore ) { return null ; }
88+ if ( ! hasMore ) {
89+ return null ;
90+ }
9491 if ( loading ) {
9592 return (
9693 < View style = { styles . loadMoreContainer } >
@@ -126,7 +123,9 @@ export default function NewsScreen() {
126123 estimatedItemSize = { 350 }
127124 onEndReached = { handleEndReached }
128125 onEndReachedThreshold = { 0.5 }
129- ListFooterComponent = { autoLoadCount >= AUTO_LOAD_LIMIT ? LoadMoreButton : null }
126+ ListFooterComponent = {
127+ autoLoadCount >= AUTO_LOAD_LIMIT ? LoadMoreButton : null
128+ }
130129 refreshControl = {
131130 < RefreshControl refreshing = { refreshing } onRefresh = { handleRefresh } />
132131 }
0 commit comments