2017-09-01 19:42:50 +00:00
|
|
|
import { delay } from 'redux-saga';
|
2017-09-25 13:15:28 +00:00
|
|
|
import { select, put, call, take, takeLatest } from 'redux-saga/effects';
|
2018-07-10 13:40:32 +00:00
|
|
|
import { NavigationActions } from '../Navigation';
|
|
|
|
|
2017-09-01 19:42:50 +00:00
|
|
|
import { CREATE_CHANNEL, LOGIN } from '../actions/actionsTypes';
|
|
|
|
import { createChannelSuccess, createChannelFailure } from '../actions/createChannel';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
|
|
|
|
const create = function* create(data) {
|
|
|
|
return yield RocketChat.createChannel(data);
|
|
|
|
};
|
|
|
|
|
2018-04-10 13:03:54 +00:00
|
|
|
const handleRequest = function* handleRequest({ data }) {
|
2017-09-21 17:08:00 +00:00
|
|
|
try {
|
2017-09-25 13:15:28 +00:00
|
|
|
yield delay(1000);
|
2017-09-21 17:08:00 +00:00
|
|
|
const auth = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (!auth) {
|
|
|
|
yield take(LOGIN.SUCCESS);
|
2017-09-01 19:42:50 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
const result = yield call(create, data);
|
2018-04-10 13:03:54 +00:00
|
|
|
const { rid, name } = result;
|
2018-07-10 13:40:32 +00:00
|
|
|
NavigationActions.popToRoot();
|
|
|
|
yield delay(1000);
|
|
|
|
NavigationActions.push({
|
|
|
|
screen: 'RoomView',
|
|
|
|
title: name,
|
|
|
|
passProps: {
|
|
|
|
room: { rid, name },
|
|
|
|
rid,
|
|
|
|
name
|
|
|
|
}
|
|
|
|
});
|
2017-09-21 17:08:00 +00:00
|
|
|
yield put(createChannelSuccess(result));
|
|
|
|
} catch (err) {
|
|
|
|
yield put(createChannelFailure(err));
|
2017-09-01 19:42:50 +00:00
|
|
|
}
|
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2018-04-10 13:03:54 +00:00
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(CREATE_CHANNEL.REQUEST, handleRequest);
|
2017-09-01 19:42:50 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-04-10 13:03:54 +00:00
|
|
|
export default root;
|