Skip to content

Instantly share code, notes, and snippets.

@ivanferrer
Last active December 9, 2019 15:08
Show Gist options
  • Save ivanferrer/af497cb85f42f2bfceccf75de6d6cdc8 to your computer and use it in GitHub Desktop.
Save ivanferrer/af497cb85f42f2bfceccf75de6d6cdc8 to your computer and use it in GitHub Desktop.
React-native - Arquivo de exemplo - versão antiga
import React, { Component } from 'react';
import { WebView } from 'react-native-webview';
import { Alert, Button, TextInput, View, StyleSheet, Text, Modal, Keyboard } from 'react-native';
//import { store } from 'react-redux'
import {Login} from 'combo-mobile';
import CookieManager from 'react-native-cookies';
import { NativeModules, Platform } from 'react-native';
import {
DOMAIN,
DOMAIN_APP,
TEST_PARAMS_LOGIN,
APPLICATION_ENV
} from '../config/env';
const autologin = true;status_logged = false;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backfaceVisibility: 'hidden',
},
webview:{
flex: 1,
justifyContent: 'center',
height: 'auto',
alignItems: 'stretch',
}
});
export default class App extends Component{
constructor(props) {
super(props);
state = {}
this.state = {
domain:DOMAIN_APP,
uri:DOMAIN,
modalVisible: true,
loginVisible: false
}
}
_gotoWebViewApp = (is_true) => {
console.log('cheguei aqui...')
if(is_true) {
console.log('renderizando a webview...')
this.callbackLogin();
// this.setState({ state: this.state })
// this.render();
// this.forceUpdate();
}
}
_gotoWindowApp = () => {
this.state = {};
const {navigate} = this.props.navigation;
navigate('Main');
}
_createCookie = (name, value, call) => {
console.log(name, value, DOMAIN_APP);
CookieManager.setFromResponse(this.state.domain+'/', name+'='+value+';path=/;expires=2030-05-30T12:30:00.00-05:00;secure')
.then((res) => {
console.log('setFromResponse.set =>', res);
CookieManager.get(this.state.domain+'/')
.then((resGet) => {
console.log(resGet[name]);
if (resGet[name] != undefined ){
console.log('COOKIE SETADO????? =>', resGet);
call({name:name, value:resGet[name]});
}
});
});
}
_requestLogin = () => {
console.log('vou criar cookie...')
this.state = {
result:{
tk_os:Platform.OS,
type_login:'app',
cookies:[]
},
is_logged:true,
create_cookie:false,
domain:DOMAIN_APP,
uri:DOMAIN,
modalVisible: true,
loginVisible: false
}
this._cookiesLoginApp();
}
_cookiesLoginApp = () => {
if (this.state.domain) {
CookieManager.clearAll()
//alert('r=>'+JSON.stringify(this.state.result))
this._createCookie('tk_os', this.state.result.tk_os, (res) => {
this.state.result.cookies.push(res);
this._createCookie('_email', this.state.result._email, (res) => {
this.state.result.cookies.push(res);
this._createCookie('_token',this.state.result._token, (res) => {
this.state.result.cookies.push(res);
this._createCookie('type_login',this.state.result.type_login, (res) => {
this.state.result.cookies.push(res);
this.state.is_logged = true;
this.state.create_cookie = true;
console.log('já criei todos,agora vou renderizar a webview...')
this._gotoWebViewApp(true);
})
})
})
})
}
}
show () {
this.setState({ modalVisible: true})
this.setState({ loginVisible: false})
}
hide () {
this.setState({ modalVisible: false })
this.setState({ loginVisible: true })
}
_onNavigationStateChange = (webViewState) => {
console.log(webViewState.url)
let url = webViewState.url, interval=1000;
console.log('navigation-change')
if (url.indexOf('/sair') !== -1) {
this.state = {};
this.state.is_logged = false;
console.log('fez logout...')
this.state = {};
CookieManager.clearAll();
console.log('limpei login')
this.hide()
// this._gotoWindowLogin();
}
}
componentDidMount(){
this.autoLogin();
setTimeout(() => {
//depois que gravou cookie... força entrar novamente...
this.autoLogin();
}, 4000)
}
autoLogin(){
console.log('passei nesta montagem...')
CookieManager.get(DOMAIN_APP+'/')
.then((res) => {
if ( res.gutenemail != undefined ){
this.state = {
result:{
tk_os:Platform.OS,
type_login:'app',
cookies:[],
is_logged:true,
create_cookie:true
},
domain:DOMAIN_APP,
uri:DOMAIN,
modalVisible: true
}
console.log(this.state);
//this._gotoWebViewGuten(true);
} else {
console.log('criei cookie...')
this._requestLogin();
}
if (this.state != null && typeof this.state.result !== 'undefined' &&
typeof this.state.result.is_logged !== 'undefined' &&
this.state.result.is_logged === true) {
this.show();
}
});
}
// shouldComponentUpdate() {
// this.state =!this.state;
// }
render () {
const jsLib = 'setScript("' + DOMAIN_APP + '/path/js/add-script-' + Platform.OS + '-platform.js", '+ Platform.OS +');';
const {
modalVisible,
loginVisible
} = this.state
return (
<React.Fragment>
{modalVisible && (
<Modal animationType={'slide'}
visible={modalVisible}
onRequestClose={this.hide.bind(this)}
transparent>
<View visible={this.state.modalVisible} style={styles.webview}>
<WebView source={{ uri: this.state.uri }}
// mixedContentMode={'compatibility'}
javaScriptEnabledAndroid={true}
onNavigationStateChange={this._onNavigationStateChange.bind(this)}
onError={this._onNavigationStateChange.bind(this)}
mixedContentMode={'compatibility'}
javaScriptEnabled={true}
domStorageEnabled={true}
injectedJavaScript={jsLib}
startInLoadingState={false}
thirdPartyCookiesEnabled={true}
useWebKit={true}
sharedCookiesEnabled={true}
// onMessage={event => {
// let logout = event.nativeEvent.data;
// if (typeof logout.logout_webapp_in_app !== 'undefined' && logout.logout_webapp_in_app) {
// CookieManager.clearAll()
// this.state = null;
// this._gotoWebViewGuten();
// }
// }}
/>
</View>
</Modal>
)}
<Modal animationType={'slide'}
visible={loginVisible}
onRequestClose={this.show.bind(this)}
transparent>
<View style={styles.container}>
<Login />
</View>
</Modal>
</React.Fragment>
// <RecoveryError />
// <RecoverySuccess />
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment