From 0986e75779d0c4b59efaf09b413c839797bd0021 Mon Sep 17 00:00:00 2001
From: Guilherme Gazzo <guilhermegazzo@gmail.com>
Date: Wed, 16 Aug 2017 22:06:31 -0300
Subject: [PATCH] saga for meteor connect

---
 app/actions/actionsTypes.js |  7 ++++---
 app/actions/connect.js      | 20 ++++++++++++++++++++
 app/lib/realm.js            |  2 +-
 app/lib/rocketchat.js       |  4 +---
 app/reducers/connect.js     | 32 ++++++++++++++++++++++++++++++++
 app/reducers/rootReducer.js |  3 ++-
 app/sagas/connect.js        | 26 ++++++++++++++++++++++++++
 app/sagas/index.js          | 24 +++---------------------
 app/sagas/login.js          |  2 +-
 app/views/roomsList.js      |  8 ++++++--
 10 files changed, 96 insertions(+), 32 deletions(-)
 create mode 100644 app/actions/connect.js
 create mode 100644 app/reducers/connect.js
 create mode 100644 app/sagas/connect.js

diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js
index 07aa655d4..207ace19f 100644
--- a/app/actions/actionsTypes.js
+++ b/app/actions/actionsTypes.js
@@ -2,15 +2,16 @@
 const REQUEST = 'REQUEST';
 const SUCCESS = 'SUCCESS';
 const FAILURE = 'FAILURE';
-
-function createRequestTypes(base) {
+const defaultTypes = [REQUEST, SUCCESS, FAILURE];
+function createRequestTypes(base, types = defaultTypes) {
 	const res = {};
-	[REQUEST, SUCCESS, FAILURE].forEach(type => res[type] = `${ base }_${ type }`);
+	types.forEach(type => res[type] = `${ base }_${ type }`);
 	return res;
 }
 
 // Login events
 export const LOGIN = createRequestTypes('LOGIN');
+export const METEOR = createRequestTypes('METEOR_CONNECT');
 export const LOGOUT = 'LOGOUT'; // logout is always success
 
 export const INCREMENT = 'INCREMENT';
diff --git a/app/actions/connect.js b/app/actions/connect.js
new file mode 100644
index 000000000..f745f6128
--- /dev/null
+++ b/app/actions/connect.js
@@ -0,0 +1,20 @@
+import * as types from './actionsTypes';
+
+export function connectRequest() {
+	return {
+		type: types.METEOR.REQUEST
+	};
+}
+
+export function connectSuccess() {
+	return {
+		type: types.METEOR.SUCCESS
+	};
+}
+
+export function connectFailure(err) {
+	return {
+		type: types.METEOR.FAILURE,
+		err
+	};
+}
diff --git a/app/lib/realm.js b/app/lib/realm.js
index b1219a3b3..5068a34b3 100644
--- a/app/lib/realm.js
+++ b/app/lib/realm.js
@@ -154,7 +154,7 @@ const messagesSchema = {
 	// }
 };
 
-// Realm.clearTestState();
+Realm.clearTestState();
 
 const realm = new Realm({
 	schema: [settingsSchema, serversSchema, subscriptionSchema, messagesSchema, usersSchema, attachment]
diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js
index eee2df778..49556a499 100644
--- a/app/lib/rocketchat.js
+++ b/app/lib/rocketchat.js
@@ -55,8 +55,6 @@ const RocketChat = {
 		Meteor.connect(url);
 
 		Meteor.ddp.on('connected', () => {
-			console.log('connected');
-
 			Meteor.call('public-settings/get', (err, data) => {
 				if (err) {
 					console.error(err);
@@ -79,7 +77,7 @@ const RocketChat = {
 				});
 				reduxStore.dispatch(actions.setAllSettings(settings));
 
-				if (cb) {
+				if (typeof cb === 'function') {
 					cb();
 				}
 			});
diff --git a/app/reducers/connect.js b/app/reducers/connect.js
new file mode 100644
index 000000000..5ace2ebfb
--- /dev/null
+++ b/app/reducers/connect.js
@@ -0,0 +1,32 @@
+import { METEOR } from '../actions/actionsTypes';
+
+const initialState = {
+	connecting: false,
+	connected: false,
+	errorMessage: '',
+	failure: false
+};
+
+export default function connect(state = initialState, action) {
+	switch (action.type) {
+		case METEOR.REQUEST:
+			return { ...state,
+				connecting: true
+			};
+		case METEOR.SUCCESS:
+			return { ...state,
+				connecting: false,
+				connected: true,
+				failure: false
+			};
+		case METEOR.FAILURE:
+			return { ...state,
+				connecting: false,
+				connected: false,
+				failure: true,
+				errorMessage: action.err
+			};
+		default:
+			return state;
+	}
+}
diff --git a/app/reducers/rootReducer.js b/app/reducers/rootReducer.js
index 609d73620..7c29d61c9 100644
--- a/app/reducers/rootReducer.js
+++ b/app/reducers/rootReducer.js
@@ -1,9 +1,10 @@
 import { combineReducers } from 'redux';
 import * as reducers from './reducers';
 import * as login from './login';
+import * as connect from './connect';
 
 const rootReducer = combineReducers({
-	...reducers, ...login
+	...reducers, ...login, ...connect
 });
 
 export default rootReducer;
diff --git a/app/sagas/connect.js b/app/sagas/connect.js
new file mode 100644
index 000000000..1b8dff2e0
--- /dev/null
+++ b/app/sagas/connect.js
@@ -0,0 +1,26 @@
+import { take, put, call, fork } from 'redux-saga/effects';
+import { METEOR } from '../actions/actionsTypes';
+import RocketChat from '../lib/rocketchat';
+
+import { connectSuccess, connectFailure } from '../actions/connect';
+
+function connect(...args) {
+	return RocketChat.connect(...args);
+}
+
+const watchConnect = function* watchConnect() {
+	while (true) {
+		yield take(METEOR.REQUEST);
+		try {
+			const response = yield call(connect);
+			yield put(connectSuccess(response));
+		} catch (err) {
+			yield put(connectFailure(err.status));
+		}
+	}
+};
+
+const root = function* root() {
+	yield fork(watchConnect);
+};
+export default root;
diff --git a/app/sagas/index.js b/app/sagas/index.js
index 61eeac14a..8bc2cf609 100644
--- a/app/sagas/index.js
+++ b/app/sagas/index.js
@@ -1,30 +1,12 @@
-import { take, fork } from 'redux-saga/effects';
+import { fork } from 'redux-saga/effects';
 import hello from './hello';
 import login from './login';
+import connect from './connect';
 
 const root = function* root() {
 	yield fork(hello);
 	yield fork(login);
+	yield fork(connect);
 };
 // 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 cfbf16287..dc12aedff 100644
--- a/app/sagas/login.js
+++ b/app/sagas/login.js
@@ -10,7 +10,7 @@ function loginCall(...args) {
 
 const watchLoginRequest = function* watchLoginRequest() {
 	while (true) {
-		// yield take('METEOR_CONNECTED');
+		yield take(types.METEOR.SUCCESS);
 		const payload = yield take(types.LOGIN.REQUEST);
 		try {
 			const response = yield call(loginCall, payload);
diff --git a/app/views/roomsList.js b/app/views/roomsList.js
index a904081ec..d249b663d 100644
--- a/app/views/roomsList.js
+++ b/app/views/roomsList.js
@@ -9,6 +9,7 @@ import Meteor from 'react-native-meteor';
 import { bindActionCreators } from 'redux';
 import { connect } from 'react-redux';
 import * as actions from '../actions';
+import * as meteor from '../actions/connect';
 import realm from '../lib/realm';
 import RocketChat from '../lib/rocketchat';
 import RoomItem from '../components/RoomItem';
@@ -96,7 +97,8 @@ class RoomsListItem extends React.PureComponent {
 	Site_Url: state.settings.Site_Url
 }), dispatch => ({
 	actions: bindActionCreators(actions, dispatch),
-	login: () => dispatch(actions.login())
+	login: () => dispatch(actions.login()),
+	connect: () => dispatch(meteor.connectRequest())
 }))
 
 export default class RoomsListView extends React.Component {
@@ -231,6 +233,7 @@ export default class RoomsListView extends React.Component {
 			subtitle: props.server
 		});
 
+		this.props.connect();
 		RocketChat.getUserToken().then((token) => {
 			if (!token) {
 				Navigation.showModal({
@@ -238,7 +241,8 @@ export default class RoomsListView extends React.Component {
 					animationType: 'slide-up'
 				});
 			}
-			RocketChat.connect();
+
+			// this.props.actions.connect();
 
 			const data = realm.objects('subscriptions').filtered('_server.id = $0', props.server).sorted('_updatedAt', true);
 
-- 
GitLab