2018-02-08 23:17:03 +01:00
|
|
|
import { call, put, takeLatest } from 'redux-saga/effects';
|
|
|
|
import * as actions from 'constants/show_action_types';
|
2018-02-13 03:18:56 +01:00
|
|
|
import { updateRequestError, addAssetToAssetList } from 'actions/show';
|
2018-02-08 23:17:03 +01:00
|
|
|
import { getShortId, getClaimData } from 'api/assetApi';
|
|
|
|
|
|
|
|
function* getAssetDataAndShowAsset (action) {
|
|
|
|
const {id, name, claimId} = action.data;
|
|
|
|
// get short Id
|
2018-02-13 03:50:19 +01:00
|
|
|
console.log('getting short id');
|
2018-02-08 23:17:03 +01:00
|
|
|
let success, message, shortId;
|
|
|
|
try {
|
|
|
|
({success, message, data: shortId} = yield call(getShortId, name, claimId));
|
|
|
|
} catch (error) {
|
2018-02-13 03:18:56 +01:00
|
|
|
return yield put(updateRequestError(error.message));
|
2018-02-08 23:17:03 +01:00
|
|
|
}
|
|
|
|
if (!success) {
|
2018-02-13 03:18:56 +01:00
|
|
|
return yield put(updateRequestError(message));
|
2018-02-08 23:17:03 +01:00
|
|
|
}
|
|
|
|
// if no error, get claim data
|
2018-02-13 03:50:19 +01:00
|
|
|
console.log('getting claim data');
|
2018-02-08 23:17:03 +01:00
|
|
|
success = null;
|
|
|
|
let claimData;
|
|
|
|
try {
|
|
|
|
({success, message, data: claimData} = yield call(getClaimData, name, claimId));
|
|
|
|
} catch (error) {
|
2018-02-13 03:18:56 +01:00
|
|
|
return yield put(updateRequestError(error.message));
|
2018-02-08 23:17:03 +01:00
|
|
|
}
|
|
|
|
if (!success) {
|
2018-02-13 03:18:56 +01:00
|
|
|
return yield put(updateRequestError(message));
|
2018-02-08 23:17:03 +01:00
|
|
|
}
|
2018-02-13 03:18:56 +01:00
|
|
|
yield put(addAssetToAssetList(id, null, name, claimId, shortId, claimData));
|
|
|
|
yield put(updateRequestError(null));
|
2018-02-08 23:17:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function* watchShowNewAsset () {
|
|
|
|
yield takeLatest(actions.SHOW_ASSET_NEW, getAssetDataAndShowAsset);
|
|
|
|
};
|