Skip to content

Commit f1c31ed

Browse files
committed
Implement WebView component for external services
- created new route `Web` for navigating to a `WebView` - created a `WebScreen` for rendering a `WebView` with loading indicator - updated the `HtmlScreen` with adding a `Button` if a `webUrl` is given in navigation params - created a `Button` component based on React Native Elements Button: https://react-native-training.github.io/react-native-elements/docs/button.html resolves #2
1 parent 306c7c6 commit f1c31ed

6 files changed

Lines changed: 114 additions & 2 deletions

File tree

src/components/Button.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import PropTypes from 'prop-types';
2+
import React from 'react';
3+
import { StyleSheet } from 'react-native';
4+
import { Button as RNEButton } from 'react-native-elements';
5+
6+
import { colors } from '../config';
7+
8+
export const Button = ({ title, onPress }) => {
9+
return (
10+
<RNEButton
11+
type="outline"
12+
onPress={onPress}
13+
title={title}
14+
titleStyle={styles.titleStyle}
15+
buttonStyle={styles.buttonStyle}
16+
/>
17+
);
18+
};
19+
20+
const styles = StyleSheet.create({
21+
titleStyle: {
22+
color: colors.primary,
23+
fontFamily: 'titillium-web-bold'
24+
},
25+
buttonStyle: {
26+
borderColor: colors.primary
27+
}
28+
});
29+
30+
Button.propTypes = {
31+
title: PropTypes.string.isRequired,
32+
onPress: PropTypes.func.isRequired
33+
};

src/components/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './Button';
12
export * from './CardList';
23
export * from './DiagonalGradient';
34
export * from './HtmlView';

src/navigation/AppStackNavigator.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createStackNavigator } from 'react-navigation';
22

3-
import { DetailScreen, HomeScreen, HtmlScreen, IndexScreen } from '../screens';
3+
import { DetailScreen, HomeScreen, HtmlScreen, IndexScreen, WebScreen } from '../screens';
44
import { defaultStackNavigatorConfig } from './defaultStackNavigatorConfig';
55

66
import { texts } from '../config';
@@ -30,6 +30,12 @@ const AppStackNavigator = createStackNavigator(
3030
navigationOptions: (props) => ({
3131
title: props.navigation.getParam('title', '')
3232
})
33+
},
34+
Web: {
35+
screen: WebScreen,
36+
navigationOptions: (props) => ({
37+
title: props.navigation.getParam('title', '')
38+
})
3339
}
3440
},
3541
defaultStackNavigatorConfig('Home')

src/screens/HtmlScreen.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ActivityIndicator, ScrollView, StyleSheet, TouchableOpacity, View } fro
44
import { Query } from 'react-apollo';
55

66
import { colors, normalize } from '../config';
7-
import { HtmlView, Icon, Wrapper } from '../components';
7+
import { Button, HtmlView, Icon, Wrapper } from '../components';
88
import { trimNewLines } from '../helpers';
99
import { GET_PUBLIC_HTML_FILE } from '../queries';
1010
import { arrowLeft } from '../icons';
@@ -25,6 +25,8 @@ export class HtmlScreen extends React.Component {
2525
render() {
2626
const { navigation } = this.props;
2727
const queryVariables = navigation.getParam('queryVariables', '');
28+
const title = navigation.getParam('title', '');
29+
const webUrl = navigation.getParam('webUrl', '');
2830

2931
if (!queryVariables || !queryVariables.name) return null;
3032

@@ -49,6 +51,20 @@ export class HtmlScreen extends React.Component {
4951
<ScrollView>
5052
<Wrapper>
5153
<HtmlView html={trimNewLines(data.publicHtmlFile.content)} />
54+
{!!webUrl && (
55+
<Button
56+
title={`${title} öffnen`}
57+
onPress={() =>
58+
navigation.navigate({
59+
routeName: 'Web',
60+
params: {
61+
title,
62+
webUrl
63+
}
64+
})
65+
}
66+
/>
67+
)}
5268
</Wrapper>
5369
</ScrollView>
5470
);

src/screens/WebScreen.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import PropTypes from 'prop-types';
2+
import React from 'react';
3+
import { ActivityIndicator, StyleSheet, TouchableOpacity, View, WebView } from 'react-native';
4+
5+
import { colors, normalize } from '../config';
6+
import { Icon } from '../components';
7+
import { arrowLeft } from '../icons';
8+
9+
export class WebScreen extends React.PureComponent {
10+
static navigationOptions = ({ navigation }) => {
11+
return {
12+
headerLeft: (
13+
<View>
14+
<TouchableOpacity onPress={() => navigation.goBack()}>
15+
<Icon icon={arrowLeft(colors.lightestText)} style={styles.icon} />
16+
</TouchableOpacity>
17+
</View>
18+
)
19+
};
20+
};
21+
22+
render() {
23+
const { navigation } = this.props;
24+
const webUrl = navigation.getParam('webUrl', '');
25+
26+
if (!webUrl) return null;
27+
28+
return (
29+
<WebView
30+
source={{ uri: webUrl }}
31+
startInLoadingState
32+
renderLoading={() => (
33+
<View style={styles.loadingContainer}>
34+
<ActivityIndicator />
35+
</View>
36+
)}
37+
/>
38+
);
39+
}
40+
}
41+
42+
const styles = StyleSheet.create({
43+
icon: {
44+
paddingHorizontal: normalize(14)
45+
},
46+
loadingContainer: {
47+
alignItems: 'center',
48+
flex: 1,
49+
justifyContent: 'center'
50+
}
51+
});
52+
53+
WebScreen.propTypes = {
54+
navigation: PropTypes.object.isRequired
55+
};

src/screens/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './DetailScreen';
22
export * from './HomeScreen';
33
export * from './HtmlScreen';
44
export * from './IndexScreen';
5+
export * from './WebScreen';

0 commit comments

Comments
 (0)