import React, { useEffect, useMemo } from 'react'; import PropTypes from 'prop-types'; import { useDispatch, useSelector } from 'react-redux'; import { useParams } from 'react-router'; import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; import { Button, Spinner } from '@edx/paragon'; import { EndorsementStatus, ThreadType } from '../../data/constants'; import { useDispatchWithState } from '../../data/hooks'; import { Post } from '../posts'; import { selectThread } from '../posts/data/selectors'; import { fetchThread, markThreadAsRead } from '../posts/data/thunks'; import { filterPosts } from '../utils'; import { selectThreadComments, selectThreadCurrentPage, selectThreadHasMorePages } from './data/selectors'; import { fetchThreadComments } from './data/thunks'; import { Comment, ResponseEditor } from './comment'; import messages from './messages'; function usePost(postId) { const dispatch = useDispatch(); const thread = useSelector(selectThread(postId)); useEffect(() => { if (thread && !thread.read) { dispatch(markThreadAsRead(postId)); } }, [postId]); return thread; } function usePostComments(postId, endorsed = null) { const [isLoading, dispatch] = useDispatchWithState(); const comments = useSelector(selectThreadComments(postId, endorsed)); const hasMorePages = useSelector(selectThreadHasMorePages(postId, endorsed)); const currentPage = useSelector(selectThreadCurrentPage(postId, endorsed)); const handleLoadMoreResponses = async () => dispatch(fetchThreadComments(postId, { endorsed, page: currentPage + 1, })); useEffect(() => { dispatch(fetchThreadComments(postId, { endorsed, page: 1, })); }, [postId]); return { comments, hasMorePages, isLoading, handleLoadMoreResponses, }; } function DiscussionCommentsView({ postType, postId, intl, endorsed, isClosed, }) { const { comments, hasMorePages, isLoading, handleLoadMoreResponses, } = usePostComments(postId, endorsed); const sortedComments = useMemo(() => [...filterPosts(comments, 'endorsed'), ...filterPosts(comments, 'unendorsed')], [comments]); return ( <> {((hasMorePages && isLoading) || !isLoading) && (