spee.ch/react/sagas/show_asset.js

56 lines
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-03-16 00:31:01 +01:00
import { selectSiteHost } from 'selectors/site';
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-03-16 00:31:01 +01:00
const host = yield select(selectSiteHost);
2018-02-15 03:02:10 +01:00
if (state.requestList[requestId]) {
return null;
}
// get long id && add request to request list
let longId;
2018-02-13 05:35:45 +01:00
try {
2018-03-16 00:31:01 +01:00
({data: longId} = yield call(getLongClaimId, host, name, modifier));
2018-02-13 05:35:45 +01:00
} catch (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]) {
return null;
}
2018-02-14 00:53:27 +01:00
// get short Id
let shortId;
try {
2018-03-16 00:31:01 +01:00
({data: shortId} = yield call(getShortId, host, name, longId));
2018-02-14 00:53:27 +01:00
} 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
let claimData;
try {
2018-03-16 00:31:01 +01:00
({data: claimData} = yield call(getClaimData, host, name, longId));
2018-02-14 00:53:27 +01:00
} 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
};