From 6c02250a7d54fa18cdeec2f4fd6866a9804d377e Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Wed, 16 Aug 2017 20:29:12 -0300 Subject: [PATCH] login saga (incomplete) --- app/actions/actionsTypes.js | 17 +++++++++++ app/actions/index.js | 6 ++++ app/actions/login.js | 30 +++++++++++++++++++ app/lib/rocketchat.js | 19 ++++++------ app/reducers/login.js | 38 ++++++++++++++++++++++++ app/reducers/rootReducer.js | 3 +- app/sagas/hello.js | 4 +-- app/sagas/login.js | 27 +++++++++++++++++ app/views/roomsList.js | 6 ++-- package-lock.json | 58 ++++++++++++++++++------------------- 10 files changed, 164 insertions(+), 44 deletions(-) create mode 100644 app/actions/actionsTypes.js create mode 100644 app/actions/login.js create mode 100644 app/reducers/login.js create mode 100644 app/sagas/login.js diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js new file mode 100644 index 00000000..07aa655d --- /dev/null +++ b/app/actions/actionsTypes.js @@ -0,0 +1,17 @@ + +const REQUEST = 'REQUEST'; +const SUCCESS = 'SUCCESS'; +const FAILURE = 'FAILURE'; + +function createRequestTypes(base) { + const res = {}; + [REQUEST, SUCCESS, FAILURE].forEach(type => res[type] = `${ base }_${ type }`); + return res; +} + +// Login events +export const LOGIN = createRequestTypes('LOGIN'); +export const LOGOUT = 'LOGOUT'; // logout is always success + +export const INCREMENT = 'INCREMENT'; +export const DECREMENT = 'DECREMENT'; diff --git a/app/actions/index.js b/app/actions/index.js index 97271809..c49b92bd 100644 --- a/app/actions/index.js +++ b/app/actions/index.js @@ -13,6 +13,12 @@ export function setAllSettings(settings) { payload: settings }; } +export function login() { + return { + type: 'LOGIN' + }; +} + // // GENRES // export function retrieveMoviesGenresSuccess(res) { diff --git a/app/actions/login.js b/app/actions/login.js new file mode 100644 index 00000000..aa87b247 --- /dev/null +++ b/app/actions/login.js @@ -0,0 +1,30 @@ +import * as types from './actionsTypes'; + +export function loginRequest(email, password) { + return { + type: types.LOGIN.REQUEST, + email, + password + }; +} + +export function loginSuccess({ token, user }) { + return { + type: types.LOGIN.SUCCESS, + token, + user + }; +} + +export function loginFailure(err) { + return { + type: types.LOGIN.FAILURE, + err + }; +} + +export function logout() { + return { + type: types.LOGOUT + }; +} diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 0275164f..8b7cd929 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -109,15 +109,16 @@ const RocketChat = { }, login(params, callback) { - Meteor._startLoggingIn(); - Meteor.call('login', params, (err, result) => { - Meteor._endLoggingIn(); - - Meteor._handleLoginCallback(err, result); - - if (typeof callback === 'function') { - callback(err); - } + return new Promise((resolve, reject) => { + Meteor._startLoggingIn(); + Meteor.call('login', params, (err, result) => { + Meteor._endLoggingIn(); + Meteor._handleLoginCallback(err, result); + err ? reject(err) : resolve(result); + if (typeof callback === 'function') { + callback(err, result); + } + }); }); }, diff --git a/app/reducers/login.js b/app/reducers/login.js new file mode 100644 index 00000000..471f1498 --- /dev/null +++ b/app/reducers/login.js @@ -0,0 +1,38 @@ +import * as types from '../actions/actionsTypes'; + +const initialState = { + isAuthenticated: false, + isFetching: false, + token: '', + user: {}, + errorMessage: '' +}; + +export default function login(state = initialState, action) { + switch (action.type) { + case types.LOGIN.REQUEST: + return { ...state, + isFetching: true, + isAuthenticated: false + }; + case types.LOGIN.SUCCESS: + return { ...state, + isFetching: false, + isAuthenticated: true, + token: action.token, + failure: false, + user: action.user + }; + case types.LOGIN.FAILURE: + return { ...state, + isFetching: false, + isAuthenticated: false, + failure: true, + errorMessage: action.err + }; + case types.LOGOUT: + return initialState; + default: + return state; + } +} diff --git a/app/reducers/rootReducer.js b/app/reducers/rootReducer.js index 49ef2cfd..609d7362 100644 --- a/app/reducers/rootReducer.js +++ b/app/reducers/rootReducer.js @@ -1,8 +1,9 @@ import { combineReducers } from 'redux'; import * as reducers from './reducers'; +import * as login from './login'; const rootReducer = combineReducers({ - ...reducers + ...reducers, ...login }); export default rootReducer; diff --git a/app/sagas/hello.js b/app/sagas/hello.js index 6e31b0f4..c5cdb794 100644 --- a/app/sagas/hello.js +++ b/app/sagas/hello.js @@ -4,8 +4,8 @@ import 'regenerator-runtime/runtime'; const foreverAlone = function* foreverAlone() { - yield take('cagado'); - console.log('foi cagado'); + yield take('FOI'); + console.log('FOIIIIIII'); yield take('voa'); console.log('o'); }; diff --git a/app/sagas/login.js b/app/sagas/login.js new file mode 100644 index 00000000..3aa751ab --- /dev/null +++ b/app/sagas/login.js @@ -0,0 +1,27 @@ +import React from 'react'; +import { take, put, call, fork, select } from 'redux-saga/effects'; +import * as types from '../actions/actionsTypes'; +import { loginSuccess, loginFailure } from '../actions/login'; +import RocketChat from '../lib/rocketchat'; + +function loginCall(...args) { + return RocketChat.loginWithPassword(...args); +} + +function* watchLoginRequest() { + while (true) { + 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() { + yield fork(watchLoginRequest); +} diff --git a/app/views/roomsList.js b/app/views/roomsList.js index 205f93ec..a904081e 100644 --- a/app/views/roomsList.js +++ b/app/views/roomsList.js @@ -8,7 +8,6 @@ import { Text, View, StyleSheet, TouchableOpacity, Platform, TextInput } from 'r import Meteor from 'react-native-meteor'; import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; - import * as actions from '../actions'; import realm from '../lib/realm'; import RocketChat from '../lib/rocketchat'; @@ -96,7 +95,8 @@ class RoomsListItem extends React.PureComponent { server: state.server, Site_Url: state.settings.Site_Url }), dispatch => ({ - actions: bindActionCreators(actions, dispatch) + actions: bindActionCreators(actions, dispatch), + login: () => dispatch(actions.login()) })) export default class RoomsListView extends React.Component { @@ -382,7 +382,7 @@ export default class RoomsListView extends React.Component { renderCreateButtons() { return ( - { this._createChannel(); }} > + { this.props.login(); }} > ); diff --git a/package-lock.json b/package-lock.json index 3f53082b..a297c56e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -115,7 +115,7 @@ "anymatch": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-1.3.2.tgz", - "integrity": "sha1-VT3Lj5HjyImEXf26NMd3IbkLnXo=", + "integrity": "sha512-0XNayC8lTHQ2OI8aljNCN3sSx6hsr/1+rlcDAotXJR7C1oZZHCNsfpbKwMjRA3Uqb5tF1Rae2oloTr4xpq+WjA==", "requires": { "micromatch": "2.3.11", "normalize-path": "2.1.1" @@ -173,7 +173,7 @@ "arr-flatten": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/arr-flatten/-/arr-flatten-1.1.0.tgz", - "integrity": "sha1-NgSLv/TntH4TZkQxbJlmnqWukfE=" + "integrity": "sha512-L3hKV5R/p5o81R7O02IGnwpDmkp6E982XhtbuwSe3O4qOtMMMtodicASA1Cny2U+aCXcNpml+m4dPsvsJ3jatg==" }, "array-differ": { "version": "1.0.0", @@ -265,7 +265,7 @@ "async": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/async/-/async-2.5.0.tgz", - "integrity": "sha1-hDGQ/WtzV6C54clW7d3V7IRitU0=", + "integrity": "sha512-e+lJAJeNWuPCNyxZKOBdaJGyLGHugXVQtrAwtuAe2vhxTYxFTKE73p8JuTmdH0qdQZtDvI4dhJwjZc5zsfIsYw==", "requires": { "lodash": "4.17.4" } @@ -1086,7 +1086,7 @@ "babylon": { "version": "6.17.4", "resolved": "https://registry.npmjs.org/babylon/-/babylon-6.17.4.tgz", - "integrity": "sha1-Pot0AriNIsNCPhN6FXeIOxX/hpo=" + "integrity": "sha512-kChlV+0SXkjE0vUn9OZ7pBMWRFd8uq3mZe8x1K6jhuNcAFAtEnjchFAqB+dYEXKyd+JpT6eppRR78QAr5gTsUw==" }, "balanced-match": { "version": "1.0.0", @@ -1814,7 +1814,7 @@ "diff": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/diff/-/diff-3.3.0.tgz", - "integrity": "sha1-BWaVFQ16qTI3yn43isOxaCt5Y7k=", + "integrity": "sha512-w0XZubFWn0Adlsapj9EAWX0FqWdO4tz8kc3RiYdWLh4k/V8PTb6i0SMgXt0vRM3zyKnT8tKO7mUlieRQHIjMNg==", "dev": true }, "doctrine": { @@ -2307,7 +2307,7 @@ "esprima": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.0.tgz", - "integrity": "sha1-RJnt3NERDgshi6zy+n9/WfVcqAQ=", + "integrity": "sha512-oftTcaMu/EGrEIu904mWteKIv8vMuOgGYo7EhVJJN00R/EED9DCua/xxHRdYnKtcECzVg7xOWhflvJMnqcFZjw==", "dev": true }, "esquery": { @@ -2869,7 +2869,7 @@ "glob": { "version": "7.1.2", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", - "integrity": "sha1-wZyd+aAocC1nhhI4SmVSQExjbRU=", + "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", "requires": { "fs.realpath": "1.0.0", "inflight": "1.0.6", @@ -2908,7 +2908,7 @@ "globals": { "version": "9.18.0", "resolved": "https://registry.npmjs.org/globals/-/globals-9.18.0.tgz", - "integrity": "sha1-qjiWs+abSH8X4x7SFD1pqOMMLYo=" + "integrity": "sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ==" }, "globby": { "version": "5.0.0", @@ -3094,7 +3094,7 @@ "hosted-git-info": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.5.0.tgz", - "integrity": "sha1-bWDjSzq7yDEwYsO3mO+NkBoHrzw=" + "integrity": "sha512-pNgbURSuab90KbTqvRPsseaTxOJCZBD0a7t+haSN33piP9cCM4l0CqdzAif2hUqm716UovKB2ROmiabGAKVXyg==" }, "html-encoding-sniffer": { "version": "1.0.1", @@ -3149,7 +3149,7 @@ "iconv-lite": { "version": "0.4.18", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.18.tgz", - "integrity": "sha1-I9hlaxaq5nQqwpcy6o8DNqR4nPI=" + "integrity": "sha512-sr1ZQph3UwHTR0XftSbK85OvBbxe/abLGzEnPENCQwmHf7sck8Oyu4ob3LgBxWWxRoM+QszeUyl7jbqapu2TqA==" }, "ignore": { "version": "3.3.3", @@ -3189,7 +3189,7 @@ "inquirer": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-3.2.1.tgz", - "integrity": "sha1-Bs6w9UD0XKVIwX1oQJWYeCZfoXU=", + "integrity": "sha512-QgW3eiPN8gpj/K5vVpHADJJgrrF0ho/dZGylikGX7iqAdRgC9FVKYKWFLx6hZDBFcOLEoSqINYrVPeFAeG/PdA==", "requires": { "ansi-escapes": "2.0.0", "chalk": "2.0.1", @@ -3484,13 +3484,13 @@ "istanbul-lib-coverage": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-1.1.1.tgz", - "integrity": "sha1-c7+5mIhSmUFck9OKPprfeEp3qdo=", + "integrity": "sha512-0+1vDkmzxqJIn5rcoEqapSB4DmPxE31EtI2dF2aCkV5esN9EWHxZ0dwgDClivMXJqE7zaYQxq30hj5L0nlTN5Q==", "dev": true }, "istanbul-lib-hook": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/istanbul-lib-hook/-/istanbul-lib-hook-1.0.7.tgz", - "integrity": "sha1-3WYH8DB2V4/n1vKmMM8UO0m6zdw=", + "integrity": "sha512-3U2HB9y1ZV9UmFlE12Fx+nPtFqIymzrqCksrXujm3NVbAZIJg/RfYgO1XiIa0mbmxTjWpVEVlkIZJ25xVIAfkQ==", "dev": true, "requires": { "append-transform": "0.4.0" @@ -3514,7 +3514,7 @@ "istanbul-lib-report": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/istanbul-lib-report/-/istanbul-lib-report-1.1.1.tgz", - "integrity": "sha1-8OVfVmVf+jQiIIC3oM1HYOFAX8k=", + "integrity": "sha512-tvF+YmCmH4thnez6JFX06ujIA19WPa9YUiwjc1uALF2cv5dmE3It8b5I8Ob7FHJ70H9Y5yF+TDkVa/mcADuw1Q==", "dev": true, "requires": { "istanbul-lib-coverage": "1.1.1", @@ -3543,7 +3543,7 @@ "istanbul-lib-source-maps": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/istanbul-lib-source-maps/-/istanbul-lib-source-maps-1.2.1.tgz", - "integrity": "sha1-pv4ay6jOCO68Y45XLilNJnAIqgw=", + "integrity": "sha512-mukVvSXCn9JQvdJl8wP/iPhqig0MRtuWuD4ZNKo6vB2Ik//AmhAKe3QnPN02dmkRe3lTudFk3rzoHhwU4hb94w==", "dev": true, "requires": { "debug": "2.6.8", @@ -3556,7 +3556,7 @@ "istanbul-reports": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/istanbul-reports/-/istanbul-reports-1.1.1.tgz", - "integrity": "sha1-BCvlyJ4XW8P4ZSPKqynAFOd/7k4=", + "integrity": "sha512-P8G873A0kW24XRlxHVGhMJBhQ8gWAec+dae7ZxOBzxT4w+a9ATSPvRVK3LB1RAJ9S8bg2tOyWHAGW40Zd2dKfw==", "dev": true, "requires": { "handlebars": "4.0.10" @@ -4237,7 +4237,7 @@ "js-yaml": { "version": "3.9.1", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.9.1.tgz", - "integrity": "sha1-CHdc69/dNZIJ8NKs04PI+GppBKA=", + "integrity": "sha512-CbcG379L1e+mWBnLvHWWeLs8GyV/EMw862uLI3c+GxVyDHWZcjZinwuBd3iW2pgxgIlksW/1vNJa4to+RvDOww==", "dev": true, "requires": { "argparse": "1.0.9", @@ -4253,7 +4253,7 @@ "jschardet": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/jschardet/-/jschardet-1.5.0.tgz", - "integrity": "sha1-ph8xAwalpxGI4bGs0IrdPPuwix4=" + "integrity": "sha512-+Q8JsoEQbrdE+a/gg1F9XO92gcKXgpE5UACqr0sIubjDmBEkd+OOWPGzQeMrWSLxd73r4dHxBeRW7edHu5LmJQ==" }, "jsdom": { "version": "9.12.0", @@ -4583,7 +4583,7 @@ "lru-cache": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.1.tgz", - "integrity": "sha1-Yi4y6CSItJJ5EUpPns9F581rulU=", + "integrity": "sha512-q4spe4KTfsAS1SUHLO0wz8Qiyf1+vMIAgpRYioFYDMNqKfHQbg+AVDH3i4fvpl71/P1L0dBl+fQi+P37UYf0ew==", "requires": { "pseudomap": "1.0.2", "yallist": "2.1.2" @@ -4812,7 +4812,7 @@ "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha1-UWbihkV/AzBgZL5Ul+jbsMPTIIM=", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", "requires": { "brace-expansion": "1.1.8" } @@ -4960,7 +4960,7 @@ "node-fetch": { "version": "1.7.1", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.1.tgz", - "integrity": "sha1-iZyz0KPJL5UsR/G4dvTIrqvUANU=", + "integrity": "sha512-j8XsFGCLw79vWXkZtMSmmLaOk9z5SQ9bV/tkbZVCqvgwzrjAGq66igobLofHtF63NvMTp2WjytpsNTGKa+XRIQ==", "requires": { "encoding": "0.1.12", "is-stream": "1.1.0" @@ -5057,7 +5057,7 @@ "normalize-package-data": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.4.0.tgz", - "integrity": "sha1-EvlaMH1YNSB1oEkHuErIvpisAS8=", + "integrity": "sha512-9jjUFbTPfEy3R/ad/2oNbKtW9Hgovl5O1FvFWKkKblNXoN/Oou6+9+KKohPK13Yc3/TyunyWhJp6gvRNR/PPAw==", "requires": { "hosted-git-info": "2.5.0", "is-builtin-module": "1.0.0", @@ -5451,7 +5451,7 @@ "promise": { "version": "7.3.1", "resolved": "https://registry.npmjs.org/promise/-/promise-7.3.1.tgz", - "integrity": "sha1-BktyYCsY+Q8pGSuLG8QY/9Hr078=", + "integrity": "sha512-nolQXZ/4L+bP/UGlkfaIujX9BKxGwmQ9OT4mOt5yvy8iK1h3wqTEJCijzGANTCCl9nWjY41juyAn2K3Q1hLLTg==", "requires": { "asap": "2.0.6" } @@ -5506,7 +5506,7 @@ "randomatic": { "version": "1.1.7", "resolved": "https://registry.npmjs.org/randomatic/-/randomatic-1.1.7.tgz", - "integrity": "sha1-x6vpzIuHwLqodrGf3oP9RkeX44w=", + "integrity": "sha512-D5JUjPyJbaJDkuAazpVnSfVkLlpeO3wDlPROTMLGKG1zMFNFRgrciKo1ltz/AzNTkqE0HzDx655QOL51N06how==", "requires": { "is-number": "3.0.0", "kind-of": "4.0.0" @@ -5860,7 +5860,7 @@ "react-native-drawer-layout": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/react-native-drawer-layout/-/react-native-drawer-layout-1.3.2.tgz", - "integrity": "sha1-uXQNdmOh3E+IphucbZPS2UjqQm4=", + "integrity": "sha512-fjO0scqbJUfNu2wuEpvywL7DYLXuCXJ2W/zYhWz986rdLytidbys1QGVvkaszHrb4Y7OqO96mTkgpOcP8KWevw==", "requires": { "react-native-dismiss-keyboard": "1.0.0" } @@ -5868,7 +5868,7 @@ "react-native-drawer-layout-polyfill": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/react-native-drawer-layout-polyfill/-/react-native-drawer-layout-polyfill-1.3.2.tgz", - "integrity": "sha1-GSyE16WmuKbSvix9ql5BZFGNDMc=", + "integrity": "sha512-XzPhfLDJrYHru+e8+dFwhf0FtTeAp7JXPpFYezYV6P1nTeA1Tia/kDpFT+O2DWTrBKBEI8FGhZnThrroZmHIxg==", "requires": { "react-native-drawer-layout": "1.3.2" } @@ -6219,7 +6219,7 @@ "readable-stream": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.3.tgz", - "integrity": "sha1-No8lEtefnUb9/HE0mueHi7weuVw=", + "integrity": "sha512-m+qzzcn7KUxEmd1gMbchF+Y2eIUbieUaxkWtptyHywrX0rE8QEYqPC07Vuy4Wm32/xE16NcdBctb8S0Xe/5IeQ==", "requires": { "core-util-is": "1.0.2", "inherits": "2.0.3", @@ -6454,7 +6454,7 @@ "resolve": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.4.0.tgz", - "integrity": "sha1-p1vgHFPaJdk0qY69DkxKcxL5KoY=", + "integrity": "sha512-aW7sVKPufyHqOmyyLzg/J+8606v5nevBgaliIlV7nUpVMsDnoBGV/cbSLNjZAg9q0Cfd/+easKVKQ8vOu8fn1Q==", "dev": true, "requires": { "path-parse": "1.0.5" @@ -6934,7 +6934,7 @@ "string-width": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", - "integrity": "sha1-q5Pyeo3BPSjKyBXEYhQ6bZASrp4=", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", "requires": { "is-fullwidth-code-point": "2.0.0", "strip-ansi": "4.0.0"