-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
MoviesListScreen.tsx
66 lines (57 loc) 路 1.87 KB
/
MoviesListScreen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as React from 'react';
import { FlatList, RefreshControl } from 'react-native';
import { useQuery } from '@tanstack/react-query';
import { StackNavigationProp } from '@react-navigation/stack';
import { LoadingIndicator } from '@app/components/LoadingIndicator';
import { ErrorMessage } from '@app/components/ErrorMessage';
import { Divider } from '@app/components/Divider';
import { ListItem } from '@app/components/ListItem';
import { useRefreshByUser } from '@app/hooks/useRefreshByUser';
import { useRefreshOnFocus } from '@app/hooks/useRefreshOnFocus';
import { fetchMovies, Movie } from '@app/lib/api';
import { MoviesStackNavigator } from '@app/navigation/types';
type MoviesListScreenNavigationProp = StackNavigationProp<
MoviesStackNavigator,
'MoviesList'
>;
type Props = {
navigation: MoviesListScreenNavigationProp;
};
export function MoviesListScreen({ navigation }: Props) {
const { isLoading, error, data, refetch } = useQuery<Movie[], Error>({
queryKey: ['movies'],
queryFn: fetchMovies,
});
const { isRefetchingByUser, refetchByUser } = useRefreshByUser(refetch);
useRefreshOnFocus(refetch);
const onListItemPress = React.useCallback(
(movie) => {
navigation.navigate('MovieDetails', {
movie,
});
},
[navigation]
);
const renderItem = React.useCallback(
({ item }) => {
return <ListItem item={item} onPress={onListItemPress} />;
},
[onListItemPress]
);
if (isLoading) return <LoadingIndicator />;
if (error) return <ErrorMessage message={error.message}></ErrorMessage>;
return (
<FlatList
data={data}
renderItem={renderItem}
keyExtractor={(item) => item.title}
ItemSeparatorComponent={() => <Divider />}
refreshControl={
<RefreshControl
refreshing={isRefetchingByUser}
onRefresh={refetchByUser}
/>
}
></FlatList>
);
}