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