2018-03-02 01:36:42 +01:00
|
|
|
import { call, put, select, take, takeLatest } from 'redux-saga/effects';
|
2018-03-01 20:54:49 +01:00
|
|
|
import * as actions from 'constants/publish_action_types';
|
2018-03-01 23:00:29 +01:00
|
|
|
import * as publishStates from 'constants/publish_claim_states';
|
|
|
|
import { updateError, updatePublishStatus, clearFile } from 'actions/publish';
|
|
|
|
import { selectPublishState } from 'selectors/publish';
|
|
|
|
import { selectChannelState } from 'selectors/channel';
|
2018-03-03 02:02:07 +01:00
|
|
|
import { selectSiteState } from 'selectors/site';
|
2018-03-01 23:00:29 +01:00
|
|
|
import { validateChannelSelection, validatePublishParams } from 'utils/validate';
|
2018-03-03 02:02:07 +01:00
|
|
|
import { createPublishMetadata, createPublishFormData, createThumbnailUrl } from 'utils/publish';
|
2018-03-02 01:36:42 +01:00
|
|
|
import { makePublishRequestChannel } from 'channels/publish';
|
2018-03-01 20:54:49 +01:00
|
|
|
|
2018-03-01 23:00:29 +01:00
|
|
|
function * publishFile (action) {
|
2018-03-01 20:54:49 +01:00
|
|
|
console.log('publishing file');
|
2018-03-02 02:16:04 +01:00
|
|
|
const { history } = action.data;
|
2018-03-03 02:02:07 +01:00
|
|
|
const { publishInChannel, selectedChannel, file, claim, metadata, thumbnailChannel, thumbnailChannelId, thumbnail, error: { url: urlError } } = yield select(selectPublishState);
|
2018-03-01 20:54:49 +01:00
|
|
|
const { loggedInChannel } = yield select(selectChannelState);
|
2018-03-03 02:02:07 +01:00
|
|
|
const { host } = yield select(selectSiteState);
|
2018-03-01 20:54:49 +01:00
|
|
|
// validate the channel selection
|
|
|
|
try {
|
2018-03-01 23:00:29 +01:00
|
|
|
validateChannelSelection(publishInChannel, selectedChannel, loggedInChannel);
|
2018-03-01 20:54:49 +01:00
|
|
|
} catch (error) {
|
2018-03-01 23:00:29 +01:00
|
|
|
return yield put(updateError('channel', error.message));
|
2018-03-01 20:54:49 +01:00
|
|
|
};
|
|
|
|
// validate publish parameters
|
|
|
|
try {
|
2018-03-01 23:00:29 +01:00
|
|
|
validatePublishParams(file, claim, urlError);
|
2018-03-01 20:54:49 +01:00
|
|
|
} catch (error) {
|
2018-03-01 23:00:29 +01:00
|
|
|
return yield put(updateError('publishSubmit', error.message));
|
2018-03-01 20:54:49 +01:00
|
|
|
}
|
|
|
|
// create metadata
|
2018-03-03 02:02:07 +01:00
|
|
|
let publishMetadata = createPublishMetadata(claim, file, metadata, publishInChannel, selectedChannel);
|
|
|
|
if (thumbnail) {
|
|
|
|
// add thumbnail to publish metadata
|
|
|
|
publishMetadata['thumbnail'] = createThumbnailUrl(thumbnailChannel, thumbnailChannelId, claim, host);
|
|
|
|
}
|
|
|
|
// create form data for main publish
|
2018-03-03 02:24:18 +01:00
|
|
|
const publishFormData = createPublishFormData(file, thumbnail, publishMetadata);
|
2018-03-01 20:54:49 +01:00
|
|
|
// make the publish request
|
2018-03-03 02:02:07 +01:00
|
|
|
const publishChannel = yield call(makePublishRequestChannel, publishFormData);
|
2018-03-03 02:24:18 +01:00
|
|
|
while (true) {
|
2018-03-03 02:02:07 +01:00
|
|
|
const {loadStart, progress, load, success, error} = yield take(publishChannel);
|
2018-03-02 01:36:42 +01:00
|
|
|
if (error) {
|
2018-03-03 02:24:18 +01:00
|
|
|
return yield put(updatePublishStatus(publishStates.FAILED, error.message));
|
2018-03-02 01:36:42 +01:00
|
|
|
}
|
|
|
|
if (success) {
|
|
|
|
yield put(clearFile());
|
2018-03-03 02:24:18 +01:00
|
|
|
return history.push(`/${success.data.claimId}/${success.data.name}`);
|
2018-03-02 01:36:42 +01:00
|
|
|
}
|
|
|
|
if (loadStart) {
|
2018-03-02 02:16:04 +01:00
|
|
|
yield put(updatePublishStatus(publishStates.LOAD_START, null));
|
2018-03-02 01:36:42 +01:00
|
|
|
}
|
|
|
|
if (progress) {
|
|
|
|
yield put(updatePublishStatus(publishStates.LOADING, `${progress}%`));
|
|
|
|
}
|
|
|
|
if (load) {
|
|
|
|
yield put(updatePublishStatus(publishStates.PUBLISHING, null));
|
|
|
|
}
|
2018-03-01 20:54:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export function * watchPublishStart () {
|
|
|
|
yield takeLatest(actions.PUBLISH_START, publishFile);
|
|
|
|
};
|