login saga[2]

This commit is contained in:
Guilherme Gazzo 2017-08-16 21:24:06 -03:00
parent 9553ecbcaf
commit f269a6d36b
No known key found for this signature in database
GPG Key ID: 1F85C9AD922D0829
9 changed files with 89 additions and 53 deletions

View File

@ -1,10 +1,9 @@
import * as types from './actionsTypes'; import * as types from './actionsTypes';
export function loginRequest(email, password) { export function loginRequest(credentials) {
return { return {
type: types.LOGIN.REQUEST, type: types.LOGIN.REQUEST,
email, credentials
password
}; };
} }

View File

@ -5,7 +5,7 @@ import { createStore, applyMiddleware } from 'redux';
import createSagaMiddleware from 'redux-saga'; import createSagaMiddleware from 'redux-saga';
import logger from 'redux-logger'; import logger from 'redux-logger';
import rootReducer from '../reducers/rootReducer'; import rootReducer from '../reducers/rootReducer';
import helloSaga from '../sagas/hello'; import sagas from '../sagas';
const sagaMiddleware = createSagaMiddleware(); const sagaMiddleware = createSagaMiddleware();
let middleware; let middleware;
@ -22,4 +22,4 @@ export default createStore(
rootReducer, rootReducer,
applyMiddleware(sagaMiddleware) applyMiddleware(sagaMiddleware)
); );
sagaMiddleware.run(helloSaga); sagaMiddleware.run(sagas);

View File

@ -123,6 +123,7 @@ const RocketChat = {
}, },
loginWithPassword({ username, password, code }, callback) { loginWithPassword({ username, password, code }, callback) {
console.log('AQQQQQ');
let params = {}; let params = {};
const state = reduxStore.getState(); const state = reduxStore.getState();

View File

@ -1,7 +1,4 @@
import { take, fork } from 'redux-saga/effects'; import { take, fork } from 'redux-saga/effects';
import 'babel-polyfill';
import 'regenerator-runtime/runtime';
const foreverAlone = function* foreverAlone() { const foreverAlone = function* foreverAlone() {
yield take('FOI'); yield take('FOI');

30
app/sagas/index.js Normal file
View File

@ -0,0 +1,30 @@
import { take, fork } from 'redux-saga/effects';
import hello from './hello';
import login from './login';
const root = function* root() {
yield fork(hello);
yield fork(login);
};
// Consider using takeEvery
export default root;
//
// import { take, fork } from 'redux-saga/effects';
// import 'babel-polyfill';
// import 'regenerator-runtime/runtime';
//
//
// const foreverAlone = function* foreverAlone() {
// yield take('FOI');
// console.log('FOIIIIIII');
// yield take('voa');
// console.log('o');
// };
//
// const root = function* root() {
// yield fork(foreverAlone);
// };
//
// export default root;

View File

@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import { take, put, call, fork, select } from 'redux-saga/effects'; import { take, put, call, fork } from 'redux-saga/effects';
import * as types from '../actions/actionsTypes'; import * as types from '../actions/actionsTypes';
import { loginSuccess, loginFailure } from '../actions/login'; import { loginSuccess, loginFailure } from '../actions/login';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
@ -8,20 +8,20 @@ function loginCall(...args) {
return RocketChat.loginWithPassword(...args); return RocketChat.loginWithPassword(...args);
} }
function* watchLoginRequest() { const watchLoginRequest = function* watchLoginRequest() {
while (true) { while (true) {
// yield take('METEOR_CONNECTED');
const payload = yield take(types.LOGIN.REQUEST); const payload = yield take(types.LOGIN.REQUEST);
try { try {
const response = yield call(loginCall, payload); const response = yield call(loginCall, payload);
yield put(loginSuccess(response)); yield put(loginSuccess(response));
console.log('SAGA LOGIN SUCCESS: ', response);
} catch (err) { } catch (err) {
console.log('SAGA LOGIN ERR: ', err);
yield put(loginFailure(err.status)); yield put(loginFailure(err.status));
} }
} }
} };
export default function* root() { const root = function* root() {
yield fork(watchLoginRequest); yield fork(watchLoginRequest);
} };
export default root;

View File

@ -3,9 +3,8 @@ import PropTypes from 'prop-types';
import { Text, TextInput, StyleSheet } from 'react-native'; import { Text, TextInput, StyleSheet } from 'react-native';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
// import * as actions from '../actions';
import * as actions from '../actions'; import * as loginActions from '../actions/login';
import RocketChat from '../lib/rocketchat';
import KeyboardView from '../components/KeyboardView'; import KeyboardView from '../components/KeyboardView';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
@ -33,17 +32,10 @@ const styles = StyleSheet.create({
} }
}); });
@connect(state => ({ class LoginView extends React.Component {
server: state.server,
Accounts_EmailOrUsernamePlaceholder: state.settings.Accounts_EmailOrUsernamePlaceholder,
Accounts_PasswordPlaceholder: state.settings.Accounts_PasswordPlaceholder
}), dispatch => ({
actions: bindActionCreators(actions, dispatch)
}))
export default class LoginView extends React.Component {
static propTypes = { static propTypes = {
navigator: PropTypes.object.isRequired, navigator: PropTypes.object.isRequired,
loginRequest: PropTypes.func.isRequired,
server: PropTypes.string.isRequired, server: PropTypes.string.isRequired,
Accounts_EmailOrUsernamePlaceholder: PropTypes.string, Accounts_EmailOrUsernamePlaceholder: PropTypes.string,
Accounts_PasswordPlaceholder: PropTypes.string Accounts_PasswordPlaceholder: PropTypes.string
@ -71,32 +63,31 @@ export default class LoginView extends React.Component {
subtitle: nextProps.server subtitle: nextProps.server
}); });
} }
submit = () => { submit = () => {
this.setState({ const { username, password, code } = this.state;
error: undefined this.props.loginRequest({ username, password, code });
}); console.log(this.props.loginRequest.toString());
//
const credentials = { //
username: this.state.username, // this.setState({
password: this.state.password, // error: undefined
code: this.state.code // });
}; //
//
RocketChat.loginWithPassword(credentials, (error) => { // RocketChat.loginWithPassword(credentials, (error) => {
if (error) { // if (error) {
if (error.error === 'totp-required') { // if (error.error === 'totp-required') {
this.setState({ totp: true }); // this.setState({ totp: true });
this.codeInput.focus(); // this.codeInput.focus();
} else { // } else {
this.setState({ // this.setState({
error: error.reason // error: error.reason
}); // });
} // }
} else { // } else {
this.props.navigator.dismissModal(); // this.props.navigator.dismissModal();
} // }
}); // });
} }
renderTOTP = () => { renderTOTP = () => {
@ -120,6 +111,7 @@ export default class LoginView extends React.Component {
render() { render() {
return ( return (
<KeyboardView style={styles.view} keyboardVerticalOffset={64}> <KeyboardView style={styles.view} keyboardVerticalOffset={64}>
{this.props.login.isFetching && <Text> INDO</Text>}
<TextInput <TextInput
style={styles.input} style={styles.input}
onChangeText={username => this.setState({ username })} onChangeText={username => this.setState({ username })}
@ -147,3 +139,18 @@ export default class LoginView extends React.Component {
); );
} }
} }
function mapStateToProps(state) {
return {
server: state.server,
Accounts_EmailOrUsernamePlaceholder: state.settings.Accounts_EmailOrUsernamePlaceholder,
Accounts_PasswordPlaceholder: state.settings.Accounts_PasswordPlaceholder,
login: state.default
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(loginActions, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(LoginView);

View File

@ -1 +1 @@
import './app/navigation'; import './index.ios';

View File

@ -1 +1,3 @@
import 'babel-polyfill';
import 'regenerator-runtime/runtime';
import './app/navigation'; import './app/navigation';