Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,8 @@

// External dependencies
import React from 'react';
import {
Alert,
BackHandler,
Linking,
Platform,
StatusBar,
StyleSheet,
View,
WebView,
} from 'react-native';
import { Alert, BackHandler, Linking, Platform, StatusBar, StyleSheet, View } from 'react-native';
import { WebView } from 'react-native-webview';
import Constants from 'expo-constants';
import { Notifications } from 'expo';

Expand Down Expand Up @@ -78,8 +70,26 @@ export default class App extends React.Component {
});

// JS injected to `WebView`
// Embedded website will change its functionality based on this.
appInfoJavaScript = 'window.trMobileApp=' + JSON.stringify(this.appInfo) + ';';
injectedJavaScript = `
function postMessage(message) {
if (window.ReactNativeWebView && typeof window.ReactNativeWebView.postMessage == 'function') {
window.ReactNativeWebView.postMessage(JSON.stringify(message));
} else {
console.error("'window.ReactNativeWebView.postMessage' is not a function");
}
}

// This is needed because we want to subscribe notifications only
// if user is authenticated window.ReactNativeWebView.postMessage
// accepts one argument, data, which will be available on the event
// object, event.nativeEvent.data. data must be a string.
if (window.user && window.user._id) {
postMessage({ "action": "authenticated" });
}

// Embedded website will change its functionality based on this.
window.trMobileApp = ${JSON.stringify(this.appInfo)};
`;

componentWillMount() {
// Subscribe to push notifications
Expand Down Expand Up @@ -274,18 +284,7 @@ export default class App extends React.Component {
// I.e. on each URL change
_handleLoadEnd = () => {
console.log('handleLoadEnd');
this.webView.injectJavaScript(
// This is needed because we want to subscribe notifications only
// if user is authenticated window.postMessage accepts one
// argument, data, which will be available on the event object,
// event.nativeEvent.data. data must be a string.
`
if (window.user && window.user._id && typeof window.postMessage === 'function') {
window.postMessage('{ "action": "authenticated" }');
}
${this.appInfoJavaScript}
`
);
this.webView.injectJavaScript(this.injectedJavaScript);
};

_handleError = error => {
Expand All @@ -311,7 +310,7 @@ export default class App extends React.Component {
<StatusBar backgroundColor={this.styles.statusBar.backgroundColor} barStyle="default" />
<WebView
domStorageEnabled
injectedJavaScript={this.appInfoJavaScript}
injectedJavaScript={this.injectedJavaScript}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this line is actually needed at all, since we're loading the same code on every onLoadEnd, but I donno 🤷‍♀️

onError={this._handleError}
onLoadEnd={this._handleLoadEnd}
onMessage={this.appInfo.os === 'ios' ? null : this._handleMessage}
Expand Down
17 changes: 14 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"react": "16.8.3",
"react-dom": "^16.8.6",
"react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
"react-native-web": "^0.11.4"
"react-native-web": "^0.11.4",
"react-native-webview": "^5.8.2"
},
"devDependencies": {
"babel-preset-expo": "^5.1.1",
Expand Down