/**
* 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
*/
import './style.scss';
import { Slider } from '../../components';
import { STORE_KEY } from '../../data/constants';
import { recordEvent } from '../../../lib/tracks';
const KnowledgeBase = ( {
posts,
isLoading,
title,
description,
category,
} ) => {
const [ page, updatePage ] = useState( 1 );
const [ animate, updateAnimate ] = useState( null );
const onPaginationPageChange = ( newPage ) => {
let newAnimate;
if ( newPage > page ) {
newAnimate = 'left';
recordEvent( 'marketing_knowledge_carousel', {
direction: 'forward',
page: newPage,
} );
} else {
newAnimate = 'right';
recordEvent( 'marketing_knowledge_carousel', {
direction: 'back',
page: newPage,
} );
}
updatePage( newPage );
updateAnimate( newAnimate );
};
const onPostClick = ( post ) => {
recordEvent( 'marketing_knowledge_article', { title: post.title } );
};
/**
* Get the 2 posts we need for the current page
*/
const getCurrentSlide = () => {
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,
}
);
const displayPosts = currentPosts.map( ( post, index ) => {
return (
{
onPostClick( post );
} }
target="_blank"
rel="noopener noreferrer"
>
{ post.image && (
By { post.author_name }
{ post.author_avatar && (
) }
{ post.title }