2020-06-12 07:17:17 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Spinner } from '@wordpress/components';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
|
|
|
import { useState } from '@wordpress/element';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Card, Pagination, EmptyContent } from '@woocommerce/components';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-08-13 02:05:22 +00:00
|
|
|
import { recordEvent } from '../../../lib/tracks';
|
2020-06-12 07:17:17 +00:00
|
|
|
|
|
|
|
const KnowledgeBase = ( {
|
|
|
|
posts,
|
|
|
|
isLoading,
|
|
|
|
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">
|
|
|
|
By { post.author_name }
|
|
|
|
{ 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 = () => {
|
|
|
|
const emptyTitle = __(
|
|
|
|
'There was an error loading knowledge base posts. Please check again later.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<EmptyContent
|
|
|
|
title={ emptyTitle }
|
|
|
|
illustrationWidth={ 250 }
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
const renderCardBody = () => {
|
|
|
|
if ( isLoading ) {
|
|
|
|
return <Spinner />;
|
|
|
|
}
|
|
|
|
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 = {
|
|
|
|
title: __( 'WooCommerce knowledge base', 'woocommerce-admin' ),
|
2020-07-28 02:32:58 +00:00
|
|
|
description: __(
|
|
|
|
'Learn the ins and outs of successful marketing from the experts at WooCommerce.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
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 ) => {
|
|
|
|
const { getBlogPosts, isResolving } = select( STORE_KEY );
|
|
|
|
|
|
|
|
return {
|
|
|
|
posts: getBlogPosts( props.category ),
|
|
|
|
isLoading: isResolving( 'getBlogPosts', [ props.category ] ),
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( ( dispatch ) => {
|
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
createNotice,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( KnowledgeBase );
|