Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // @flow
  2. import * as React from 'react';
  3. import WebView from "react-native-webview";
  4. import {withTheme} from 'react-native-paper';
  5. import HeaderButton from "../Custom/HeaderButton";
  6. import BasicLoadingScreen from "../Custom/BasicLoadingScreen";
  7. import NetworkErrorComponent from "../Custom/NetworkErrorComponent";
  8. import i18n from "i18n-js";
  9. type Props = {
  10. navigation: Object,
  11. data: Array<{
  12. url: string,
  13. icon: string,
  14. name: string,
  15. customJS: string
  16. }>,
  17. headerTitle: string,
  18. hasHeaderBackButton: boolean,
  19. hasSideMenu: boolean,
  20. hasFooter: boolean,
  21. }
  22. /**
  23. * Class defining a webview screen.
  24. */
  25. class WebViewScreen extends React.PureComponent<Props> {
  26. static defaultProps = {
  27. hasBackButton: false,
  28. hasSideMenu: true,
  29. hasFooter: true,
  30. };
  31. webviewRef: Object;
  32. onRefreshClicked: Function;
  33. onWebviewRef: Function;
  34. getRenderLoading: Function;
  35. colors: Object;
  36. constructor(props) {
  37. super(props);
  38. this.onRefreshClicked = this.onRefreshClicked.bind(this);
  39. this.onWebviewRef = this.onWebviewRef.bind(this);
  40. this.getRenderLoading = this.getRenderLoading.bind(this);
  41. this.colors = props.theme.colors;
  42. }
  43. /**
  44. * Creates refresh button after mounting
  45. */
  46. componentDidMount() {
  47. const rightButton = this.getRefreshButton.bind(this);
  48. this.props.navigation.setOptions({
  49. headerRight: rightButton,
  50. });
  51. }
  52. /**
  53. * Gets a header refresh button
  54. *
  55. * @return {*}
  56. */
  57. getRefreshButton() {
  58. return <HeaderButton icon={'refresh'} onPress={this.onRefreshClicked}/>
  59. };
  60. /**
  61. * Callback to use when refresh button is clicked. Reloads the webview.
  62. */
  63. onRefreshClicked() {
  64. if (this.webviewRef !== null)
  65. this.webviewRef.reload();
  66. }
  67. /**
  68. * Callback used when receiving the webview ref. Stores the ref for later use
  69. *
  70. * @param ref
  71. */
  72. onWebviewRef(ref: Object) {
  73. this.webviewRef = ref
  74. }
  75. /**
  76. * Gets the loading indicator
  77. *
  78. * @return {*}
  79. */
  80. getRenderLoading() {
  81. return <BasicLoadingScreen/>;
  82. }
  83. render() {
  84. return (
  85. <WebView
  86. ref={this.onWebviewRef}
  87. source={{uri: this.props.data[0]['url']}}
  88. style={{
  89. width: '100%',
  90. height: '100%',
  91. }}
  92. startInLoadingState={true}
  93. injectedJavaScript={this.props.data[0]['customJS']}
  94. javaScriptEnabled={true}
  95. renderLoading={this.getRenderLoading}
  96. renderError={() => <NetworkErrorComponent
  97. {...this.props}
  98. onRefresh={this.onRefreshClicked}
  99. message={i18n.t("loginScreen.errors.connection")}
  100. icon={'access-point-network-off'}
  101. />}
  102. />
  103. );
  104. }
  105. }
  106. export default withTheme(WebViewScreen);