-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
141 lines (125 loc) · 3.35 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
import React from 'react';
import { Screen, NavigationBar, ImageBackground, Tile, Divider, ListView, Title, Subtitle, Html, TextInput, Button, View, Text, Caption } from '@shoutem/ui';
import { Font, AppLoading } from 'expo';
import { createStackNavigator } from 'react-navigation';
import { ScrollView, TouchableOpacity, Button as RNButton } from 'react-native';
class HomeScreen extends React.Component {
constructor (props) {
super(props);
this.state = {
fontsAreLoaded: false,
posts: []
};
}
static navigationOptions ({ navigation }) {
return {
headerTitle: 'Posts',
headerRight: (
<RNButton
onPress={() => navigation.navigate('Login')}
title="Login"
/>
)
};
}
async componentWillMount () {
await Font.loadAsync({
'Rubik-Regular': require('./node_modules/@shoutem/ui/fonts/Rubik-Regular.ttf')
});
fetch('https://dmyz.org/wp-json/wp/v2/posts?categories=79').then(r => r.json()).then(posts => this.setState({ posts }));
this.setState({ fontsAreLoaded: true });
}
renderRow (post) {
return (
<View>
<TouchableOpacity onPress={() => this.props.navigation.navigate('Post', { postId: post.id })}>
<ImageBackground
styleName="large-banner"
source={{ uri: 'https://picsum.photos/320/480/?random' }}
>
<Tile>
<Title styleName="md-gutter-bottom">{post.title.rendered}</Title>
<Subtitle styleName="sm-gutter-horizontal">{post.link}</Subtitle>
</Tile>
</ImageBackground>
<Divider styleName="line" />
</TouchableOpacity>
</View>
);
}
render () {
if (!this.state.fontsAreLoaded) {
return <AppLoading />;
}
const posts = this.state.posts;
return (
<Screen>
<ListView
data={posts}
renderRow={data => this.renderRow(data)}
/>
</Screen>
);
}
}
class PostScreen extends React.Component {
constructor (props) {
super(props);
this.state = {
post: undefined
};
}
componentWillMount () {
const postId = this.props.navigation.state.params.postId;
fetch(`https://dmyz.org/wp-json/wp/v2/posts/${postId}`).then(r => r.json()).then(post => this.setState({ post }));
}
render () {
const post = this.state.post;
if (!post) {
return <AppLoading />;
}
return (
<ScrollView>
<Tile styleName="text-centric">
<Title styleName="sm-gutter-bottom">{post.title.rendered}</Title>
<Caption>{post.date}</Caption>
</Tile>
<Html body={post.content.rendered} />
</ScrollView>
);
}
}
class LoginScreen extends React.Component {
render () {
return (
<Screen>
<NavigationBar
title="Login"
styleName="inline"
/>
<TextInput placeholder={'Username or email'} />
<TextInput placeholder={'Password'} secureTextEntry />
<View>
<Button styleName="secondary">
<Text>LOGIN</Text>
</Button>
</View>
</Screen>
);
}
}
const RootStack = createStackNavigator(
{
Home: HomeScreen,
Post: PostScreen,
Login: LoginScreen
},
{
initialRouteName: 'Home'
}
);
export default class App extends React.Component {
render () {
return <RootStack />;
}
}