forked from vergnet/application-amicale
107 lines
2.9 KiB
JavaScript
107 lines
2.9 KiB
JavaScript
// @flow
|
|
|
|
import * as React from 'react';
|
|
import WebView from "react-native-webview";
|
|
import BasicLoadingScreen from "../Custom/BasicLoadingScreen";
|
|
import ErrorView from "../Custom/ErrorView";
|
|
import {ERROR_TYPE} from "../../utils/WebData";
|
|
import MaterialHeaderButtons, {Item} from '../Custom/HeaderButton';
|
|
import {HiddenItem} from "react-navigation-header-buttons";
|
|
import {Linking} from "expo";
|
|
import i18n from 'i18n-js';
|
|
|
|
type Props = {
|
|
navigation: Object,
|
|
url: string,
|
|
customJS: string,
|
|
}
|
|
|
|
/**
|
|
* Class defining a webview screen.
|
|
*/
|
|
class WebViewScreen extends React.PureComponent<Props> {
|
|
|
|
static defaultProps = {
|
|
customJS: '',
|
|
};
|
|
|
|
webviewRef: Object;
|
|
|
|
constructor() {
|
|
super();
|
|
this.webviewRef = React.createRef();
|
|
}
|
|
|
|
/**
|
|
* Creates refresh button after mounting
|
|
*/
|
|
componentDidMount() {
|
|
const rightButton = this.getRefreshButton.bind(this);
|
|
this.props.navigation.setOptions({
|
|
headerRight: rightButton,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Gets a header refresh button
|
|
*
|
|
* @return {*}
|
|
*/
|
|
getRefreshButton() {
|
|
return (
|
|
<MaterialHeaderButtons>
|
|
<Item
|
|
title="refresh"
|
|
iconName="refresh"
|
|
onPress={this.onRefreshClicked}/>
|
|
<HiddenItem
|
|
title={i18n.t("general.goBack")}
|
|
iconName="arrow-left"
|
|
onPress={this.onGoBackClicked}/>
|
|
<HiddenItem
|
|
title={i18n.t("general.goForward")}
|
|
iconName="arrow-right"
|
|
onPress={this.onGoForwardClicked}/>
|
|
<HiddenItem
|
|
title={i18n.t("general.openInBrowser")}
|
|
iconName="web"
|
|
onPress={this.onOpenClicked}/>
|
|
</MaterialHeaderButtons>
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Callback to use when refresh button is clicked. Reloads the webview.
|
|
*/
|
|
onRefreshClicked = () => this.webviewRef.current.reload();
|
|
onGoBackClicked = () => this.webviewRef.current.goBack();
|
|
onGoForwardClicked = () => this.webviewRef.current.goForward();
|
|
|
|
onOpenClicked = () => Linking.openURL(this.props.url);
|
|
|
|
/**
|
|
* Gets the loading indicator
|
|
*
|
|
* @return {*}
|
|
*/
|
|
getRenderLoading = () => <BasicLoadingScreen/>;
|
|
|
|
render() {
|
|
return (
|
|
<WebView
|
|
ref={this.webviewRef}
|
|
source={{uri: this.props.url}}
|
|
startInLoadingState={true}
|
|
injectedJavaScript={this.props.customJS}
|
|
javaScriptEnabled={true}
|
|
renderLoading={this.getRenderLoading}
|
|
renderError={() => <ErrorView
|
|
errorCode={ERROR_TYPE.CONNECTION_ERROR}
|
|
onRefresh={this.onRefreshClicked}
|
|
/>}
|
|
/>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default WebViewScreen;
|