2018-03-01 20:54:49 +01:00
|
|
|
import { call, put, select, takeLatest } from 'redux-saga/effects';
|
|
|
|
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';
|
|
|
|
import { validateChannelSelection, validatePublishParams } from 'utils/validate';
|
|
|
|
import { createPublishMetadata, createPublishFormData } from 'utils/publish';
|
2018-03-01 20:54:49 +01:00
|
|
|
|
|
|
|
const makePublishRequest = (fd) => {
|
|
|
|
console.log('making publish request');
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const uri = '/api/claim/publish';
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.upload.addEventListener('loadstart', () => {
|
2018-03-01 23:00:29 +01:00
|
|
|
put(updatePublishStatus(publishStates.LOAD_START, 'upload started'));
|
2018-03-01 20:54:49 +01:00
|
|
|
});
|
|
|
|
xhr.upload.addEventListener('progress', (e) => {
|
|
|
|
if (e.lengthComputable) {
|
|
|
|
const percentage = Math.round((e.loaded * 100) / e.total);
|
|
|
|
console.log('progress:', percentage);
|
2018-03-01 23:00:29 +01:00
|
|
|
put(updatePublishStatus(publishStates.LOADING, `${percentage}%`));
|
2018-03-01 20:54:49 +01:00
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
xhr.upload.addEventListener('load', () => {
|
|
|
|
console.log('loaded 100%');
|
2018-03-01 23:00:29 +01:00
|
|
|
put(updatePublishStatus(publishStates.PUBLISHING, null));
|
2018-03-01 20:54:49 +01:00
|
|
|
}, false);
|
|
|
|
xhr.open('POST', uri, true);
|
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
const response = JSON.parse(xhr.response);
|
2018-03-01 23:00:29 +01:00
|
|
|
// console.log('publish response:', response);
|
2018-03-01 20:54:49 +01:00
|
|
|
if ((xhr.status === 200) && response.success) {
|
2018-03-01 23:00:29 +01:00
|
|
|
resolve(response);
|
2018-03-01 20:54:49 +01:00
|
|
|
} else {
|
2018-03-01 23:00:29 +01:00
|
|
|
reject(new Error(response.message));
|
2018-03-01 20:54:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Initiate a multipart/form-data upload
|
|
|
|
xhr.send(fd);
|
|
|
|
});
|
2018-03-01 23:00:29 +01:00
|
|
|
};
|
2018-03-01 20:54:49 +01:00
|
|
|
|
2018-03-01 23:00:29 +01:00
|
|
|
function * publishFile (action) {
|
|
|
|
const { history } = action.data;
|
2018-03-01 20:54:49 +01:00
|
|
|
console.log('publishing file');
|
|
|
|
const { publishInChannel, selectedChannel, file, claim, metadata, error: { url: urlError } } = yield select(selectPublishState);
|
|
|
|
const { loggedInChannel } = yield select(selectChannelState);
|
|
|
|
// 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
|
|
|
|
const publishMetadata = createPublishMetadata(claim, file, metadata, publishInChannel, selectedChannel);
|
|
|
|
// create form data
|
|
|
|
const publishFormData = createPublishFormData(file, publishMetadata);
|
|
|
|
// make the publish request
|
2018-03-01 23:00:29 +01:00
|
|
|
let response;
|
2018-03-01 20:54:49 +01:00
|
|
|
try {
|
2018-03-01 23:00:29 +01:00
|
|
|
response = yield call(makePublishRequest, publishFormData);
|
|
|
|
yield put(clearFile());
|
|
|
|
history.push(`/${response.data.claimId}/${response.data.name}`);
|
2018-03-01 20:54:49 +01:00
|
|
|
} catch (error) {
|
2018-03-01 23:00:29 +01:00
|
|
|
return yield put(updatePublishStatus(publishStates.FAILED, error.message));
|
2018-03-01 20:54:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export function * watchPublishStart () {
|
|
|
|
yield takeLatest(actions.PUBLISH_START, publishFile);
|
|
|
|
};
|