spee.ch/react/sagas/show_channel.js

66 lines
2.5 KiB
JavaScript
Raw Normal View History

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';
import { addNewChannelToChannelList, addRequestToRequestList, onRequestError, onRequestUpdate, updateChannelClaims } from 'actions/show';
2018-02-14 01:51:59 +01:00
import { getChannelClaims, getChannelData } from 'api/channelApi';
import { selectShowState } from 'selectors/show';
import { selectSiteHost } from 'selectors/site';
2018-02-08 23:17:03 +01:00
export function * newChannelRequest (action) {
const { requestType, requestId, channelName, channelId } = action.data;
// put an action to update the request in redux
yield put(onRequestUpdate(requestType, requestId));
// is this an existing request?
// If this uri is in the request list, it's already been fetched
const state = yield select(selectShowState);
const host = yield select(selectSiteHost);
if (state.requestList[requestId]) {
return null;
}
2018-02-14 01:51:59 +01:00
// get channel long id
let longId, shortId;
try {
({ data: {longChannelClaimId: longId, shortChannelClaimId: shortId} } = yield call(getChannelData, host, 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(addRequestToRequestList(requestId, null, channelKey));
// is this an existing channel?
// If this channel is in the channel list, it's already been fetched
if (state.channelList[channelKey]) {
return null;
}
2018-02-14 01:51:59 +01:00
// get channel claims data
let claimsData;
2018-02-08 23:17:03 +01:00
try {
({ data: claimsData } = yield call(getChannelClaims, host, longId, channelName, 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
}
export function * watchNewChannelRequest () {
yield takeLatest(actions.CHANNEL_REQUEST_NEW, newChannelRequest);
2018-02-08 23:17:03 +01:00
};
function * getNewClaimsAndUpdateChannel (action) {
const { channelKey, name, longId, page } = action.data;
const host = yield select(selectSiteHost);
let claimsData;
try {
({ data: claimsData } = yield call(getChannelClaims, host, longId, name, page));
} catch (error) {
return yield put(onRequestError(error.message));
}
yield put(updateChannelClaims(channelKey, claimsData));
}
export function * watchUpdateChannelClaims () {
2018-02-14 01:51:59 +01:00
yield takeLatest(actions.CHANNEL_CLAIMS_UPDATE_ASYNC, getNewClaimsAndUpdateChannel);
}