import { isEnabled } from '@automattic/calypso-config';
import { Dialog, Gridicon } from '@automattic/components';
import { Icon, linkOff } from '@wordpress/icons';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { PureComponent } from 'react';
import { connect } from 'react-redux';
import QuerySharePostActions from 'calypso/components/data/query-share-post-actions/index.jsx';
import EllipsisMenu from 'calypso/components/ellipsis-menu';
import { withLocalizedMoment } from 'calypso/components/localized-moment';
import Notice from 'calypso/components/notice';
import PopoverMenuItem from 'calypso/components/popover-menu/item';
import SectionNav from 'calypso/components/section-nav';
import NavItem from 'calypso/components/section-nav/item';
import NavTabs from 'calypso/components/section-nav/tabs';
import SocialLogo from 'calypso/components/social-logo';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import getPostSharePublishedActions from 'calypso/state/selectors/get-post-share-published-actions';
import getPostShareScheduledActions from 'calypso/state/selectors/get-post-share-scheduled-actions';
import { deletePostShareAction } from 'calypso/state/sharing/publicize/publicize-actions/actions';
import { SCHEDULED, PUBLISHED } from './constants';
import SharingPreviewModal from './sharing-preview-modal';
class PublicizeActionsList extends PureComponent {
static propTypes = {
siteId: PropTypes.number,
postId: PropTypes.number,
scheduledActions: PropTypes.array,
publishedActions: PropTypes.array,
};
state = {
selectedShareTab: PUBLISHED,
showDeleteDialog: false,
selectedScheduledShareId: null,
showPreviewModal: false,
previewMessage: '',
previewService: '',
};
setFooterSection = ( selectedShareTab ) => () => {
recordTracksEvent( 'calypso_publicize_action_tab_click', { tab: selectedShareTab } );
this.setState( { selectedShareTab } );
};
togglePreviewModal =
( message = '', service = '' ) =>
() => {
if ( this.state.showPreviewModal ) {
return this.setState( { showPreviewModal: false } );
}
this.setState( {
showPreviewModal: true,
previewMessage: message,
previewService: service,
} );
};
renderActionItem( item, index ) {
const { service, connectionName, date, message } = item;
const shareDate = this.props.moment( date ).format( 'llll' );
return (
{ service ? (
) : (
) }
{ connectionName }
{ shareDate }
{ message }
{ this.renderFooterSectionItemActions( item ) }
);
}
renderFooterSectionItemActions( item ) {
const { ID: actionId, message, service, url } = item;
if ( this.state.selectedShareTab === SCHEDULED ) {
return this.renderScheduledMenu( actionId, message, service );
}
// PUBLISHED tab
return (
url && (
)
);
}
renderScheduledMenu( publicizeActionId, message, service ) {
const actions = [];
const { translate } = this.props;
if ( isEnabled( 'publicize-preview' ) ) {
actions.push(
{ translate( 'Preview' ) }
);
}
actions.push(
{ translate( 'Trash' ) }
);
if ( actions.length === 0 ) {
return ;
}
return { actions };
}
deleteScheduledAction( actionId ) {
return () => {
this.setState( {
showDeleteDialog: true,
selectedScheduledShareId: actionId,
} );
};
}
closeDeleteDialog = ( dialogAction ) => {
if ( dialogAction === 'delete' ) {
const { siteId, postId } = this.props;
recordTracksEvent( 'calypso_publicize_scheduled_delete' );
this.props.deletePostShareAction( siteId, postId, this.state.selectedScheduledShareId );
}
this.setState( { showDeleteDialog: false } );
};
renderActionsList = () => {
const { publishedActions, scheduledActions, translate } = this.props;
if ( this.state.selectedShareTab === PUBLISHED ) {
return (
{ publishedActions.map( ( item, index ) => this.renderActionItem( item, index ) ) }
);
}
if ( scheduledActions.length === 0 ) {
return (
);
}
return (
{ scheduledActions.map( ( item, index ) => this.renderActionItem( item, index ) ) }
);
};
renderDeleteDialog() {
const { translate } = this.props;
const buttons = [
{ action: 'cancel', label: translate( 'Cancel' ) },
{ action: 'delete', label: translate( 'Delete scheduled share' ), isPrimary: true },
];
return (
);
}
getShareTabLabel( shareTab ) {
const { translate } = this.props;
return shareTab === SCHEDULED ? translate( 'Scheduled' ) : translate( 'Published' );
}
getShareTabCount( shareTab ) {
return shareTab === SCHEDULED
? this.props.scheduledActions.length
: this.props.publishedActions.length;
}
renderShareTab( shareTab ) {
return (
{ this.getShareTabLabel( shareTab ) }
);
}
render() {
const { hasRepublicizeFeature, postId, siteId } = this.props;
const tabs = hasRepublicizeFeature ? [ SCHEDULED, PUBLISHED ] : [ PUBLISHED ];
return (
{ tabs.map( this.renderShareTab, this ) }
{ this.renderActionsList() }
{ this.renderDeleteDialog() }
);
}
}
export default connect(
( state, { postId, siteId } ) => {
return {
scheduledActions: getPostShareScheduledActions( state, siteId, postId ),
publishedActions: getPostSharePublishedActions( state, siteId, postId ),
};
},
{ deletePostShareAction }
)( localize( withLocalizedMoment( PublicizeActionsList ) ) );