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';
|
2018-02-23 00:43:26 +01:00
|
|
|
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
|
|
|
|
2018-02-23 00:43:26 +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
|
2018-02-23 00:43:26 +01:00
|
|
|
const state = yield select(selectShowState);
|
2018-02-15 03:02:10 +01:00
|
|
|
if (state.requestList[requestId]) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// get long id && add request to request list
|
2018-02-13 21:19:57 +01:00
|
|
|
let longId;
|
2018-02-13 05:35:45 +01:00
|
|
|
try {
|
2018-02-13 21:19:57 +01:00
|
|
|
({data: longId} = yield call(getLongClaimId, name, modifier));
|
2018-02-13 05:35:45 +01:00
|
|
|
} catch (error) {
|
2018-02-14 18:19:22 +01:00
|
|
|
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}`;
|
2018-02-14 21:19:43 +01:00
|
|
|
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]) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-02-14 00:53:27 +01:00
|
|
|
// get short Id
|
|
|
|
let shortId;
|
|
|
|
try {
|
|
|
|
({data: shortId} = yield call(getShortId, name, longId));
|
|
|
|
} catch (error) {
|
2018-02-14 18:19:22 +01:00
|
|
|
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
|
|
|
let claimData;
|
|
|
|
try {
|
|
|
|
({data: claimData} = yield call(getClaimData, name, longId));
|
|
|
|
} catch (error) {
|
2018-02-14 18:19:22 +01:00
|
|
|
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
|
2018-02-14 18:19:22 +01:00
|
|
|
yield put(onRequestError(null));
|
2018-02-13 05:35:45 +01:00
|
|
|
};
|
|
|
|
|
2018-02-22 02:02:57 +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
|
|
|
};
|