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';
|
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);
|
|
|
|
};
|
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
const get = function* get({ data }) {
|
|
|
|
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);
|
|
|
|
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
|
|
|
|
2017-09-01 19:42:50 +00:00
|
|
|
const getData = function* getData() {
|
2017-09-25 13:15:28 +00:00
|
|
|
yield takeLatest(CREATE_CHANNEL.REQUEST, get);
|
2017-09-01 19:42:50 +00:00
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2017-09-01 19:42:50 +00:00
|
|
|
export default getData;
|