lbry-desktop/ui/redux/actions/publish.js

114 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import * as MODALS from 'constants/modal_types';
import * as ACTIONS from 'constants/action_types';
import * as PAGES from 'constants/pages';
import {
batchActions,
selectMyClaims,
doPublish,
2020-06-21 18:51:06 +02:00
doCheckPendingClaims,
2020-05-07 14:22:55 +02:00
doCheckReflectingFiles,
ACTIONS as LBRY_REDUX_ACTIONS,
2021-03-29 23:42:07 +02:00
makeSelectPublishFormValue,
makeSelectClaimForUri,
} from 'lbry-redux';
import { doError } from 'redux/actions/notifications';
2019-07-13 04:59:45 +02:00
import { push } from 'connected-react-router';
import analytics from 'analytics';
import { doOpenModal } from 'redux/actions/app';
2021-03-26 23:03:16 +01:00
export const NO_FILE = '---';
2020-11-16 20:09:00 +01:00
export const doPublishDesktop = (filePath: string, preview?: boolean) => (dispatch: Dispatch, getState: () => {}) => {
const publishPreview = (previewResponse) => {
dispatch(
doOpenModal(MODALS.PUBLISH_PREVIEW, {
previewResponse,
})
);
};
2021-03-29 23:42:07 +02:00
const noFileParam = !filePath || filePath === NO_FILE;
const state = getState();
const editingUri = makeSelectPublishFormValue('editingURI')(state) || '';
const claim = makeSelectClaimForUri(editingUri)(state) || {};
const hasSourceFile = claim.value && claim.value.source;
const redirectToLivestream = noFileParam && !hasSourceFile;
2021-03-26 23:03:16 +01:00
const publishSuccess = (successResponse, lbryFirstError) => {
const state = getState();
2019-07-13 04:59:45 +02:00
const myClaims = selectMyClaims(state);
2019-04-24 16:02:08 +02:00
const pendingClaim = successResponse.outputs[0];
2019-10-16 23:36:50 +02:00
analytics.apiLogPublish(pendingClaim);
const { permanent_url: url } = pendingClaim;
const actions = [];
// @if TARGET='app'
2020-07-23 19:02:07 +02:00
actions.push(push(`/$/${PAGES.UPLOADS}`));
// @endif
2019-11-07 20:39:22 +01:00
actions.push({
type: ACTIONS.PUBLISH_SUCCESS,
});
2021-03-29 23:42:07 +02:00
// We have to fake a temp claim until the new pending one is returned by claim_list_mine
// We can't rely on claim_list_mine because there might be some delay before the new claims are returned
// Doing this allows us to show the pending claim immediately, it will get overwritten by the real one
const isMatch = (claim) => claim.claim_id === pendingClaim.claim_id;
const isEdit = myClaims.some(isMatch);
actions.push({
type: LBRY_REDUX_ACTIONS.UPDATE_PENDING_CLAIMS,
data: {
claims: [pendingClaim],
},
});
2020-05-07 14:22:55 +02:00
// @if TARGET='app'
actions.push({
type: LBRY_REDUX_ACTIONS.ADD_FILES_REFLECTING,
data: pendingClaim,
});
// @endif
dispatch(batchActions(...actions));
dispatch(
doOpenModal(MODALS.PUBLISH, {
uri: url,
isEdit,
filePath,
lbryFirstError,
})
);
2020-07-23 16:22:57 +02:00
dispatch(doCheckPendingClaims());
2020-05-07 14:22:55 +02:00
// @if TARGET='app'
dispatch(doCheckReflectingFiles());
// @endif
2021-03-29 23:42:07 +02:00
if (redirectToLivestream) {
dispatch(push(`/$/${PAGES.LIVESTREAM}`));
}
};
2018-03-26 23:32:43 +02:00
const publishFail = (error) => {
const actions = [];
actions.push({
type: ACTIONS.PUBLISH_FAIL,
});
actions.push(doError(error.message));
dispatch(batchActions(...actions));
2018-03-26 23:32:43 +02:00
};
2019-07-09 08:02:08 +02:00
if (preview) {
dispatch(doPublish(publishSuccess, publishFail, publishPreview));
return;
}
// Redirect on web immediately because we have a file upload progress componenet
// on the publishes page. This doesn't exist on desktop so wait until we get a response
// from the SDK
// @if TARGET='web'
2021-03-29 23:42:07 +02:00
if (!redirectToLivestream) {
dispatch(push(`/$/${PAGES.UPLOADS}`));
}
// @endif
dispatch(doPublish(publishSuccess, publishFail));
2018-03-26 23:32:43 +02:00
};