-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
140 lines (137 loc) · 3.76 KB
/
App.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
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
import { StatusBar } from "expo-status-bar";
import {
ScrollView,
StyleSheet,
View,
Text,
ActivityIndicator,
useWindowDimensions,
} from "react-native";
import axios from "axios";
import { Card, Button } from "react-native-elements";
import { useState, useEffect } from "react";
import { post, Posts } from "./types";
import { openBrowserAsync } from "expo-web-browser";
import RenderHtml from "react-native-render-html";
import he from "he";
export default function App() {
const [index, setIndex] = useState(1);
const [posts, setPosts] = useState([{} as post]);
const { width } = useWindowDimensions();
const fetchPosts = () => {
axios
.get("https://www.reddit.com/r/appideas/new/.json?limit=100")
.then((res) => {
const postsToRender: Posts = [{} as post];
postsToRender.pop();
res.data.data.children.forEach((child: any) => {
postsToRender.push({
title: child.data.title,
url: child.data.url,
text: child.data.selftext_html,
});
});
setPosts(postsToRender);
})
.catch((error) => {
console.log(error.message);
});
};
useEffect(() => {
fetchPosts();
}, []);
return (
<>
<ScrollView>
<View style={styles.container}>
{posts.length > 0 && posts[index] ? (
<>
<Card containerStyle={styles.card}>
<Card.Title>{posts[index].title}</Card.Title>
<Card.Divider />
{posts[index].text ? (
<RenderHtml
contentWidth={width}
source={{
html: he.decode(posts[index].text),
}}
/>
) : null}
<View style={styles.buttonsContainer}>
<Button
containerStyle={styles.buttonContainerStyles}
buttonStyle={styles.buttonStyle}
titleStyle={styles.buttonTitleStyle}
title={"Link"}
onPress={() => {
openBrowserAsync(posts[index].url);
}}
/>
</View>
</Card>
<View style={styles.buttonsContainer}>
<Button
containerStyle={styles.buttonContainerStyles}
titleStyle={styles.buttonTitleStyle}
title={"Back"}
onPress={() => {
if (index < posts.length) {
setIndex(index - 1);
}
}}
/>
<Button
containerStyle={styles.buttonContainerStyles}
titleStyle={styles.buttonTitleStyle}
title={"Next"}
onPress={() => {
if (index < posts.length) {
setIndex(index + 1);
}
}}
/>
</View>
</>
) : (
<>
<ActivityIndicator />
</>
)}
</View>
</ScrollView>
</>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
paddingTop: 50,
alignContent: "center",
},
card: {
borderRadius: 25,
width: "80%",
},
text: {
textAlign: "center",
},
buttonsContainer: {
flexDirection: "row",
flexWrap: "wrap",
alignItems: "center",
justifyContent: "center",
},
buttonContainerStyles: {
margin: 20,
},
buttonStyle: {
backgroundColor: "black",
borderRadius: 50,
paddingLeft: 20,
paddingRight: 20,
},
buttonTitleStyle: {},
});