From f269a6d36b3ce9e55f39c789251f3b0da3e0af05 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Wed, 16 Aug 2017 21:24:06 -0300 Subject: [PATCH] login saga[2] --- app/actions/login.js | 5 ++- app/lib/createStore.js | 4 +-- app/lib/rocketchat.js | 1 + app/sagas/hello.js | 3 -- app/sagas/index.js | 30 ++++++++++++++++ app/sagas/login.js | 14 ++++---- app/views/login.js | 81 +++++++++++++++++++++++------------------- index.android.js | 2 +- index.ios.js | 2 ++ 9 files changed, 89 insertions(+), 53 deletions(-) create mode 100644 app/sagas/index.js diff --git a/app/actions/login.js b/app/actions/login.js index aa87b247..f593707f 100644 --- a/app/actions/login.js +++ b/app/actions/login.js @@ -1,10 +1,9 @@ import * as types from './actionsTypes'; -export function loginRequest(email, password) { +export function loginRequest(credentials) { return { type: types.LOGIN.REQUEST, - email, - password + credentials }; } diff --git a/app/lib/createStore.js b/app/lib/createStore.js index b71d589f..cd923bcb 100644 --- a/app/lib/createStore.js +++ b/app/lib/createStore.js @@ -5,7 +5,7 @@ import { createStore, applyMiddleware } from 'redux'; import createSagaMiddleware from 'redux-saga'; import logger from 'redux-logger'; import rootReducer from '../reducers/rootReducer'; -import helloSaga from '../sagas/hello'; +import sagas from '../sagas'; const sagaMiddleware = createSagaMiddleware(); let middleware; @@ -22,4 +22,4 @@ export default createStore( rootReducer, applyMiddleware(sagaMiddleware) ); -sagaMiddleware.run(helloSaga); +sagaMiddleware.run(sagas); diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 623f8b5b..eee2df77 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -123,6 +123,7 @@ const RocketChat = { }, loginWithPassword({ username, password, code }, callback) { + console.log('AQQQQQ'); let params = {}; const state = reduxStore.getState(); diff --git a/app/sagas/hello.js b/app/sagas/hello.js index c5cdb794..83a72731 100644 --- a/app/sagas/hello.js +++ b/app/sagas/hello.js @@ -1,7 +1,4 @@ import { take, fork } from 'redux-saga/effects'; -import 'babel-polyfill'; -import 'regenerator-runtime/runtime'; - const foreverAlone = function* foreverAlone() { yield take('FOI'); diff --git a/app/sagas/index.js b/app/sagas/index.js new file mode 100644 index 00000000..61eeac14 --- /dev/null +++ b/app/sagas/index.js @@ -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; diff --git a/app/sagas/login.js b/app/sagas/login.js index 3aa751ab..cfbf1628 100644 --- a/app/sagas/login.js +++ b/app/sagas/login.js @@ -1,5 +1,5 @@ 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 { loginSuccess, loginFailure } from '../actions/login'; import RocketChat from '../lib/rocketchat'; @@ -8,20 +8,20 @@ function loginCall(...args) { return RocketChat.loginWithPassword(...args); } -function* watchLoginRequest() { +const watchLoginRequest = function* watchLoginRequest() { while (true) { + // yield take('METEOR_CONNECTED'); const payload = yield take(types.LOGIN.REQUEST); try { const response = yield call(loginCall, payload); yield put(loginSuccess(response)); - console.log('SAGA LOGIN SUCCESS: ', response); } catch (err) { - console.log('SAGA LOGIN ERR: ', err); yield put(loginFailure(err.status)); } } -} +}; -export default function* root() { +const root = function* root() { yield fork(watchLoginRequest); -} +}; +export default root; diff --git a/app/views/login.js b/app/views/login.js index 3fba2378..b65e0f35 100644 --- a/app/views/login.js +++ b/app/views/login.js @@ -3,9 +3,8 @@ import PropTypes from 'prop-types'; import { Text, TextInput, StyleSheet } from 'react-native'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; - -import * as actions from '../actions'; -import RocketChat from '../lib/rocketchat'; +// import * as actions from '../actions'; +import * as loginActions from '../actions/login'; import KeyboardView from '../components/KeyboardView'; const styles = StyleSheet.create({ @@ -33,17 +32,10 @@ const styles = StyleSheet.create({ } }); -@connect(state => ({ - 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 { +class LoginView extends React.Component { static propTypes = { navigator: PropTypes.object.isRequired, + loginRequest: PropTypes.func.isRequired, server: PropTypes.string.isRequired, Accounts_EmailOrUsernamePlaceholder: PropTypes.string, Accounts_PasswordPlaceholder: PropTypes.string @@ -71,32 +63,31 @@ export default class LoginView extends React.Component { subtitle: nextProps.server }); } - submit = () => { - this.setState({ - error: undefined - }); - - const credentials = { - username: this.state.username, - password: this.state.password, - code: this.state.code - }; - - RocketChat.loginWithPassword(credentials, (error) => { - if (error) { - if (error.error === 'totp-required') { - this.setState({ totp: true }); - this.codeInput.focus(); - } else { - this.setState({ - error: error.reason - }); - } - } else { - this.props.navigator.dismissModal(); - } - }); + const { username, password, code } = this.state; + this.props.loginRequest({ username, password, code }); + console.log(this.props.loginRequest.toString()); + // + // + // this.setState({ + // error: undefined + // }); + // + // + // RocketChat.loginWithPassword(credentials, (error) => { + // if (error) { + // if (error.error === 'totp-required') { + // this.setState({ totp: true }); + // this.codeInput.focus(); + // } else { + // this.setState({ + // error: error.reason + // }); + // } + // } else { + // this.props.navigator.dismissModal(); + // } + // }); } renderTOTP = () => { @@ -120,6 +111,7 @@ export default class LoginView extends React.Component { render() { return ( + {this.props.login.isFetching && INDO} 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); diff --git a/index.android.js b/index.android.js index 0db019b3..6902f2f2 100644 --- a/index.android.js +++ b/index.android.js @@ -1 +1 @@ -import './app/navigation'; +import './index.ios'; diff --git a/index.ios.js b/index.ios.js index 0db019b3..31dc38cf 100644 --- a/index.ios.js +++ b/index.ios.js @@ -1 +1,3 @@ +import 'babel-polyfill'; +import 'regenerator-runtime/runtime'; import './app/navigation';