application-amicale/components/Screens/WebViewScreen.js

121 lines
3 KiB
JavaScript
Raw Normal View History

// @flow
import * as React from 'react';
import WebView from "react-native-webview";
2020-04-02 10:45:42 +02:00
import {withTheme} from 'react-native-paper';
2020-04-02 10:07:20 +02:00
import HeaderButton from "../Custom/HeaderButton";
2020-04-02 10:45:42 +02:00
import BasicLoadingScreen from "../Custom/BasicLoadingScreen";
import NetworkErrorComponent from "../Custom/NetworkErrorComponent";
import i18n from "i18n-js";
type Props = {
navigation: Object,
data: Array<{
url: string,
icon: string,
name: string,
customJS: string
}>,
headerTitle: string,
hasHeaderBackButton: boolean,
hasSideMenu: boolean,
2019-11-07 10:57:58 +01:00
hasFooter: boolean,
}
/**
* Class defining a webview screen.
*/
class WebViewScreen extends React.PureComponent<Props> {
static defaultProps = {
hasBackButton: false,
hasSideMenu: true,
2019-11-07 10:57:58 +01:00
hasFooter: true,
};
webviewRef: Object;
onRefreshClicked: Function;
onWebviewRef: Function;
getRenderLoading: Function;
colors: Object;
constructor(props) {
super(props);
this.onRefreshClicked = this.onRefreshClicked.bind(this);
this.onWebviewRef = this.onWebviewRef.bind(this);
this.getRenderLoading = this.getRenderLoading.bind(this);
this.colors = props.theme.colors;
}
2020-03-29 14:46:44 +02:00
/**
* Creates refresh button after mounting
*/
2020-03-06 09:12:56 +01:00
componentDidMount() {
const rightButton = this.getRefreshButton.bind(this);
this.props.navigation.setOptions({
headerRight: rightButton,
});
}
2020-03-29 14:46:44 +02:00
/**
* Gets a header refresh button
*
* @return {*}
*/
getRefreshButton() {
2020-03-29 14:46:44 +02:00
return <HeaderButton icon={'refresh'} onPress={this.onRefreshClicked}/>
};
2020-03-29 14:46:44 +02:00
/**
* Callback to use when refresh button is clicked. Reloads the webview.
*/
onRefreshClicked() {
if (this.webviewRef !== null)
this.webviewRef.reload();
}
2020-03-29 14:46:44 +02:00
/**
* Callback used when receiving the webview ref. Stores the ref for later use
*
* @param ref
*/
onWebviewRef(ref: Object) {
this.webviewRef = ref
}
2020-03-29 14:46:44 +02:00
/**
* Gets the loading indicator
*
* @return {*}
*/
getRenderLoading() {
2020-04-02 10:45:42 +02:00
return <BasicLoadingScreen/>;
}
render() {
return (
<WebView
ref={this.onWebviewRef}
source={{uri: this.props.data[0]['url']}}
style={{
width: '100%',
height: '100%',
}}
startInLoadingState={true}
injectedJavaScript={this.props.data[0]['customJS']}
javaScriptEnabled={true}
renderLoading={this.getRenderLoading}
renderError={() => <NetworkErrorComponent
{...this.props}
onRefresh={this.onRefreshClicked}
message={i18n.t("loginScreen.errors.connection")}
icon={'access-point-network-off'}
/>}
/>
);
}
}
export default withTheme(WebViewScreen);