1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-11 11:12:03 +02:00

124 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-07-29 15:43:53 +02:00
const React = require('react');
2019-07-29 15:58:33 +02:00
const { View } = require('react-native');
2019-07-29 15:58:33 +02:00
const { Button } = require('react-native');
2019-07-29 15:43:53 +02:00
const { WebView } = require('react-native-webview');
const { connect } = require('react-redux');
const { ScreenHeader } = require('lib/components/screen-header.js');
const { reg } = require('lib/registry.js');
const { _ } = require('lib/locale.js');
const { BaseScreenComponent } = require('lib/components/base-screen.js');
const parseUri = require('lib/parseUri');
2017-07-06 19:29:09 +00:00
2017-07-14 18:49:14 +00:00
class OneDriveLoginScreenComponent extends BaseScreenComponent {
static navigationOptions() {
2017-07-06 19:29:09 +00:00
return { header: null };
}
constructor() {
super();
this.state = { webviewUrl: '' };
2017-07-06 19:29:09 +00:00
this.authCode_ = null;
}
UNSAFE_componentWillMount() {
2017-07-06 19:29:09 +00:00
this.setState({
2017-07-06 19:48:17 +00:00
webviewUrl: this.startUrl(),
2017-07-06 19:29:09 +00:00
});
}
2017-07-06 19:48:17 +00:00
startUrl() {
2019-07-29 15:43:53 +02:00
return reg
.syncTarget()
.api()
.authCodeUrl(this.redirectUrl());
2017-07-06 19:48:17 +00:00
}
2017-07-06 19:29:09 +00:00
redirectUrl() {
2019-07-29 15:43:53 +02:00
return reg
.syncTarget()
.api()
.nativeClientRedirectUrl();
2017-07-06 19:29:09 +00:00
}
async webview_load(noIdeaWhatThisIs) {
// This is deprecated according to the doc but since the non-deprecated property (source)
// doesn't exist, use this for now. The whole component is completely undocumented
// at the moment so it's likely to change.
const url = noIdeaWhatThisIs.url;
2017-11-28 21:49:58 +00:00
const parsedUrl = parseUri(url);
2017-07-06 19:29:09 +00:00
2017-11-28 21:49:58 +00:00
if (!this.authCode_ && parsedUrl && parsedUrl.queryKey && parsedUrl.queryKey.code) {
2019-07-29 15:43:53 +02:00
this.authCode_ = parsedUrl.queryKey.code;
2017-07-06 19:29:09 +00:00
2017-07-08 00:25:10 +01:00
try {
2019-07-29 15:43:53 +02:00
await reg
.syncTarget()
.api()
.execTokenRequest(this.authCode_, this.redirectUrl(), true);
this.props.dispatch({ type: 'NAV_BACK' });
2017-07-24 22:52:30 +01:00
reg.scheduleSync(0);
2017-07-08 00:25:10 +01:00
} catch (error) {
2019-09-19 22:51:18 +01:00
alert(`Could not login to OneDrive. Please try again\n\n${error.message}\n\n${url}`);
2017-07-08 00:25:10 +01:00
}
2017-07-06 19:29:09 +00:00
2017-07-06 19:48:17 +00:00
this.authCode_ = null;
2017-07-06 19:29:09 +00:00
}
}
async webview_error() {
alert('Could not load page. Please check your connection and try again.');
2017-07-06 19:48:17 +00:00
}
retryButton_click() {
// It seems the only way it would reload the page is by loading an unrelated
// URL, waiting a bit, and then loading the actual URL. There's probably
// a better way to do this.
this.setState({
webviewUrl: 'https://microsoft.com',
2017-07-06 19:48:17 +00:00
});
this.forceUpdate();
setTimeout(() => {
this.setState({
webviewUrl: this.startUrl(),
});
this.forceUpdate();
}, 1000);
}
2017-07-06 19:29:09 +00:00
render() {
const source = {
uri: this.state.webviewUrl,
2019-07-29 15:43:53 +02:00
};
2017-07-06 19:29:09 +00:00
return (
2017-07-14 18:49:14 +00:00
<View style={this.styles().screen}>
2019-07-29 15:43:53 +02:00
<ScreenHeader title={_('Login with OneDrive')} />
2017-07-06 19:29:09 +00:00
<WebView
source={source}
onNavigationStateChange={o => {
2019-07-29 15:43:53 +02:00
this.webview_load(o);
}}
onError={() => {
this.webview_error();
}}
2017-07-06 19:29:09 +00:00
/>
2019-07-29 15:43:53 +02:00
<Button
title={_('Refresh')}
onPress={() => {
this.retryButton_click();
}}
></Button>
2017-07-06 19:29:09 +00:00
</View>
);
}
}
const OneDriveLoginScreen = connect(() => {
2019-07-29 15:43:53 +02:00
return {};
})(OneDriveLoginScreenComponent);
2017-07-06 19:29:09 +00:00
2019-07-29 15:43:53 +02:00
module.exports = { OneDriveLoginScreen };