2020-06-12 07:17:17 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
|
|
|
import { useState } from '@wordpress/element';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-10-23 06:41:45 +00:00
|
|
|
import { Pagination, EmptyContent } from '@woocommerce/components';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-07-28 02:32:58 +00:00
|
|
|
import './style.scss';
|
2020-06-12 07:17:17 +00:00
|
|
|
import { Slider } from '../../components';
|
|
|
|
import { STORE_KEY } from '../../data/constants';
|
2020-10-23 06:41:45 +00:00
|
|
|
import Card from '../card';
|
2020-10-23 06:07:58 +00:00
|
|
|
import ReadBlogMessage from './ReadBlogMessage';
|
2020-11-23 10:15:07 +00:00
|
|
|
import KnowledgebaseCardPostPlaceholder from './placeholder';
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
const KnowledgeBase = ( {
|
|
|
|
posts,
|
|
|
|
isLoading,
|
2020-10-19 15:18:21 +00:00
|
|
|
error,
|
2020-06-12 07:17:17 +00:00
|
|
|
title,
|
|
|
|
description,
|
|
|
|
category,
|
|
|
|
} ) => {
|
2020-07-28 02:32:58 +00:00
|
|
|
const [ page, updatePage ] = useState( 1 );
|
|
|
|
const [ animate, updateAnimate ] = useState( null );
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
const onPaginationPageChange = ( newPage ) => {
|
|
|
|
let newAnimate;
|
|
|
|
if ( newPage > page ) {
|
|
|
|
newAnimate = 'left';
|
2020-07-28 02:32:58 +00:00
|
|
|
recordEvent( 'marketing_knowledge_carousel', {
|
|
|
|
direction: 'forward',
|
|
|
|
page: newPage,
|
|
|
|
} );
|
2020-06-12 07:17:17 +00:00
|
|
|
} else {
|
|
|
|
newAnimate = 'right';
|
2020-07-28 02:32:58 +00:00
|
|
|
recordEvent( 'marketing_knowledge_carousel', {
|
|
|
|
direction: 'back',
|
|
|
|
page: newPage,
|
|
|
|
} );
|
2020-06-12 07:17:17 +00:00
|
|
|
}
|
2020-07-28 02:32:58 +00:00
|
|
|
updatePage( newPage );
|
|
|
|
updateAnimate( newAnimate );
|
|
|
|
};
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
const onPostClick = ( post ) => {
|
|
|
|
recordEvent( 'marketing_knowledge_article', { title: post.title } );
|
2020-07-28 02:32:58 +00:00
|
|
|
};
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the 2 posts we need for the current page
|
|
|
|
*/
|
|
|
|
const getCurrentSlide = () => {
|
2020-07-28 02:32:58 +00:00
|
|
|
const currentPosts = posts.slice(
|
|
|
|
( page - 1 ) * 2,
|
|
|
|
( page - 1 ) * 2 + 2
|
|
|
|
);
|
|
|
|
const pageClass = classNames(
|
|
|
|
'woocommerce-marketing-knowledgebase-card__page',
|
|
|
|
{
|
|
|
|
'page-with-single-post': currentPosts.length === 1,
|
|
|
|
}
|
|
|
|
);
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
const displayPosts = currentPosts.map( ( post, index ) => {
|
|
|
|
return (
|
|
|
|
<a
|
|
|
|
className="woocommerce-marketing-knowledgebase-card__post"
|
|
|
|
href={ post.link }
|
|
|
|
key={ index }
|
2020-07-28 02:32:58 +00:00
|
|
|
onClick={ () => {
|
|
|
|
onPostClick( post );
|
|
|
|
} }
|
2020-06-12 07:17:17 +00:00
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
|
|
|
{ post.image && (
|
2020-07-28 02:32:58 +00:00
|
|
|
<div className="woocommerce-marketing-knowledgebase-card__post-img">
|
|
|
|
<img src={ post.image } alt="" />
|
|
|
|
</div>
|
2020-06-12 07:17:17 +00:00
|
|
|
) }
|
|
|
|
<div className="woocommerce-marketing-knowledgebase-card__post-text">
|
|
|
|
<h3>{ post.title }</h3>
|
|
|
|
<p className="woocommerce-marketing-knowledgebase-card__post-meta">
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'By', 'woocommerce' ) + ' ' }
|
2020-11-23 10:15:07 +00:00
|
|
|
{ post.author_name }
|
2020-06-12 07:17:17 +00:00
|
|
|
{ post.author_avatar && (
|
|
|
|
<img
|
2020-07-28 02:32:58 +00:00
|
|
|
src={ post.author_avatar.replace(
|
|
|
|
's=96',
|
|
|
|
's=32'
|
|
|
|
) }
|
2020-06-12 07:17:17 +00:00
|
|
|
className="woocommerce-gravatar"
|
|
|
|
alt=""
|
|
|
|
width="16"
|
|
|
|
height="16"
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</a>
|
2020-07-28 02:32:58 +00:00
|
|
|
);
|
2020-06-12 07:17:17 +00:00
|
|
|
} );
|
|
|
|
|
2020-07-28 02:32:58 +00:00
|
|
|
return <div className={ pageClass }>{ displayPosts }</div>;
|
|
|
|
};
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
const renderEmpty = () => {
|
2022-03-30 09:00:04 +00:00
|
|
|
const emptyTitle = __( 'No posts yet', 'woocommerce' );
|
2020-10-19 15:18:21 +00:00
|
|
|
|
|
|
|
return (
|
2020-10-23 06:07:58 +00:00
|
|
|
<EmptyContent
|
|
|
|
title={ emptyTitle }
|
|
|
|
message={ <ReadBlogMessage /> }
|
|
|
|
illustration=""
|
|
|
|
actionLabel=""
|
|
|
|
/>
|
2020-10-19 15:18:21 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderError = () => {
|
2020-10-23 06:07:58 +00:00
|
|
|
const errorTitle = __(
|
|
|
|
"Oops, our posts aren't loading right now",
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-06-12 07:17:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<EmptyContent
|
2020-10-23 06:07:58 +00:00
|
|
|
title={ errorTitle }
|
|
|
|
message={ <ReadBlogMessage /> }
|
|
|
|
illustration=""
|
2020-06-12 07:17:17 +00:00
|
|
|
actionLabel=""
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderPosts = () => {
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-marketing-knowledgebase-card__posts">
|
|
|
|
<Slider animationKey={ page } animate={ animate }>
|
|
|
|
{ getCurrentSlide() }
|
|
|
|
</Slider>
|
|
|
|
<Pagination
|
|
|
|
page={ page }
|
|
|
|
perPage={ 2 }
|
|
|
|
total={ posts.length }
|
|
|
|
onPageChange={ onPaginationPageChange }
|
|
|
|
showPagePicker={ false }
|
|
|
|
showPerPagePicker={ false }
|
|
|
|
showPageArrowsLabel={ false }
|
|
|
|
/>
|
|
|
|
</div>
|
2020-07-28 02:32:58 +00:00
|
|
|
);
|
2020-06-12 07:17:17 +00:00
|
|
|
};
|
|
|
|
|
2020-11-23 10:15:07 +00:00
|
|
|
/**
|
|
|
|
* Renders two `KnowledgebaseCardPostPlaceholder`s wrapped to mimic {@link renderPosts()} output.
|
|
|
|
*/
|
|
|
|
const renderPlaceholder = () => {
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-marketing-knowledgebase-card__posts">
|
|
|
|
<div className="woocommerce-marketing-knowledgebase-card__page">
|
|
|
|
<KnowledgebaseCardPostPlaceholder />
|
|
|
|
<KnowledgebaseCardPostPlaceholder />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-12 07:17:17 +00:00
|
|
|
const renderCardBody = () => {
|
|
|
|
if ( isLoading ) {
|
2020-11-23 10:15:07 +00:00
|
|
|
return renderPlaceholder();
|
2020-06-12 07:17:17 +00:00
|
|
|
}
|
2020-10-19 15:18:21 +00:00
|
|
|
|
|
|
|
if ( error ) {
|
|
|
|
return renderError();
|
|
|
|
}
|
|
|
|
|
2020-06-12 07:17:17 +00:00
|
|
|
return posts.length === 0 ? renderEmpty() : renderPosts();
|
|
|
|
};
|
|
|
|
|
2020-07-28 02:32:58 +00:00
|
|
|
const categoryClass = category
|
|
|
|
? `woocommerce-marketing-knowledgebase-card__category-${ category }`
|
|
|
|
: '';
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Card
|
|
|
|
title={ title }
|
|
|
|
description={ description }
|
|
|
|
className={ classNames(
|
|
|
|
'woocommerce-marketing-knowledgebase-card',
|
2020-07-28 02:32:58 +00:00
|
|
|
categoryClass
|
2020-06-12 07:17:17 +00:00
|
|
|
) }
|
|
|
|
>
|
|
|
|
{ renderCardBody() }
|
|
|
|
</Card>
|
2020-07-28 02:32:58 +00:00
|
|
|
);
|
|
|
|
};
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
KnowledgeBase.propTypes = {
|
|
|
|
/**
|
|
|
|
* Array of posts.
|
|
|
|
*/
|
|
|
|
posts: PropTypes.arrayOf( PropTypes.object ).isRequired,
|
|
|
|
/**
|
|
|
|
* Whether the card is loading.
|
|
|
|
*/
|
|
|
|
isLoading: PropTypes.bool.isRequired,
|
|
|
|
/**
|
|
|
|
* Cart title.
|
|
|
|
*/
|
|
|
|
title: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* Card description.
|
|
|
|
*/
|
|
|
|
description: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* Category of extensions to display.
|
|
|
|
*/
|
|
|
|
category: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
KnowledgeBase.defaultProps = {
|
2022-03-30 09:00:04 +00:00
|
|
|
title: __( 'WooCommerce knowledge base', 'woocommerce' ),
|
2020-07-28 02:32:58 +00:00
|
|
|
description: __(
|
|
|
|
'Learn the ins and outs of successful marketing from the experts at WooCommerce.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-07-28 02:32:58 +00:00
|
|
|
),
|
2020-06-12 07:17:17 +00:00
|
|
|
};
|
|
|
|
|
2020-07-28 02:32:58 +00:00
|
|
|
export { KnowledgeBase };
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
2020-10-19 15:18:21 +00:00
|
|
|
const { getBlogPosts, getBlogPostsError, isResolving } = select(
|
|
|
|
STORE_KEY
|
|
|
|
);
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
posts: getBlogPosts( props.category ),
|
|
|
|
isLoading: isResolving( 'getBlogPosts', [ props.category ] ),
|
2020-10-19 15:18:21 +00:00
|
|
|
error: getBlogPostsError( props.category ),
|
2020-06-12 07:17:17 +00:00
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( ( dispatch ) => {
|
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
createNotice,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( KnowledgeBase );
|