spee.ch/react/sagas/show_asset.js

45 lines
1.6 KiB
JavaScript
Raw Normal View History

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 01:51:59 +01:00
import { addRequestToAssetRequests, updateRequestError, 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}`);
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);
2018-02-13 05:35:45 +01:00
return yield put(updateRequestError(error.message));
}
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) {
return yield put(updateRequestError(error.message));
}
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(updateRequestError(error.message));
}
// 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
yield put(updateRequestError(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
};