spee.ch/react/sagas/show_channel.js

51 lines
2 KiB
JavaScript
Raw Normal View History

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 21:09:57 +01:00
import { addNewChannelToChannelList, addRequestToPreviousRequests, 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) {
const { requestId, channelName, channelId } = action.data;
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 {
({ data: {longChannelClaimId: longId, shortChannelClaimId: shortId} } = yield call(getChannelData, channelName, channelId));
2018-02-14 01:51:59 +01:00
} catch (error) {
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}`;
yield put(addRequestToPreviousRequests(requestId, null, channelKey));
2018-02-14 01:51:59 +01:00
// get channel claims data
console.log('getting channel claims data');
let claimsData;
2018-02-08 23:17:03 +01:00
try {
({ data: claimsData } = yield call(getChannelClaims, channelName, longId, 1));
2018-02-08 23:17:03 +01:00
} catch (error) {
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
yield put(addNewChannelToChannelList(channelKey, channelName, shortId, longId, claimsData));
2018-02-14 01:51:59 +01:00
// clear any request errors
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-14 01:51:59 +01:00
function* getNewClaimsAndUpdateChannel (action) {
const { channelKey, name, longId, page } = action.data;
let claimsData;
try {
({ data: claimsData } = yield call(getChannelClaims, name, longId, page));
} catch (error) {
return yield put(onRequestError(error.message));
}
yield put(updateChannelClaims(channelKey, claimsData));
}
2018-02-14 01:51:59 +01:00
export function* watchUpdateChannelClaims () {
yield takeLatest(actions.CHANNEL_CLAIMS_UPDATE_ASYNC, getNewClaimsAndUpdateChannel);
}