Skip to content

Commit

Permalink
docs(examples): code refactoring and type improvement (#7634)
Browse files Browse the repository at this point in the history
* refactor: minor code refactoring and type improvement for react-native example

* chore: slice the movie with original value
  • Loading branch information
aziyatali authored Jun 27, 2024
1 parent 8b89941 commit 8d75aaa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
5 changes: 3 additions & 2 deletions examples/react/react-native/src/components/ListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as React from 'react'
import { View, StyleSheet } from 'react-native'
import { Paragraph, TouchableRipple } from 'react-native-paper'
import { MovieDetails } from '../lib/api'

type Props = {
item: any
onPress: (item: any) => void
item: MovieDetails
onPress: (item: MovieDetails) => void
}

export function ListItem({ item, onPress }: Props) {
Expand Down
18 changes: 9 additions & 9 deletions examples/react/react-native/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ function delay(t: number) {
})
}

export async function fetchMovies() {
export async function fetchMovies(): Promise<Movie[]> {
console.log('fetchMovies')

await delay(200 + Math.floor(Math.random() * 2000))

return movies
.slice(0, 100)
.map((movie) => ({ title: movie.title, year: movie.year })) as Promise<
Movie[]
>
return Promise.resolve(
movies
.slice(0, 100)
.map((movie) => ({ title: movie.title, year: movie.year })),
)
}

export async function fetchMovie(title: string) {
export async function fetchMovie(title: string): Promise<MovieDetails> {
console.log('fetchMovie', title)

await delay(200 + Math.floor(Math.random() * 2000))

const result = movies.filter((item) => item.title === title)

if (result.length == 0) {
if (result.length === 0) {
throw new Error('Movie not found')
}
return result[0] as Promise<MovieDetails>
return Promise.resolve(result[0] as MovieDetails)
}
7 changes: 3 additions & 4 deletions examples/react/react-native/src/screens/MoviesListScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Divider } from '../components/Divider'
import { ListItem } from '../components/ListItem'
import { useRefreshByUser } from '../hooks/useRefreshByUser'
import { useRefreshOnFocus } from '../hooks/useRefreshOnFocus'
import { fetchMovies, Movie } from '../lib/api'
import { fetchMovies, Movie, MovieDetails } from '../lib/api'
import { MoviesStackNavigator } from '../navigation/types'

type MoviesListScreenNavigationProp = StackNavigationProp<
Expand All @@ -30,7 +30,7 @@ export function MoviesListScreen({ navigation }: Props) {
useRefreshOnFocus(refetch)

const onListItemPress = React.useCallback(
(movie) => {
(movie: MovieDetails) => {
navigation.navigate('MovieDetails', {
movie,
})
Expand All @@ -39,7 +39,7 @@ export function MoviesListScreen({ navigation }: Props) {
)

const renderItem = React.useCallback(
({ item }) => {
({ item }: { item: MovieDetails }) => {
return <ListItem item={item} onPress={onListItemPress} />
},
[onListItemPress],
Expand All @@ -48,7 +48,6 @@ export function MoviesListScreen({ navigation }: Props) {
if (isPending) return <LoadingIndicator />

if (error) return <ErrorMessage message={error.message}></ErrorMessage>

return (
<FlatList
data={data}
Expand Down

0 comments on commit 8d75aaa

Please sign in to comment.