2018-03-16 00:46:56 +01:00
|
|
|
import {call, put, select, takeLatest} from 'redux-saga/effects';
|
2018-02-08 23:17:03 +01:00
|
|
|
import * as actions from 'constants/show_action_types';
|
|
|
|
import { updateFileAvailability, updateDisplayAssetError } from 'actions/show';
|
|
|
|
import { UNAVAILABLE, AVAILABLE } from 'constants/asset_display_states';
|
|
|
|
import { checkFileAvailability, triggerClaimGet } from 'api/fileApi';
|
2018-03-16 00:46:56 +01:00
|
|
|
import { selectSiteHost } from 'selectors/site';
|
2018-02-08 23:17:03 +01:00
|
|
|
|
2018-02-22 02:02:57 +01:00
|
|
|
function * retrieveFile (action) {
|
2018-02-08 23:17:03 +01:00
|
|
|
const name = action.data.name;
|
|
|
|
const claimId = action.data.claimId;
|
2018-03-16 00:46:56 +01:00
|
|
|
const host = yield select(selectSiteHost);
|
2018-02-08 23:17:03 +01:00
|
|
|
// see if the file is available
|
2018-02-13 21:19:57 +01:00
|
|
|
let isAvailable;
|
2018-02-08 23:17:03 +01:00
|
|
|
try {
|
2018-03-16 00:46:56 +01:00
|
|
|
({ data: isAvailable } = yield call(checkFileAvailability, claimId, host, name));
|
2018-02-08 23:17:03 +01:00
|
|
|
} catch (error) {
|
|
|
|
return yield put(updateDisplayAssetError(error.message));
|
|
|
|
};
|
|
|
|
if (isAvailable) {
|
2018-02-15 02:10:44 +01:00
|
|
|
yield put(updateDisplayAssetError(null));
|
2018-02-08 23:17:03 +01:00
|
|
|
return yield put(updateFileAvailability(AVAILABLE));
|
|
|
|
}
|
|
|
|
yield put(updateFileAvailability(UNAVAILABLE));
|
|
|
|
// initiate get request for the file
|
|
|
|
try {
|
2018-03-16 00:46:56 +01:00
|
|
|
yield call(triggerClaimGet, claimId, host, name);
|
2018-02-08 23:17:03 +01:00
|
|
|
} catch (error) {
|
|
|
|
return yield put(updateDisplayAssetError(error.message));
|
|
|
|
};
|
|
|
|
yield put(updateFileAvailability(AVAILABLE));
|
|
|
|
};
|
|
|
|
|
2018-02-22 02:02:57 +01:00
|
|
|
export function * watchFileIsRequested () {
|
2018-02-08 23:17:03 +01:00
|
|
|
yield takeLatest(actions.FILE_REQUESTED, retrieveFile);
|
|
|
|
};
|