-
Notifications
You must be signed in to change notification settings - Fork 28
/
[movie].tsx
75 lines (68 loc) · 2.16 KB
/
[movie].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
67
68
69
70
71
72
73
74
75
import { ErrorMessage } from '@/react-query/components/ErrorMessage';
import { LoadingIndicator } from '@/react-query/components/LoadingIndicator';
import { useRefreshByUser } from '@/react-query/hooks/useRefreshByUser';
import { fetchMovie, MovieDetails } from '@/react-query/lib/api';
import { useQuery } from '@tanstack/react-query';
import { Stack, useLocalSearchParams } from 'expo-router';
import * as React from 'react';
import { RefreshControl, ScrollView, StyleSheet, View } from 'react-native';
import { Paragraph, Title } from 'react-native-paper';
export default function MovieDetailsScreen() {
const { movie } = useLocalSearchParams<{ movie: string }>();
return (
<>
<Stack.Screen options={{ title: movie }} />
<MovieDetailsInfo movie={movie} />
</>
);
}
function MovieDetailsInfo({ movie }: { movie: string }) {
const { isPending, error, data, refetch } = useQuery<MovieDetails, Error>({
queryKey: ['movie', movie],
queryFn: () => fetchMovie(movie),
initialData: { title: movie } as MovieDetails,
});
const { isRefetchingByUser, refetchByUser } = useRefreshByUser(refetch);
if (isPending) return <LoadingIndicator />;
if (error) return <ErrorMessage message={error.message}></ErrorMessage>;
if (!data) return null;
return (
<ScrollView
refreshControl={<RefreshControl refreshing={isRefetchingByUser} onRefresh={refetchByUser} />}>
<View style={styles.titleRow}>
<Title>
{data.title} ({data.year})
</Title>
</View>
{data.info ? (
<>
<View style={styles.infoRow}>
<Paragraph>{data.info.plot}</Paragraph>
</View>
<View style={styles.actorsRow}>
<Paragraph>
{data.info.actors.slice(0, -1).join(', ') + ' or ' + data.info.actors.slice(-1)}
</Paragraph>
</View>
</>
) : (
<LoadingIndicator />
)}
</ScrollView>
);
}
const styles = StyleSheet.create({
titleRow: {
flexDirection: 'row',
margin: 20,
},
infoRow: {
flexDirection: 'row',
margin: 20,
},
actorsRow: {
flexDirection: 'column',
margin: 20,
marginTop: 10,
},
});