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

72 lines
2.8 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, doError, selectMyClaims, doPublish, doCheckPendingPublishes } from 'lbry-redux';
2019-07-13 04:59:45 +02:00
import { selectosNotificationsEnabled } from 'redux/selectors/settings';
import { push } from 'connected-react-router';
import analytics from 'analytics';
import { formatLbryUriForWeb } from 'util/uri';
import { doOpenModal } from './app';
2018-03-26 23:32:43 +02:00
2019-11-01 18:27:01 +01:00
export const doPublishDesktop = (filePath: string) => (dispatch: Dispatch, getState: () => {}) => {
const publishSuccess = successResponse => {
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 = [];
actions.push({
type: ACTIONS.PUBLISH_SUCCESS,
});
// 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);
const myNewClaims = isEdit
2019-04-24 16:02:08 +02:00
? myClaims.map(claim => (isMatch(claim) ? pendingClaim : claim))
: myClaims.concat(pendingClaim);
2019-11-01 18:27:01 +01:00
actions.push(doOpenModal(MODALS.PUBLISH, { uri: url, isEdit, filePath }));
actions.push({
type: ACTIONS.FETCH_CLAIM_LIST_MINE_COMPLETED,
data: {
claims: myNewClaims,
},
});
dispatch(batchActions(...actions));
};
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
dispatch(push(`/$/${PAGES.PUBLISHED}`));
dispatch(doPublish(publishSuccess, publishFail));
2018-03-26 23:32:43 +02:00
};
// Calls claim_list_mine until any pending publishes are confirmed
2019-07-13 04:59:45 +02:00
export const doCheckPendingPublishesApp = () => (dispatch: Dispatch, getState: GetState) => {
const onConfirmed = claim => {
if (selectosNotificationsEnabled(getState())) {
const notif = new window.Notification('LBRY Publish Complete', {
2019-10-03 23:40:54 +02:00
body: __('%nameOrTitle% has been published to lbry://%name%. Click here to view it.', {
nameOrTitle: claim.value_type === 'channel' ? `@${claim.name}` : claim.value.title,
name: claim.name,
}),
2019-07-13 04:59:45 +02:00
silent: false,
});
notif.onclick = () => {
dispatch(push(formatLbryUriForWeb(claim.permanent_url)));
};
}
};
return dispatch(doCheckPendingPublishes(onConfirmed));
};