getrooms Saga
This commit is contained in:
parent
4300b87035
commit
b02ff73285
|
@ -11,6 +11,7 @@ function createRequestTypes(base, types = defaultTypes) {
|
||||||
|
|
||||||
// Login events
|
// Login events
|
||||||
export const LOGIN = createRequestTypes('LOGIN');
|
export const LOGIN = createRequestTypes('LOGIN');
|
||||||
|
export const ROOMS = createRequestTypes('ROOMS');
|
||||||
export const METEOR = createRequestTypes('METEOR_CONNECT');
|
export const METEOR = createRequestTypes('METEOR_CONNECT');
|
||||||
export const LOGOUT = 'LOGOUT'; // logout is always success
|
export const LOGOUT = 'LOGOUT'; // logout is always success
|
||||||
|
|
||||||
|
|
|
@ -7,11 +7,11 @@ export function loginRequest(credentials) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function loginSuccess({ token, user }) {
|
export function loginSuccess(/* { token, user } */) {
|
||||||
return {
|
return {
|
||||||
type: types.LOGIN.SUCCESS,
|
type: types.LOGIN.SUCCESS
|
||||||
token,
|
// token,
|
||||||
user
|
// user
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
};
|
||||||
|
}
|
|
@ -154,7 +154,7 @@ const messagesSchema = {
|
||||||
// }
|
// }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Realm.clearTestState();
|
Realm.clearTestState();
|
||||||
|
|
||||||
const realm = new Realm({
|
const realm = new Realm({
|
||||||
schema: [settingsSchema, serversSchema, subscriptionSchema, messagesSchema, usersSchema, attachment]
|
schema: [settingsSchema, serversSchema, subscriptionSchema, messagesSchema, usersSchema, attachment]
|
||||||
|
|
|
@ -363,20 +363,9 @@ const RocketChat = {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
getRooms() {
|
||||||
logout() {
|
// Meteor.Accounts.onLogin(() => {
|
||||||
return AsyncStorage.clear();
|
return Promise.all([call('subscriptions/get'), call('rooms/get')]).then(([subscriptions, rooms]) => {
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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));
|
subscriptions = subscriptions.sort((s1, s2) => (s1.rid > s2.rid ? 1 : -1));
|
||||||
rooms = rooms.sort((s1, s2) => (s1._id > s2._id ? 1 : -1));
|
rooms = rooms.sort((s1, s2) => (s1._id > s2._id ? 1 : -1));
|
||||||
const data = subscriptions.map((subscription, index) => {
|
const data = subscriptions.map((subscription, index) => {
|
||||||
|
@ -398,10 +387,22 @@ Meteor.Accounts.onLogin(() => {
|
||||||
realm.create('subscriptions', subscription, true);
|
realm.create('subscriptions', subscription, true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}).then(() => {
|
return data;
|
||||||
|
}).then((data) => {
|
||||||
console.log('subscriptions done.');
|
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
|
// Use for logout
|
||||||
// AsyncStorage.clear();
|
// AsyncStorage.clear();
|
||||||
|
|
|
@ -19,9 +19,9 @@ export default function login(state = initialState, action) {
|
||||||
return { ...state,
|
return { ...state,
|
||||||
isFetching: false,
|
isFetching: false,
|
||||||
isAuthenticated: true,
|
isAuthenticated: true,
|
||||||
token: action.token,
|
// token: action.token,
|
||||||
failure: false,
|
failure: false
|
||||||
user: action.user
|
// user: action.user
|
||||||
};
|
};
|
||||||
case types.LOGIN.FAILURE:
|
case types.LOGIN.FAILURE:
|
||||||
return { ...state,
|
return { ...state,
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,9 +2,11 @@ import { fork } from 'redux-saga/effects';
|
||||||
import hello from './hello';
|
import hello from './hello';
|
||||||
import login from './login';
|
import login from './login';
|
||||||
import connect from './connect';
|
import connect from './connect';
|
||||||
|
import rooms from './rooms';
|
||||||
|
|
||||||
const root = function* root() {
|
const root = function* root() {
|
||||||
yield fork(hello);
|
yield fork(hello);
|
||||||
|
yield fork(rooms);
|
||||||
yield fork(login);
|
yield fork(login);
|
||||||
yield fork(connect);
|
yield fork(connect);
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,7 +5,6 @@ import { loginSuccess, loginFailure } from '../actions/login';
|
||||||
import RocketChat from '../lib/rocketchat';
|
import RocketChat from '../lib/rocketchat';
|
||||||
|
|
||||||
function loginCall(args) {
|
function loginCall(args) {
|
||||||
console.log(args);
|
|
||||||
return RocketChat.loginWithPassword(args);
|
return RocketChat.loginWithPassword(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +18,7 @@ const watchLoginRequest = function* watchLoginRequest() {
|
||||||
yield put(loginSuccess(response));
|
yield put(loginSuccess(response));
|
||||||
console.log('\n\n[LOGIN SUCCESS]\n\n');
|
console.log('\n\n[LOGIN SUCCESS]\n\n');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.log('\n\n[LOGIN FAILURE]\n\n', err);
|
||||||
yield put(loginFailure(err.status));
|
yield put(loginFailure(err.status));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
Loading…
Reference in New Issue