-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserList.tsx
74 lines (65 loc) · 1.95 KB
/
UserList.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
import { FunctionComponent, useEffect } from "react";
import { FlatList, Image, SafeAreaView, StyleSheet, Text, View } from "react-native";
import { useDispatch, useSelector } from "react-redux";
import { RootState } from "./store";
import { fetchUsers, User } from "./useListSlice";
const UserList: FunctionComponent = () => {
const dispatch = useDispatch();
useEffect(() => {
dispatch(fetchUsers({ page: 1 }))
}, [])
const screenState = useSelector((state: RootState) => state.userList)
const handleOnEnd = () => {
if (!screenState.loading) {
dispatch(fetchUsers({ page: screenState.nextPage }))
}
}
return (<>
<SafeAreaView style={{ flex: 1 }}>
{screenState.loading && <Text>Loading</Text>}
{screenState.error && <Text>Error</Text>}
{!screenState.loading && !screenState.error && <Text>Done</Text>}
<FlatList data={screenState.users}
keyExtractor={(_, i) => i.toString()}
renderItem={({ item }) => {
return (
<UserListItem user={item} />
)
}}
onEndReached={handleOnEnd}
/>
</SafeAreaView>
</>)
}
const UserListItem: FunctionComponent<{ user: User }> = ({ user }) => {
return <View style={style.container}>
<Image
style={style.img}
source={{
uri: user.picture.thumbnail
}} />
<Text style={style.nameText}>
{
user.name.first
}
</Text>
</View>
}
const style = StyleSheet.create({
nameText: {
padding: 15
},
container: {
flexDirection: 'row',
padding: 15,
alignItems: 'center'
},
img: {
width: 50,
height: 50,
borderRadius: 25,
borderColor: 'purple',
borderWidth: 2
}
})
export default UserList