spee.ch/react/sagas/show_asset.js

60 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-02-15 03:02:10 +01:00
import { call, put, select, takeLatest } from 'redux-saga/effects';
2018-02-13 05:35:45 +01:00
import * as actions from 'constants/show_action_types';
import { addRequestToRequestList, onRequestError, onRequestUpdate, addAssetToAssetList } from 'actions/show';
2018-02-14 00:53:27 +01:00
import { getLongClaimId, getShortId, getClaimData } from 'api/assetApi';
2018-02-15 03:02:10 +01:00
import { selectShowState } from 'selectors/show';
2018-02-13 05:35:45 +01:00
export function * newAssetRequest (action) {
const { requestType, requestId, name, modifier } = action.data;
// put an action to update the request in redux
yield put(onRequestUpdate(requestType, requestId));
2018-02-15 03:02:10 +01:00
// is this an existing request?
// If this uri is in the request list, it's already been fetched
const state = yield select(selectShowState);
2018-02-15 03:02:10 +01:00
if (state.requestList[requestId]) {
console.log('that request already exists in the request list!');
return null;
}
// get long id && add request to request list
2018-02-14 00:53:27 +01:00
console.log(`getting asset long id ${name}`);
let longId;
2018-02-13 05:35:45 +01:00
try {
({data: longId} = yield call(getLongClaimId, name, modifier));
2018-02-13 05:35:45 +01:00
} catch (error) {
console.log('error:', error);
return yield put(onRequestError(error.message));
2018-02-13 05:35:45 +01:00
}
2018-02-14 21:09:57 +01:00
const assetKey = `a#${name}#${longId}`;
yield put(addRequestToRequestList(requestId, null, assetKey));
2018-02-15 03:02:10 +01:00
// is this an existing asset?
// If this asset is in the asset list, it's already been fetched
if (state.assetList[assetKey]) {
console.log('that asset already exists in the asset list!');
return null;
}
2018-02-14 00:53:27 +01:00
// get short Id
console.log(`getting asset short id ${name} ${longId}`);
let shortId;
try {
({data: shortId} = yield call(getShortId, name, longId));
} catch (error) {
return yield put(onRequestError(error.message));
2018-02-14 00:53:27 +01:00
}
2018-02-14 01:51:59 +01:00
// get asset claim data
2018-02-14 00:53:27 +01:00
console.log(`getting asset claim data ${name} ${longId}`);
let claimData;
try {
({data: claimData} = yield call(getClaimData, name, longId));
} catch (error) {
return yield put(onRequestError(error.message));
2018-02-14 00:53:27 +01:00
}
2018-02-15 03:02:10 +01:00
// add asset to asset list
2018-02-14 00:53:27 +01:00
yield put(addAssetToAssetList(assetKey, null, name, longId, shortId, claimData));
// clear any errors in request error
yield put(onRequestError(null));
2018-02-13 05:35:45 +01:00
};
export function * watchNewAssetRequest () {
2018-02-14 00:53:27 +01:00
yield takeLatest(actions.ASSET_REQUEST_NEW, newAssetRequest);
2018-02-13 05:35:45 +01:00
};