2018-02-08 23:17:03 +01:00
|
|
|
import { call, put, takeLatest } from 'redux-saga/effects';
|
|
|
|
import * as actions from 'constants/show_action_types';
|
2018-02-14 18:19:22 +01:00
|
|
|
import { addNewChannelToChannelList, addRequestToChannelRequests, onRequestError, updateChannelClaims } from 'actions/show';
|
2018-02-14 01:51:59 +01:00
|
|
|
import { getChannelClaims, getChannelData } from 'api/channelApi';
|
2018-02-08 23:17:03 +01:00
|
|
|
|
2018-02-14 02:16:50 +01:00
|
|
|
function* getNewChannelAndUpdateChannelList (action) {
|
2018-02-14 01:51:59 +01:00
|
|
|
const { id, name, channelId } = action.data;
|
|
|
|
// get channel long id
|
|
|
|
console.log('getting channel long id and short id');
|
|
|
|
let longId, shortId;
|
|
|
|
try {
|
|
|
|
({ data: {longChannelClaimId: longId, shortChannelClaimId: shortId} } = yield call(getChannelData, name, channelId));
|
|
|
|
} catch (error) {
|
2018-02-14 18:19:22 +01:00
|
|
|
return yield put(onRequestError(error.message));
|
2018-02-14 01:51:59 +01:00
|
|
|
}
|
|
|
|
// store the request in the channel requests list
|
|
|
|
yield put(addRequestToChannelRequests(id, null, name, longId, shortId));
|
|
|
|
// get channel claims data
|
|
|
|
console.log('getting channel claims data');
|
2018-02-13 21:19:57 +01:00
|
|
|
let claimsData;
|
2018-02-08 23:17:03 +01:00
|
|
|
try {
|
2018-02-13 21:19:57 +01:00
|
|
|
({ data: claimsData } = yield call(getChannelClaims, name, longId, 1));
|
2018-02-08 23:17:03 +01:00
|
|
|
} catch (error) {
|
2018-02-14 18:19:22 +01:00
|
|
|
return yield put(onRequestError(error.message));
|
2018-02-08 23:17:03 +01:00
|
|
|
}
|
2018-02-14 01:51:59 +01:00
|
|
|
// store the channel data in the channel list
|
|
|
|
const channelKey = `c#${name}#${longId}`;
|
|
|
|
yield put(addNewChannelToChannelList(channelKey, name, shortId, longId, claimsData));
|
|
|
|
// clear any request errors
|
2018-02-14 18:19:22 +01:00
|
|
|
yield put(onRequestError(null));
|
2018-02-08 23:17:03 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 01:51:59 +01:00
|
|
|
export function* watchNewChannelRequest () {
|
2018-02-14 02:16:50 +01:00
|
|
|
yield takeLatest(actions.CHANNEL_REQUEST_NEW, getNewChannelAndUpdateChannelList);
|
2018-02-08 23:17:03 +01:00
|
|
|
};
|
2018-02-09 19:02:13 +01:00
|
|
|
|
2018-02-14 01:51:59 +01:00
|
|
|
function* getNewClaimsAndUpdateChannel (action) {
|
2018-02-13 03:18:56 +01:00
|
|
|
const { channelKey, name, longId, page } = action.data;
|
2018-02-13 21:19:57 +01:00
|
|
|
let claimsData;
|
2018-02-09 19:02:13 +01:00
|
|
|
try {
|
2018-02-13 21:19:57 +01:00
|
|
|
({ data: claimsData } = yield call(getChannelClaims, name, longId, page));
|
2018-02-09 19:02:13 +01:00
|
|
|
} catch (error) {
|
2018-02-14 18:19:22 +01:00
|
|
|
return yield put(onRequestError(error.message));
|
2018-02-09 19:02:13 +01:00
|
|
|
}
|
2018-02-13 03:18:56 +01:00
|
|
|
yield put(updateChannelClaims(channelKey, claimsData));
|
2018-02-09 19:02:13 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 01:51:59 +01:00
|
|
|
export function* watchUpdateChannelClaims () {
|
|
|
|
yield takeLatest(actions.CHANNEL_CLAIMS_UPDATE_ASYNC, getNewClaimsAndUpdateChannel);
|
2018-02-09 19:02:13 +01:00
|
|
|
}
|