getrooms Saga

This commit is contained in:
Guilherme Gazzo 2017-08-16 23:06:22 -03:00
parent 4300b87035
commit b02ff73285
No known key found for this signature in database
GPG Key ID: 1F85C9AD922D0829
10 changed files with 123 additions and 39 deletions

View File

@ -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

View File

@ -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
};
}

20
app/actions/rooms.js Normal file
View File

@ -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
};
}

View File

@ -154,7 +154,7 @@ const messagesSchema = {
// }
};
// Realm.clearTestState();
Realm.clearTestState();
const realm = new Realm({
schema: [settingsSchema, serversSchema, subscriptionSchema, messagesSchema, usersSchema, attachment]

View File

@ -363,20 +363,9 @@ const RocketChat = {
});
}
},
logout() {
return AsyncStorage.clear();
}
};
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]) => {
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) => {
@ -398,10 +387,22 @@ Meteor.Accounts.onLogin(() => {
realm.create('subscriptions', subscription, true);
});
});
}).then(() => {
return data;
}).then((data) => {
console.log('subscriptions done.');
return data;
});
});
// });
},
logout() {
return AsyncStorage.clear();
}
};
export default RocketChat;
if (RocketChat.currentServer) {
reduxStore.dispatch(actions.setCurrentServer(RocketChat.currentServer));
}
// Use for logout
// AsyncStorage.clear();

View File

@ -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,

29
app/reducers/rooms.js Normal file
View File

@ -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;
}
}

View File

@ -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);
};

View File

@ -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));
}
}

31
app/sagas/rooms.js Normal file
View File

@ -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;