-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
169 lines (152 loc) · 3.65 KB
/
App.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React, {useState, useEffect, useCallback} from 'react';
import {
SafeAreaView,
StyleSheet,
Text,
FlatList,
View,
Image,
useWindowDimensions,
useColorScheme,
} from 'react-native';
const themes = colorScheme =>
colorScheme === 'dark'
? {
textColor: '#fff',
headerColor: '#fff',
backgroundColor: '#000',
cardColor: '#222',
}
: {
textColor: '#222',
headerColor: '#000',
backgroundColor: '#fff',
cardColor: '#eef',
};
const ThemeContext = React.createContext({});
const ThemeProvider = props => {
const [theme, setTheme] = useState(themes());
const colorScheme = useColorScheme();
useEffect(() => {
setTheme(themes(colorScheme));
}, [colorScheme]);
return (
<ThemeContext.Provider value={theme}>
{props.children}
</ThemeContext.Provider>
);
};
// HOC
const withTheme =
Component =>
({...props}) => {
const theme = React.useContext(ThemeContext);
return <Component theme={theme} {...props} />;
};
const ListItem = ({image, name, location, flexCols, theme}) => {
const styles = StyleSheet.create({
root: {
flex: 1 / flexCols,
padding: 10,
},
container: {
flex: 1,
overflow: 'hidden',
backgroundColor: theme.cardColor,
borderRadius: 10,
},
name: {
fontSize: 14,
color: theme.textColor,
fontWeight: '700',
},
location: {
fontSize: 10,
fontStyle: 'italic',
color: theme.textColor,
fontWeight: '700',
},
image: {
aspectRatio: 1,
resizeMode: 'cover',
},
contentContainer: {
padding: 5,
},
});
return (
<View style={styles.root}>
<View style={styles.container}>
<Image source={{uri: image}} style={styles.image} />
<View style={styles.contentContainer}>
<Text style={styles.name}>{name}</Text>
<Text style={styles.location}>{location.name}</Text>
</View>
</View>
</View>
);
};
const ThemedListItem = withTheme(ListItem);
const App = ({theme}) => {
const [data, setdata] = useState([]);
const [nextPageURL, setnextPageURL] = useState(null);
const getData = useCallback(async () => {
const apiEndPoint = nextPageURL
? nextPageURL
: 'https://rickandmortyapi.com/api/character';
fetch(apiEndPoint)
.then(response => response.json())
.then(resp => {
setnextPageURL(resp.info.next);
setdata(prevData => {
return [...prevData, ...resp.results];
});
});
}, [nextPageURL]);
useEffect(() => {
(async () => {
getData();
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const window = useWindowDimensions();
const numCols = Math.ceil(window.width / 220);
const styles = StyleSheet.create({
root: {
backgroundColor: theme.backgroundColor,
},
heading: {
fontSize: 30,
color: theme.headerColor,
fontWeight: '400',
marginHorizontal: 10,
marginVertical: 10,
},
});
return (
<SafeAreaView>
<View style={styles.root}>
<Text style={styles.heading}>Characters</Text>
<FlatList
onEndReached={getData}
numColumns={numCols}
key={numCols}
data={data}
renderItem={({item}) => {
return <ThemedListItem {...item} flexCols={numCols} />;
}}
keyExtractor={(item, idx) => `${item.name}_${idx}`}
/>
</View>
</SafeAreaView>
);
};
const ThemedApp = withTheme(App);
const Root = () => {
return (
<ThemeProvider>
<ThemedApp />
</ThemeProvider>
);
};
export default Root;