aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/CommentsList/CommentsList.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-02-14 13:57:06 +0100
committerArmand Philippot <git@armandphilippot.com>2022-02-14 14:15:54 +0100
commit637350e4d152de1346857d645bda8443900ec6f4 (patch)
tree23b43c7ce7e25935efe5b1dd156ad186334ee956 /src/components/CommentsList/CommentsList.tsx
parent9c150f28e4a23dfb4965fc3a4cdee16933a3def5 (diff)
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.
Diffstat (limited to 'src/components/CommentsList/CommentsList.tsx')
-rw-r--r--src/components/CommentsList/CommentsList.tsx27
1 files changed, 24 insertions, 3 deletions
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<CommentData[]>(
+ '/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 <Spinner />;
+
+ return data.map((comment) => {
return (
- <Comment key={comment.id} articleId={articleId} comment={comment} />
+ <Comment
+ key={comment.commentId}
+ articleId={articleId}
+ comment={comment}
+ />
);
});
};
@@ -28,7 +49,7 @@ const CommentsList = ({
description: 'CommentsList: Comments section title',
})}
</h2>
- {comments.length > 0 ? (
+ {data && data.length > 0 ? (
<ol className={styles.list}>{getCommentsList()}</ol>
) : (
<p className={styles['no-comments']}>