2018-02-15 03:08:39 +01:00
|
|
|
import {call, put, select, takeLatest} from 'redux-saga/effects';
|
2018-02-08 23:17:03 +01:00
|
|
|
import * as actions from 'constants/show_action_types';
|
2018-02-14 21:19:43 +01:00
|
|
|
import { addNewChannelToChannelList, addRequestToRequestList, onRequestError, updateChannelClaims } from 'actions/show';
|
2018-02-14 01:51:59 +01:00
|
|
|
import { getChannelClaims, getChannelData } from 'api/channelApi';
|
2018-02-15 03:08:39 +01:00
|
|
|
import { selectShowState } from 'selectors/show';
|
2018-02-08 23:17:03 +01:00
|
|
|
|
2018-02-22 02:02:57 +01:00
|
|
|
function * getNewChannelAndUpdateChannelList (action) {
|
2018-02-14 20:39:24 +01:00
|
|
|
const { requestId, channelName, channelId } = action.data;
|
2018-02-15 03:08:39 +01:00
|
|
|
const state = yield select(selectShowState);
|
|
|
|
// is this an existing request?
|
|
|
|
// If this uri is in the request list, it's already been fetched
|
|
|
|
if (state.requestList[requestId]) {
|
|
|
|
console.log('that request already exists in the request list!');
|
|
|
|
return null;
|
|
|
|
}
|
2018-02-14 01:51:59 +01:00
|
|
|
// get channel long id
|
|
|
|
console.log('getting channel long id and short id');
|
|
|
|
let longId, shortId;
|
|
|
|
try {
|
2018-02-14 20:39:24 +01:00
|
|
|
({ data: {longChannelClaimId: longId, shortChannelClaimId: shortId} } = yield call(getChannelData, channelName, channelId));
|
2018-02-14 01:51:59 +01:00
|
|
|
} 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
|
2018-02-14 21:09:57 +01:00
|
|
|
const channelKey = `c#${channelName}#${longId}`;
|
2018-02-14 21:19:43 +01:00
|
|
|
yield put(addRequestToRequestList(requestId, null, channelKey));
|
2018-02-15 03:08:39 +01:00
|
|
|
// is this an existing channel?
|
|
|
|
// If this channel is in the channel list, it's already been fetched
|
|
|
|
if (state.channelList[channelKey]) {
|
|
|
|
console.log('that channel already exists in the channel list!');
|
|
|
|
return null;
|
|
|
|
}
|
2018-02-14 01:51:59 +01:00
|
|
|
// 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-14 20:39:24 +01:00
|
|
|
({ data: claimsData } = yield call(getChannelClaims, channelName, 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
|
2018-02-14 20:39:24 +01:00
|
|
|
yield put(addNewChannelToChannelList(channelKey, channelName, shortId, longId, claimsData));
|
2018-02-14 01:51:59 +01:00
|
|
|
// 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-22 02:02:57 +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-22 02:02:57 +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-22 02:02:57 +01:00
|
|
|
export function * watchUpdateChannelClaims () {
|
2018-02-14 01:51:59 +01:00
|
|
|
yield takeLatest(actions.CHANNEL_CLAIMS_UPDATE_ASYNC, getNewClaimsAndUpdateChannel);
|
2018-02-09 19:02:13 +01:00
|
|
|
}
|