split sagas into separate files

This commit is contained in:
bill bittner 2018-02-08 14:17:03 -08:00
parent 764ce24676
commit e9baab3c61
6 changed files with 149 additions and 135 deletions

38
react/sagas/file.js Normal file
View file

@ -0,0 +1,38 @@
import { call, put, takeLatest } from 'redux-saga/effects';
import * as actions from 'constants/show_action_types';
import { updateFileAvailability, updateDisplayAssetError } from 'actions/show';
import { UNAVAILABLE, AVAILABLE } from 'constants/asset_display_states';
import { checkFileAvailability, triggerClaimGet } from 'api/fileApi';
function* retrieveFile (action) {
const name = action.data.name;
const claimId = action.data.claimId;
// see if the file is available
let success, message, isAvailable;
try {
({ success, message, data: isAvailable } = yield call(checkFileAvailability, name, claimId));
} catch (error) {
return yield put(updateDisplayAssetError(error.message));
};
if (!success) {
return yield put(updateDisplayAssetError(message));
}
if (isAvailable) {
return yield put(updateFileAvailability(AVAILABLE));
}
yield put(updateFileAvailability(UNAVAILABLE));
// initiate get request for the file
try {
({ success, message } = yield call(triggerClaimGet, name, claimId));
} catch (error) {
return yield put(updateDisplayAssetError(error.message));
};
if (!success) {
return yield put(updateDisplayAssetError(message));
}
yield put(updateFileAvailability(AVAILABLE));
};
export function* watchFileIsRequested () {
yield takeLatest(actions.FILE_REQUESTED, retrieveFile);
};

View file

@ -1,11 +1,14 @@
import { all } from 'redux-saga/effects';
import { watchNewAssetRequest, watchShowNewAsset, watchNewChannelRequest, watchShowNewChannel, watchFileIsRequested } from './show';
import { watchNewAssetRequest, watchNewChannelRequest } from './request';
import { watchFileIsRequested } from './file';
import { watchShowNewChannel } from './show_channel';
import { watchShowNewAsset } from './show_asset';
export default function* rootSaga () {
yield all([
watchNewAssetRequest(),
watchShowNewAsset(),
watchNewChannelRequest(),
watchShowNewAsset(),
watchShowNewChannel(),
watchFileIsRequested(),
]);

47
react/sagas/request.js Normal file
View file

@ -0,0 +1,47 @@
import { call, put, takeLatest } from 'redux-saga/effects';
import * as actions from 'constants/show_action_types';
import { addAssetRequest, updateRequestError, showNewAsset, addChannelRequest, showNewChannel } from 'actions/show';
import { getLongClaimId } from 'api/assetApi';
import { getChannelData } from 'api/channelApi';
function* newAssetRequest (action) {
const { id, name, modifier } = action.data;
let success, message, longId;
try {
({success, message, data: longId} = yield call(getLongClaimId, name, modifier));
} catch (error) {
return yield put(updateRequestError(error.message));
}
if (!success) {
return yield put(updateRequestError(message));
}
yield put(addAssetRequest(id, null, name, longId));
yield put(showNewAsset(name, longId));
};
function* newChannelRequest (action) {
const { id, name, channelId } = action.data;
let success, message, data;
try {
({success, message, data} = yield call(getChannelData, name, channelId));
} catch (error) {
// return yield put(addChannelRequest(id, error.message, null, null, null));
return yield put(updateRequestError(message));
}
if (!success) {
// return yield put(addChannelRequest(id, message, null, null, null));
return yield put(updateRequestError(message));
}
const { longChannelClaimId: longId, shortChannelClaimId: shortId } = data;
yield put(addChannelRequest(id, null, name, longId, shortId));
const channelData = {name, longId, shortId};
yield put(showNewChannel(channelData));
}
export function* watchNewAssetRequest () {
yield takeLatest(actions.ASSET_REQUEST_NEW, newAssetRequest);
};
export function* watchNewChannelRequest () {
yield takeLatest(actions.CHANNEL_REQUEST_NEW, newChannelRequest);
};

View file

@ -1,133 +0,0 @@
import { call, put, takeLatest } from 'redux-saga/effects';
import * as actions from 'constants/show_action_types';
import { addAssetRequest, updateRequestError, showNewAsset, updateShowAsset, addAssetToAssetList, addChannelRequest, showNewChannel, updateShowChannel, addNewChannelToChannelList, updateFileAvailability, updateDisplayAssetError } from 'actions/show';
import { UNAVAILABLE, AVAILABLE } from 'constants/asset_display_states';
import { checkFileAvailability, triggerClaimGet } from 'api/fileApi';
import { getLongClaimId, getShortId, getClaimData } from 'api/assetApi';
import { getChannelData, getChannelClaims } from 'api/channelApi';
function* newAssetRequest (action) {
const { id, name, modifier } = action.data;
let success, message, longId;
try {
({success, message, data: longId} = yield call(getLongClaimId, name, modifier));
} catch (error) {
return yield put(updateRequestError(error.message));
}
if (!success) {
return yield put(updateRequestError(message));
}
yield put(addAssetRequest(id, null, name, longId));
yield put(showNewAsset(name, longId));
};
function* getAssetDataAndShowAsset (action) {
const {id, name, claimId} = action.data;
// get short Id
let success, message, shortId;
try {
({success, message, data: shortId} = yield call(getShortId, name, claimId));
} catch (error) {
return yield put(updateShowAsset(error.message, name, claimId));
}
if (!success) {
return yield put(updateShowAsset(message, name, claimId));
}
// if no error, get claim data
success = null;
let claimData;
try {
({success, message, data: claimData} = yield call(getClaimData, name, claimId));
} catch (error) {
return yield put(updateShowAsset(error.message, name, claimId));
}
if (!success) {
return yield put(updateShowAsset(message, name, claimId));
}
yield put(updateShowAsset(null, name, claimId, shortId, claimData));
yield put(addAssetToAssetList(id, null, name, claimId, shortId, claimData));
}
function* retrieveFile (action) {
const name = action.data.name;
const claimId = action.data.claimId;
// see if the file is available
let success, message, isAvailable;
try {
({ success, message, data: isAvailable } = yield call(checkFileAvailability, name, claimId));
} catch (error) {
return yield put(updateDisplayAssetError(error.message));
};
if (!success) {
return yield put(updateDisplayAssetError(message));
}
if (isAvailable) {
return yield put(updateFileAvailability(AVAILABLE));
}
yield put(updateFileAvailability(UNAVAILABLE));
// initiate get request for the file
try {
({ success, message } = yield call(triggerClaimGet, name, claimId));
} catch (error) {
return yield put(updateDisplayAssetError(error.message));
};
if (!success) {
return yield put(updateDisplayAssetError(message));
}
yield put(updateFileAvailability(AVAILABLE));
};
function* newChannelRequest (action) {
const { id, name, channelId } = action.data;
let success, message, data;
try {
({success, message, data} = yield call(getChannelData, name, channelId));
} catch (error) {
// return yield put(addChannelRequest(id, error.message, null, null, null));
return yield put(updateRequestError(message));
}
if (!success) {
// return yield put(addChannelRequest(id, message, null, null, null));
return yield put(updateRequestError(message));
}
const { longChannelClaimId: longId, shortChannelClaimId: shortId } = data;
yield put(addChannelRequest(id, null, name, longId, shortId));
const channelData = {name, longId, shortId};
yield put(showNewChannel(channelData));
}
function* getNewChannelDataAndShowChannel (action) {
const { id, channelData: {name, shortId, longId} } = action.data;
let success, message, claimsData;
try {
({ success, message, data: claimsData } = yield call(getChannelClaims, name, longId, 1));
} catch (error) {
return yield put(updateShowChannel(error.message, name, shortId, longId));
}
if (!success) {
return yield put(updateShowChannel(message, name, shortId, longId));
}
yield put(updateShowChannel(null, name, shortId, longId, claimsData));
const channelData = {name, shortId, longId};
yield put(addNewChannelToChannelList(id, null, channelData, claimsData));
}
export function* watchNewAssetRequest () {
yield takeLatest(actions.ASSET_REQUEST_NEW, newAssetRequest);
};
export function* watchNewChannelRequest () {
yield takeLatest(actions.CHANNEL_REQUEST_NEW, newChannelRequest);
};
export function* watchShowNewAsset () {
yield takeLatest(actions.SHOW_ASSET_NEW, getAssetDataAndShowAsset);
};
export function* watchShowNewChannel () {
yield takeLatest(actions.SHOW_CHANNEL_NEW, getNewChannelDataAndShowChannel);
};
export function* watchFileIsRequested () {
yield takeLatest(actions.FILE_REQUESTED, retrieveFile);
};

35
react/sagas/show_asset.js Normal file
View file

@ -0,0 +1,35 @@
import { call, put, takeLatest } from 'redux-saga/effects';
import * as actions from 'constants/show_action_types';
import { updateShowAsset, addAssetToAssetList } from 'actions/show';
import { getShortId, getClaimData } from 'api/assetApi';
function* getAssetDataAndShowAsset (action) {
const {id, name, claimId} = action.data;
// get short Id
let success, message, shortId;
try {
({success, message, data: shortId} = yield call(getShortId, name, claimId));
} catch (error) {
return yield put(updateShowAsset(error.message, name, claimId));
}
if (!success) {
return yield put(updateShowAsset(message, name, claimId));
}
// if no error, get claim data
success = null;
let claimData;
try {
({success, message, data: claimData} = yield call(getClaimData, name, claimId));
} catch (error) {
return yield put(updateShowAsset(error.message, name, claimId));
}
if (!success) {
return yield put(updateShowAsset(message, name, claimId));
}
yield put(updateShowAsset(null, name, claimId, shortId, claimData));
yield put(addAssetToAssetList(id, null, name, claimId, shortId, claimData));
}
export function* watchShowNewAsset () {
yield takeLatest(actions.SHOW_ASSET_NEW, getAssetDataAndShowAsset);
};

View file

@ -0,0 +1,24 @@
import { call, put, takeLatest } from 'redux-saga/effects';
import * as actions from 'constants/show_action_types';
import { updateShowChannel, addNewChannelToChannelList } from 'actions/show';
import { getChannelClaims } from 'api/channelApi';
function* getNewChannelDataAndShowChannel (action) {
const { id, channelData: {name, shortId, longId} } = action.data;
let success, message, claimsData;
try {
({ success, message, data: claimsData } = yield call(getChannelClaims, name, longId, 1));
} catch (error) {
return yield put(updateShowChannel(error.message, name, shortId, longId));
}
if (!success) {
return yield put(updateShowChannel(message, name, shortId, longId));
}
yield put(updateShowChannel(null, name, shortId, longId, claimsData));
const channelData = {name, shortId, longId};
yield put(addNewChannelToChannelList(id, null, channelData, claimsData));
}
export function* watchShowNewChannel () {
yield takeLatest(actions.SHOW_CHANNEL_NEW, getNewChannelDataAndShowChannel);
};