|
| 1 | +import React from "react" |
| 2 | +import { FlatList, TextStyle, View, ViewStyle } from "react-native" |
| 3 | +import { ListItem, Text } from "app/components" |
| 4 | +import { AppStackScreenProps } from "app/navigators" |
| 5 | +import { colors, spacing } from "app/theme" |
| 6 | +import { useSafeAreaInsetsStyle } from "app/utils/useSafeAreaInsetsStyle" |
| 7 | +import { gql, useQuery } from "@apollo/client" |
| 8 | + |
| 9 | +const CHAPTERS_QUERY = gql` |
| 10 | + query Chapters { |
| 11 | + chapters { |
| 12 | + id |
| 13 | + number |
| 14 | + title |
| 15 | + } |
| 16 | + } |
| 17 | +` |
| 18 | + |
| 19 | +const ChapterItem = ({ |
| 20 | + chapter, |
| 21 | + onPress, |
| 22 | +}: { |
| 23 | + chapter: { id: number; number: number; title: string } |
| 24 | + onPress?: () => void |
| 25 | +}) => { |
| 26 | + const { number, title } = chapter |
| 27 | + let header, subheader |
| 28 | + |
| 29 | + if (number) { |
| 30 | + header = `Chapter ${number}` |
| 31 | + subheader = ` - ${title}` |
| 32 | + } else { |
| 33 | + header = title |
| 34 | + subheader = "" |
| 35 | + } |
| 36 | + |
| 37 | + return ( |
| 38 | + <ListItem |
| 39 | + text={`${header}${subheader}`} |
| 40 | + onPress={onPress} |
| 41 | + containerStyle={{ paddingHorizontal: spacing.sm }} |
| 42 | + /> |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +interface ApolloScreenProps extends AppStackScreenProps<"Apollo"> {} |
| 47 | + |
| 48 | +export const ApolloScreen: React.FC<ApolloScreenProps> = function ApolloScreen() { |
| 49 | + const { data, loading } = useQuery(CHAPTERS_QUERY) |
| 50 | + |
| 51 | + const $bottomContainerInsets = useSafeAreaInsetsStyle(["bottom"]) |
| 52 | + |
| 53 | + return ( |
| 54 | + <FlatList |
| 55 | + style={$container} |
| 56 | + contentContainerStyle={$bottomContainerInsets} |
| 57 | + data={loading ? [] : data.chapters} |
| 58 | + renderItem={({ item }) => ( |
| 59 | + <ChapterItem |
| 60 | + chapter={item} |
| 61 | + // onPress={() => navigation.navigate("Chapter", { chapter: item })} |
| 62 | + /> |
| 63 | + )} |
| 64 | + ListHeaderComponent={() => { |
| 65 | + return ( |
| 66 | + <View> |
| 67 | + <Text |
| 68 | + text="Reactotron can intercept and inspect Apollo client queries" |
| 69 | + style={$subheading} |
| 70 | + preset="subheading" |
| 71 | + /> |
| 72 | + <Text text="Like this example from the GraphQL example app:" style={$subheading} /> |
| 73 | + <View style={$bottomBorder} /> |
| 74 | + </View> |
| 75 | + ) |
| 76 | + }} |
| 77 | + keyExtractor={(chapter) => chapter.id.toString()} |
| 78 | + /> |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +const $container: ViewStyle = { |
| 83 | + flex: 1, |
| 84 | + backgroundColor: colors.background, |
| 85 | +} |
| 86 | + |
| 87 | +const $text: TextStyle = { |
| 88 | + color: colors.text, |
| 89 | +} |
| 90 | +const $subheading: TextStyle = { |
| 91 | + ...$text, |
| 92 | + margin: spacing.sm, |
| 93 | +} |
| 94 | + |
| 95 | +const $bottomBorder: ViewStyle = { |
| 96 | + borderBottomWidth: 1, |
| 97 | + borderBottomColor: colors.text, |
| 98 | + marginHorizontal: spacing.sm, |
| 99 | +} |
0 commit comments