diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js index 207ace19f..5b541dcd0 100644 --- a/app/actions/actionsTypes.js +++ b/app/actions/actionsTypes.js @@ -11,6 +11,7 @@ function createRequestTypes(base, types = defaultTypes) { // Login events export const LOGIN = createRequestTypes('LOGIN'); +export const ROOMS = createRequestTypes('ROOMS'); export const METEOR = createRequestTypes('METEOR_CONNECT'); export const LOGOUT = 'LOGOUT'; // logout is always success diff --git a/app/actions/login.js b/app/actions/login.js index cf9bd55e1..eb41082fa 100644 --- a/app/actions/login.js +++ b/app/actions/login.js @@ -7,11 +7,11 @@ export function loginRequest(credentials) { }; } -export function loginSuccess({ token, user }) { +export function loginSuccess(/* { token, user } */) { return { - type: types.LOGIN.SUCCESS, - token, - user + type: types.LOGIN.SUCCESS + // token, + // user }; } diff --git a/app/actions/rooms.js b/app/actions/rooms.js new file mode 100644 index 000000000..e22049e4b --- /dev/null +++ b/app/actions/rooms.js @@ -0,0 +1,20 @@ +import * as types from './actionsTypes'; + +export function roomsSuccessRequest() { + return { + type: types.ROOMS.REQUEST + }; +} + +export function roomsSuccess() { + return { + type: types.ROOMS.SUCCESS + }; +} + +export function roomsSuccessFailure(err) { + return { + type: types.ROOMS.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 11959ed30..a7666ed46 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -363,7 +363,37 @@ const RocketChat = { }); } }, - + getRooms() { + // Meteor.Accounts.onLogin(() => { + return Promise.all([call('subscriptions/get'), call('rooms/get')]).then(([subscriptions, rooms]) => { + subscriptions = subscriptions.sort((s1, s2) => (s1.rid > s2.rid ? 1 : -1)); + rooms = rooms.sort((s1, s2) => (s1._id > s2._id ? 1 : -1)); + const data = subscriptions.map((subscription, index) => { + subscription._updatedAt = rooms[index]._updatedAt; + return subscription; + }); + Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/subscriptions-changed`, false); + // Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/rooms-changed`, false); + realm.write(() => { + data.forEach((subscription) => { + // const subscription = { + // _id: item._id + // }; + // if (typeof item.value === 'string') { + // subscription.value = item.value; + // } + subscription._server = { id: RocketChat.currentServer }; + // write('subscriptions', subscription); + realm.create('subscriptions', subscription, true); + }); + }); + return data; + }).then((data) => { + console.log('subscriptions done.'); + return data; + }); + // }); + }, logout() { return AsyncStorage.clear(); } @@ -374,34 +404,5 @@ export default RocketChat; if (RocketChat.currentServer) { reduxStore.dispatch(actions.setCurrentServer(RocketChat.currentServer)); } - -Meteor.Accounts.onLogin(() => { - Promise.all([call('subscriptions/get'), call('rooms/get')]).then(([subscriptions, rooms]) => { - subscriptions = subscriptions.sort((s1, s2) => (s1.rid > s2.rid ? 1 : -1)); - rooms = rooms.sort((s1, s2) => (s1._id > s2._id ? 1 : -1)); - const data = subscriptions.map((subscription, index) => { - subscription._updatedAt = rooms[index]._updatedAt; - return subscription; - }); - Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/subscriptions-changed`, false); - // Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/rooms-changed`, false); - realm.write(() => { - data.forEach((subscription) => { - // const subscription = { - // _id: item._id - // }; - // if (typeof item.value === 'string') { - // subscription.value = item.value; - // } - subscription._server = { id: RocketChat.currentServer }; - // write('subscriptions', subscription); - realm.create('subscriptions', subscription, true); - }); - }); - }).then(() => { - console.log('subscriptions done.'); - }); -}); - // Use for logout // AsyncStorage.clear(); diff --git a/app/reducers/login.js b/app/reducers/login.js index 471f14988..5074077fa 100644 --- a/app/reducers/login.js +++ b/app/reducers/login.js @@ -19,9 +19,9 @@ export default function login(state = initialState, action) { return { ...state, isFetching: false, isAuthenticated: true, - token: action.token, - failure: false, - user: action.user + // token: action.token, + failure: false + // user: action.user }; case types.LOGIN.FAILURE: return { ...state, diff --git a/app/reducers/rooms.js b/app/reducers/rooms.js new file mode 100644 index 000000000..f1e158134 --- /dev/null +++ b/app/reducers/rooms.js @@ -0,0 +1,29 @@ +import * as types from '../actions/actionsTypes'; + +const initialState = { + isFetching: false, + failure: false +}; + +export default function login(state = initialState, action) { + switch (action.type) { + case types.ROOMS.REQUEST: + return { ...state, + isFetching: true + }; + case types.ROOMS.SUCCESS: + return { ...state, + isFetching: false + }; + case types.LOGIN.FAILURE: + return { ...state, + isFetching: false, + failure: true, + errorMessage: action.err + }; + // case types.LOGOUT: + // return initialState; + default: + return state; + } +} diff --git a/app/sagas/index.js b/app/sagas/index.js index 8bc2cf609..c2e3aead9 100644 --- a/app/sagas/index.js +++ b/app/sagas/index.js @@ -2,9 +2,11 @@ import { fork } from 'redux-saga/effects'; import hello from './hello'; import login from './login'; import connect from './connect'; +import rooms from './rooms'; const root = function* root() { yield fork(hello); + yield fork(rooms); yield fork(login); yield fork(connect); }; diff --git a/app/sagas/login.js b/app/sagas/login.js index 0f3c267e8..aec7be89c 100644 --- a/app/sagas/login.js +++ b/app/sagas/login.js @@ -5,7 +5,6 @@ import { loginSuccess, loginFailure } from '../actions/login'; import RocketChat from '../lib/rocketchat'; function loginCall(args) { - console.log(args); return RocketChat.loginWithPassword(args); } @@ -19,6 +18,7 @@ const watchLoginRequest = function* watchLoginRequest() { yield put(loginSuccess(response)); console.log('\n\n[LOGIN SUCCESS]\n\n'); } catch (err) { + console.log('\n\n[LOGIN FAILURE]\n\n', err); yield put(loginFailure(err.status)); } } diff --git a/app/sagas/rooms.js b/app/sagas/rooms.js new file mode 100644 index 000000000..f02a445a1 --- /dev/null +++ b/app/sagas/rooms.js @@ -0,0 +1,31 @@ +import { take, put, call, fork } from 'redux-saga/effects'; +import * as types from '../actions/actionsTypes'; +import { roomsSuccess, roomsFailure } from '../actions/rooms'; +import RocketChat from '../lib/rocketchat'; + +function getRooms(...args) { + // console.log('\n\n\n\n\n\naqui\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n'); + return RocketChat.getRooms(...args); +} + +const watchRoomsRequest = function* watchRoomsRequest() { + // console.log('\n\n\n\n\n\n\n\nWAINTING FOR LOGINsss\n\n\n\n\n\n\n\n'); + while (true) { + // console.log('\n\n\n\n\n\n\n\nWAINTING FOR LOGIN\n\n\n\n\n\n\n\n'); + yield take(types.LOGIN.SUCCESS); + // console.log('\n\n\n\n\n\n\n\nWAINTING FOR LOGIN NO MORE\n\n\n\n\n\n\n\n'); + // const payload = yield take(types.ROOMS.REQUEST); + try { + yield call(getRooms); + yield put(roomsSuccess()); + } catch (err) { + console.log(err); + yield put(roomsFailure(err.status)); + } + } +}; + +const root = function* root() { + yield fork(watchRoomsRequest); +}; +export default root;