2018-02-13 05:35:45 +01:00
|
|
|
import { call, put, takeLatest } from 'redux-saga/effects';
|
|
|
|
import * as actions from 'constants/show_action_types';
|
2018-02-14 18:19:22 +01:00
|
|
|
import { addRequestToAssetRequests, onRequestError, addAssetToAssetList } from 'actions/show';
|
2018-02-14 00:53:27 +01:00
|
|
|
import { getLongClaimId, getShortId, getClaimData } from 'api/assetApi';
|
2018-02-13 05:35:45 +01:00
|
|
|
|
|
|
|
function* newAssetRequest (action) {
|
|
|
|
const { id, name, modifier } = action.data;
|
2018-02-14 00:53:27 +01:00
|
|
|
// get long id
|
|
|
|
console.log(`getting asset long id ${name}`);
|
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-13 21:19:57 +01:00
|
|
|
console.log('error:', 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 00:53:27 +01:00
|
|
|
// put action to add request to asset request list
|
|
|
|
yield put(addRequestToAssetRequests(id, null, name, longId));
|
|
|
|
// get short Id
|
|
|
|
console.log(`getting asset short id ${name} ${longId}`);
|
|
|
|
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
|
|
|
console.log(`getting asset claim data ${name} ${longId}`);
|
|
|
|
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
|
|
|
}
|
|
|
|
// put action to add asset to asset list
|
|
|
|
const assetKey = `a#${name}#${longId}`;
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
};
|