diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-02-14 13:57:06 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-02-14 14:15:54 +0100 |
| commit | 637350e4d152de1346857d645bda8443900ec6f4 (patch) | |
| tree | 23b43c7ce7e25935efe5b1dd156ad186334ee956 /src/services | |
| parent | 9c150f28e4a23dfb4965fc3a4cdee16933a3def5 (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/services')
| -rw-r--r-- | src/services/graphql/queries.ts | 60 |
1 files changed, 41 insertions, 19 deletions
diff --git a/src/services/graphql/queries.ts b/src/services/graphql/queries.ts index a9b3141..688d3f2 100644 --- a/src/services/graphql/queries.ts +++ b/src/services/graphql/queries.ts @@ -1,6 +1,7 @@ import { Slug } from '@ts/types/app'; import { Article, PostBy, TotalArticles } from '@ts/types/articles'; import { AllPostsSlug, PostsList, RawPostsList } from '@ts/types/blog'; +import { Comment, CommentsByPostId } from '@ts/types/comments'; import { AllTopics, AllTopicsSlug, @@ -18,6 +19,8 @@ import { getFormattedPostPreview, getFormattedTopic, getFormattedThematic, + getFormattedComments, + buildCommentsTree, } from '@utils/helpers/format'; import { gql } from 'graphql-request'; import { fetchApi } from './api'; @@ -188,24 +191,6 @@ export const getPostBySlug = async (slug: string): Promise<Article> => { } } commentCount - comments(where: { order: ASC, orderby: COMMENT_DATE }) { - nodes { - approved - author { - node { - gravatarUrl - name - url - } - } - commentId - content - date - id - parentDatabaseId - parentId - } - } contentParts { afterMore beforeMore @@ -242,7 +227,6 @@ export const getPostBySlug = async (slug: string): Promise<Article> => { opengraphTitle opengraphType opengraphUrl - readingTime } title } @@ -255,6 +239,44 @@ export const getPostBySlug = async (slug: string): Promise<Article> => { }; //============================================================================== +// Comments query +//============================================================================== + +export const getCommentsByPostId = async (id: number): Promise<Comment[]> => { + const query = gql` + query MyQuery($id: Int) { + postBy(postId: $id) { + comments(where: { order: ASC, orderby: COMMENT_DATE }) { + nodes { + approved + author { + node { + gravatarUrl + id + name + url + } + } + commentId + content + date + parentDatabaseId + } + } + } + } + `; + + const variables = { id }; + const response = await fetchApi<CommentsByPostId>(query, variables); + const formattedComments = getFormattedComments( + response.postBy.comments.nodes + ); + + return buildCommentsTree(formattedComments); +}; + +//============================================================================== // Topic query //============================================================================== |
