spee.ch/react/sagas/file.js

34 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-02-08 23:17:03 +01:00
import { call, put, takeLatest } from 'redux-saga/effects';
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';
function * retrieveFile (action) {
2018-02-08 23:17:03 +01:00
const name = action.data.name;
const claimId = action.data.claimId;
// see if the file is available
let isAvailable;
2018-02-08 23:17:03 +01:00
try {
({ data: isAvailable } = yield call(checkFileAvailability, name, claimId));
2018-02-08 23:17:03 +01:00
} catch (error) {
return yield put(updateDisplayAssetError(error.message));
};
if (isAvailable) {
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 {
yield call(triggerClaimGet, name, claimId);
2018-02-08 23:17:03 +01:00
} catch (error) {
return yield put(updateDisplayAssetError(error.message));
};
yield put(updateFileAvailability(AVAILABLE));
};
export function * watchFileIsRequested () {
2018-02-08 23:17:03 +01:00
yield takeLatest(actions.FILE_REQUESTED, retrieveFile);
};