Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebViewScreen.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // @flow
  2. import * as React from 'react';
  3. import {Linking, Platform, View} from 'react-native';
  4. import {Spinner} from 'native-base';
  5. import WebView from "react-native-webview";
  6. import Touchable from "react-native-platform-touchable";
  7. import CustomMaterialIcon from "../components/CustomMaterialIcon";
  8. import ThemeManager from "../utils/ThemeManager";
  9. import BaseContainer from "../components/BaseContainer";
  10. type Props = {
  11. navigation: Object,
  12. url: string,
  13. customInjectedJS: string,
  14. headerTitle: string,
  15. hasHeaderBackButton: boolean,
  16. hasSideMenu: boolean,
  17. }
  18. /**
  19. * Class defining a webview screen.
  20. */
  21. export default class WebViewScreen extends React.Component<Props> {
  22. static defaultProps = {
  23. customInjectedJS: '',
  24. hasBackButton: false,
  25. hasSideMenu: true,
  26. };
  27. webview: WebView;
  28. openWebLink() {
  29. Linking.openURL(this.props.url).catch((err) => console.error('Error opening link', err));
  30. }
  31. getHeaderButton(clickAction: Function, icon: string) {
  32. return (
  33. <Touchable
  34. style={{padding: 6}}
  35. onPress={() => clickAction()}>
  36. <CustomMaterialIcon
  37. color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
  38. icon={icon}/>
  39. </Touchable>
  40. );
  41. }
  42. getRefreshButton() {
  43. return (
  44. <View style={{flexDirection: 'row'}}>
  45. {this.getHeaderButton(() => this.openWebLink(), 'web')}
  46. {this.getHeaderButton(() => this.refreshWebview(), 'refresh')}
  47. </View>
  48. );
  49. };
  50. refreshWebview() {
  51. this.webview.reload();
  52. }
  53. render() {
  54. const nav = this.props.navigation;
  55. return (
  56. <BaseContainer
  57. navigation={nav}
  58. headerTitle={this.props.headerTitle}
  59. headerRightButton={this.getRefreshButton()}
  60. hasBackButton={this.props.hasHeaderBackButton}
  61. hasSideMenu={this.props.hasSideMenu}>
  62. <WebView
  63. ref={ref => (this.webview = ref)}
  64. source={{uri: this.props.url}}
  65. style={{
  66. width: '100%',
  67. height: '100%',
  68. }}
  69. startInLoadingState={true}
  70. injectedJavaScript={this.props.customInjectedJS}
  71. javaScriptEnabled={true}
  72. renderLoading={() =>
  73. <View style={{
  74. backgroundColor: ThemeManager.getCurrentThemeVariables().containerBgColor,
  75. position: 'absolute',
  76. top: 0,
  77. right: 0,
  78. width: '100%',
  79. height: '100%',
  80. flex: 1,
  81. alignItems: 'center',
  82. justifyContent: 'center'
  83. }}>
  84. <Spinner/>
  85. </View>
  86. }
  87. />
  88. </BaseContainer>
  89. );
  90. }
  91. }