1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
import { SingleComment } from '@ts/types/app';
import { GraphQLEdgesInput } from '@ts/types/graphql/generics';
import { SendCommentInput, SentComment } from '@ts/types/graphql/mutations';
import { ContentId } from '@ts/types/graphql/queries';
import { RawComment, RawCommentsPage } from '@ts/types/raw-data';
import { getAuthorFromRawData } from '@utils/helpers/author';
import { fetchAPI } from './api';
import { sendCommentMutation } from './comments.mutation';
import { commentsQuery } from './comments.query';
/**
* Convert a comment from RawComment type to SingleComment type.
*
* @param {RawComment} comment - A raw comment.
* @returns {SingleComment} A formatted comment.
*/
export const getCommentFromRawData = (comment: RawComment): SingleComment => {
const { author, databaseId, date, parentDatabaseId, ...data } = comment;
return {
id: databaseId,
meta: {
author: getAuthorFromRawData(author.node, 'comment'),
date,
},
parentId: parentDatabaseId === 0 ? undefined : parentDatabaseId,
replies: [],
...data,
};
};
/**
* Convert an array of RawComment type to an array of SingleComment type.
*
* @param {RawComment[]} comments - The raw comments.
* @returns {SingleComment[]} The formatted comments.
*/
export const getCommentsFromRawData = (
comments: RawComment[]
): SingleComment[] => {
return comments.map((comment) => getCommentFromRawData(comment));
};
/**
* Create a comments tree with replies.
*
* @param {SingleComment[]} comments - A flatten comments list.
* @returns {SingleComment[]} An array of comments with replies.
*/
export const buildCommentsTree = (
comments: SingleComment[]
): SingleComment[] => {
type CommentsHashTable = {
[key: string]: SingleComment;
};
const hashTable: CommentsHashTable = Object.create(null);
const commentsTree: SingleComment[] = [];
comments.forEach(
(comment) => (hashTable[comment.id] = { ...comment, replies: [] })
);
comments.forEach((comment) => {
if (!comment.parentId) {
commentsTree.push(hashTable[comment.id]);
} else {
hashTable[comment.parentId].replies.push(hashTable[comment.id]);
}
});
return commentsTree;
};
type FetchCommentsInput = ContentId &
Pick<GraphQLEdgesInput, 'after' | 'first'>;
/**
* Retrieve a raw comments page from GraphQL.
*
* @param {FetchCommentsInput} variables - An object of variables.
* @returns {Promise<RawCommentsPage>} A raw comments page.
*/
export const fetchRawComments = async (
variables: FetchCommentsInput
): Promise<RawCommentsPage> => {
const response = await fetchAPI<RawComment, typeof commentsQuery>({
query: commentsQuery,
variables,
});
return {
comments: response.comments.edges.map((edge) => edge.node),
hasNextPage: response.comments.pageInfo.hasNextPage,
endCursor: response.comments.pageInfo.endCursor,
};
};
/**
* Fetch recursively all the comments on a post.
*
* @param {FetchCommentsInput} variables - An object of query variables.
* @param {RawCommentsPage[]} pages - An accumulator to keep track of pages.
* @returns {Promise<RawCommentsPage[]>} The raw comments pages.
*/
export const fetchAllRawCommentsPages = async (
variables: FetchCommentsInput,
pages: RawCommentsPage[] = []
): Promise<RawCommentsPage[]> => {
return fetchRawComments(variables).then((page) => {
pages.push(page);
if (page.hasNextPage) {
return fetchAllRawCommentsPages(
{ ...variables, after: page.endCursor },
pages
);
} else {
return pages;
}
});
};
/**
* Method to compare two comments dates and sort them from older to newest.
*
* @param {SingleComment} a - A comment.
* @param {SingleComment} b - Another comment.
* @returns {number} The difference between dates.
*/
export const compareCommentsDate = (
a: SingleComment,
b: SingleComment
): number => {
return +new Date(a.meta.date) - +new Date(b.meta.date);
};
/**
* Retrieve all the comments on a post.
*
* @param {number} id - A post id.
* @returns {Promise<SingleComment[]>} The comments list.
*/
export const getAllComments = async ({
contentId,
}: {
contentId: number;
}): Promise<SingleComment[]> => {
const pages = await fetchAllRawCommentsPages({ contentId });
const comments = pages
.map((page) => getCommentsFromRawData(page.comments))
.flat()
.sort(compareCommentsDate);
return buildCommentsTree(comments);
};
/**
* Send a comment using GraphQL API.
*
* @param {SendCommentVars} data - The comment data.
* @returns {Promise<SentComment>} The mutation response.
*/
export const sendComment = async (
data: SendCommentInput
): Promise<SentComment> => {
const response = await fetchAPI<SentComment, typeof sendCommentMutation>({
query: sendCommentMutation,
variables: { ...data },
});
return response.createComment;
};
|