From 637350e4d152de1346857d645bda8443900ec6f4 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 14 Feb 2022 13:57:06 +0100 Subject: fix: update comments list when a new comment is send The comments list was static before. If an user posted a comment, even after it was approved, the comments list was keeping the old state. --- src/components/Comment/Comment.tsx | 2 +- src/components/CommentsList/CommentsList.tsx | 27 ++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) (limited to 'src/components') diff --git a/src/components/Comment/Comment.tsx b/src/components/Comment/Comment.tsx index 4835f8c..ab1dffc 100644 --- a/src/components/Comment/Comment.tsx +++ b/src/components/Comment/Comment.tsx @@ -121,7 +121,7 @@ const Comment = ({ return ( diff --git a/src/components/CommentsList/CommentsList.tsx b/src/components/CommentsList/CommentsList.tsx index 6630a03..1e7c3e7 100644 --- a/src/components/CommentsList/CommentsList.tsx +++ b/src/components/CommentsList/CommentsList.tsx @@ -1,6 +1,9 @@ import Comment from '@components/Comment/Comment'; +import Spinner from '@components/Spinner/Spinner'; +import { getCommentsByPostId } from '@services/graphql/queries'; import { Comment as CommentData } from '@ts/types/comments'; import { useIntl } from 'react-intl'; +import useSWR from 'swr'; import styles from './CommentsList.module.scss'; const CommentsList = ({ @@ -11,11 +14,29 @@ const CommentsList = ({ comments: CommentData[]; }) => { const intl = useIntl(); + const { data, error } = useSWR( + '/api/comments', + () => getCommentsByPostId(articleId), + { fallbackData: comments } + ); const getCommentsList = () => { - return comments.map((comment) => { + if (error) { + return intl.formatMessage({ + defaultMessage: 'Failed to load.', + description: 'CommentsList: failed to load', + }); + } + + if (!data) return ; + + return data.map((comment) => { return ( - + ); }); }; @@ -28,7 +49,7 @@ const CommentsList = ({ description: 'CommentsList: Comments section title', })} - {comments.length > 0 ? ( + {data && data.length > 0 ? (
    {getCommentsList()}
) : (

-- cgit v1.2.3