import React from 'react';
import PropTypes from 'prop-types';
import { useDispatch } from 'react-redux';
import * as timeago from 'timeago.js';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import {
Avatar, Icon, IconButton, OverlayTrigger, Tooltip,
} from '@edx/paragon';
import {
Pin,
QuestionAnswer,
StarFilled,
StarOutline,
} from '@edx/paragon/icons';
import { updateExistingThread } from '../data/thunks';
import messages from './messages';
import LikeButton from './LikeButton';
export const postShape = PropTypes.shape({
abuseFlagged: PropTypes.bool,
author: PropTypes.string,
commentCount: PropTypes.number,
courseId: PropTypes.string,
following: PropTypes.bool,
id: PropTypes.string,
pinned: PropTypes.bool,
rawBody: PropTypes.string,
read: PropTypes.bool,
title: PropTypes.string,
topicId: PropTypes.string,
type: PropTypes.string,
updatedAt: PropTypes.string,
});
function PostTypeIcon(props) {
return (
{props.type === 'question' && }
{props.type === 'discussion' && }
{props.pinned && (
)}
);
}
PostTypeIcon.propTypes = {
type: PropTypes.string.isRequired,
pinned: PropTypes.bool.isRequired,
};
function PostHeader({
intl,
post,
}) {
return (
{post.title}
{intl.formatMessage(
messages.postedOn,
{
author: post.author,
time: timeago.format(post.createdAt, intl.locale),
},
)}
{post.commentCount}
);
}
PostHeader.propTypes = {
intl: intlShape.isRequired,
post: postShape.isRequired,
};
function Post({
post,
intl,
}) {
const dispatch = useDispatch();
return (
dispatch(updateExistingThread(post.id, { voted: !post.voted }))}
voted={post.voted}
/>
{intl.formatMessage(post.following ? messages.unfollow : messages.follow)}
)}
>
{
dispatch(updateExistingThread(post.id, { following: !post.following }));
return true;
}}
alt="Follow"
iconAs={Icon}
size="inline"
src={post.following ? StarFilled : StarOutline}
/>
);
}
Post.propTypes = {
intl: intlShape.isRequired,
post: postShape.isRequired,
};
export default injectIntl(Post);