/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { Component, createRef, Fragment } from '@wordpress/element'; import { Button, Dropdown, Modal } from '@wordpress/components'; import PropTypes from 'prop-types'; import VisibilitySensor from 'react-visibility-sensor'; import moment from 'moment'; /** * Internal dependencies */ import NoteAction from './action'; import sanitizeHTML from 'lib/sanitize-html'; import classnames from 'classnames'; import { recordEvent } from 'lib/tracks'; import './style.scss'; import { H, Section } from '@woocommerce/components'; import { getUrlParams } from 'utils'; class InboxNoteCard extends Component { constructor( props ) { super( props ); this.onVisible = this.onVisible.bind( this ); this.hasBeenSeen = false; this.state = { isDismissModalOpen: false, dismissType: null, }; this.openDismissModal = this.openDismissModal.bind( this ); this.closeDismissModal = this.closeDismissModal.bind( this ); this.bodyNotificationRef = createRef(); this.screen = this.getScreenName(); } componentDidMount() { if ( this.bodyNotificationRef.current ) { this.bodyNotificationRef.current.addEventListener( 'click', ( event ) => this.handleBodyClick( event, this.props ) ); } } componentWillUnmount() { if ( this.bodyNotificationRef.current ) { this.bodyNotificationRef.current.removeEventListener( 'click', ( event ) => this.handleBodyClick( event, this.props ) ); } } handleBodyClick( event, props ) { const innerLink = event.target.href; if ( innerLink ) { const { note } = props; recordEvent( 'wcadmin_inbox_action_click', { note_name: note.name, note_title: note.title, note_content_inner_link: innerLink, screen: this.screen, } ); } } getScreenName() { let screenName = ''; const { page, path, post_type: postType } = getUrlParams( window.location.search ); if ( page ) { const currentPage = page === 'wc-admin' ? 'home_screen' : page; screenName = path ? path.replace( /\//g, '_' ).substring( 1 ) : currentPage; } else if ( postType ) { screenName = postType; } return screenName; } // Trigger a view Tracks event when the note is seen. onVisible( isVisible ) { if ( isVisible && ! this.hasBeenSeen ) { const { note } = this.props; recordEvent( 'inbox_note_view', { note_content: note.content, note_name: note.name, note_title: note.title, note_type: note.type, screen: this.screen, } ); this.hasBeenSeen = true; } } openDismissModal( type, onToggle ) { this.setState( { isDismissModalOpen: true, dismissType: type, } ); onToggle(); } closeDismissModal( noteNameDismissConfirmation ) { const { dismissType } = this.state; const { note } = this.props; const noteNameDismissAll = dismissType === 'all' ? true : false; recordEvent( 'inbox_action_dismiss', { note_name: note.name, note_title: note.title, note_name_dismiss_all: noteNameDismissAll, note_name_dismiss_confirmation: noteNameDismissConfirmation || false, screen: this.screen, } ); this.setState( { isDismissModalOpen: false, } ); } handleBlur( event, onClose ) { const dropdownClasses = [ 'woocommerce-admin-dismiss-notification', 'components-popover__content', ]; const isClickOutsideDropdown = event.relatedTarget ? dropdownClasses.some( ( className ) => event.relatedTarget.className.includes( className ) ) : false; if ( isClickOutsideDropdown ) { event.preventDefault(); } else { onClose(); } } renderDismissButton() { return ( ( ) } focusOnMount={ false } popoverProps={ { noArrow: true } } renderContent={ ( { onToggle } ) => ( ) } /> ); } getDismissConfirmationButton() { const { note } = this.props; const { dismissType } = this.state; return ( ); } renderDismissConfirmationModal() { return ( { __( 'Are you sure?', 'woocommerce-admin' ) } } onRequestClose={ () => this.closeDismissModal() } className="woocommerce-inbox-dismiss-confirmation_modal" >

{ __( 'Dismissed messages cannot be viewed again', 'woocommerce-admin' ) }

{ this.getDismissConfirmationButton() }
); } renderActions( note ) { const { actions: noteActions, id: noteId } = note; if ( ! noteActions ) { return; } return ( { noteActions.map( ( action, index ) => ( ) ) } ); } render() { const { lastRead, note } = this.props; const { isDismissModalOpen } = this.state; const { content, date_created: dateCreated, date_created_gmt: dateCreatedGmt, image, is_deleted: isDeleted, layout, title, } = note; if ( isDeleted ) { return null; } const unread = ! lastRead || ! dateCreatedGmt || new Date( dateCreatedGmt + 'Z' ).getTime() > lastRead; const date = dateCreated; const hasImage = layout !== 'plain' && layout !== ''; const cardClassName = classnames( 'woocommerce-inbox-message', layout, { 'message-is-unread': unread, } ); return (
{ hasImage && (
) }
{ date && ( { moment.utc( date ).fromNow() } ) } { title }
{ this.renderActions( note ) } { this.renderDismissButton() }
{ isDismissModalOpen && this.renderDismissConfirmationModal() }
); } } InboxNoteCard.propTypes = { note: PropTypes.shape( { id: PropTypes.number, status: PropTypes.string, title: PropTypes.string, content: PropTypes.string, date_created: PropTypes.string, date_created_gmt: PropTypes.string, actions: PropTypes.arrayOf( PropTypes.shape( { id: PropTypes.number.isRequired, url: PropTypes.string, label: PropTypes.string.isRequired, primary: PropTypes.bool.isRequired, } ) ), layout: PropTypes.string, image: PropTypes.string, is_deleted: PropTypes.bool, } ), lastRead: PropTypes.number, }; export default InboxNoteCard;