Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: code refactoring and type improvement #7634

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading