summaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/format.ts
blob: dd35868261259185b6a699285e9bc5e0a417b02b (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { ParamsIds, ParamsSlug, Slug } from '@ts/types/app';
import {
  Article,
  ArticlePreview,
  RawArticle,
  RawArticlePreview,
} from '@ts/types/articles';
import { Comment, RawComment } from '@ts/types/comments';
import {
  RawTopic,
  RawTopicPreview,
  RawThematic,
  Topic,
  TopicPreview,
  Thematic,
} from '@ts/types/taxonomies';

/**
 * Format a post preview from RawArticlePreview to ArticlePreview type.
 * @param rawPost - A post preview coming from WP GraphQL.
 * @returns A formatted post preview.
 */
export const getFormattedPostPreview = (rawPost: RawArticlePreview) => {
  const {
    acfPosts,
    commentCount,
    contentParts,
    date,
    featuredImage,
    id,
    info,
    modified,
    slug,
    title,
  } = rawPost;

  const dates = {
    publication: date,
    update: modified,
  };

  const topics = acfPosts.postsInTopic ? acfPosts.postsInTopic : [];
  const thematics = acfPosts.postsInThematic ? acfPosts.postsInThematic : [];

  const formattedPost: ArticlePreview = {
    commentCount,
    dates,
    featuredImage: featuredImage ? featuredImage.node : null,
    id,
    info,
    intro: contentParts.beforeMore,
    slug,
    topics,
    thematics,
    title,
  };

  return formattedPost;
};

/**
 * Format an array of posts list from RawArticlePreview to ArticlePreview type.
 * @param rawPosts - A posts list coming from WP GraphQL.
 * @returns A formatted posts list.
 */
export const getFormattedPostsList = (
  rawPosts: RawArticlePreview[]
): ArticlePreview[] => {
  return rawPosts
    .filter((post) => Object.getOwnPropertyNames(post).length > 0)
    .map((post) => {
      return getFormattedPostPreview(post);
    });
};

/**
 * Format a topic from RawTopic to Topic type.
 * @param rawTopic - A topic coming from WP GraphQL.
 * @returns A formatted topic.
 */
export const getFormattedTopic = (rawTopic: RawTopic): Topic => {
  const {
    acfTopics,
    contentParts,
    databaseId,
    date,
    featuredImage,
    id,
    info,
    modified,
    seo,
    title,
  } = rawTopic;

  const dates = {
    publication: date,
    update: modified,
  };

  const posts = getFormattedPostsList(acfTopics.postsInTopic);

  const formattedTopic: Topic = {
    content: contentParts.afterMore,
    databaseId,
    dates,
    featuredImage: featuredImage ? featuredImage.node : null,
    id,
    info,
    intro: contentParts.beforeMore,
    officialWebsite: acfTopics.officialWebsite,
    posts,
    seo,
    title,
  };

  return formattedTopic;
};

/**
 * Format a thematic from RawThematic to Thematic type.
 * @param rawThematic - A thematic coming from wP GraphQL.
 * @returns A formatted thematic.
 */
export const getFormattedThematic = (rawThematic: RawThematic): Thematic => {
  const {
    acfThematics,
    contentParts,
    databaseId,
    date,
    id,
    info,
    modified,
    seo,
    title,
  } = rawThematic;

  const dates = {
    publication: date,
    update: modified,
  };

  const posts = getFormattedPostsList(acfThematics.postsInThematic);

  const formattedThematic: Thematic = {
    content: contentParts.afterMore,
    databaseId,
    dates,
    id,
    info,
    intro: contentParts.beforeMore,
    posts,
    seo,
    title,
  };

  return formattedThematic;
};

/**
 * Format a comments list from RawComment to Comment type.
 * @param rawComments - A comments list coming from WP GraphQL.
 * @returns A formatted comments list.
 */
export const getFormattedComments = (rawComments: RawComment[]): Comment[] => {
  const formattedComments: Comment[] = rawComments.map((comment) => {
    const formattedComment: Comment = {
      ...comment,
      author: comment.author.node,
      replies: [],
    };

    return formattedComment;
  });

  return formattedComments;
};

/**
 * Create a comments tree with replies.
 * @param comments - A flatten comments list.
 * @returns An array of comments with replies.
 */
export const buildCommentsTree = (comments: Comment[]) => {
  type CommentsHashTable = {
    [key: string]: Comment;
  };

  const hashTable: CommentsHashTable = Object.create(null);
  const commentsTree: Comment[] = [];

  comments.forEach(
    (comment) => (hashTable[comment.databaseId] = { ...comment, replies: [] })
  );

  comments.forEach((comment) => {
    if (!comment.parentDatabaseId) {
      commentsTree.push(hashTable[comment.databaseId]);
    } else {
      hashTable[comment.parentDatabaseId].replies.push(
        hashTable[comment.databaseId]
      );
    }
  });

  return commentsTree;
};

export const getFormattedTopicsPreview = (
  topics: RawTopicPreview[]
): TopicPreview[] => {
  const formattedTopics: TopicPreview[] = topics.map((topic) => {
    return {
      ...topic,
      featuredImage: topic.featuredImage ? topic.featuredImage.node : null,
    };
  });

  return formattedTopics;
};

/**
 * Format an article from RawArticle to Article type.
 * @param rawPost - An article coming from WP GraphQL.
 * @returns A formatted article.
 */
export const getFormattedPost = (rawPost: RawArticle): Article => {
  const {
    acfPosts,
    author,
    commentCount,
    contentParts,
    databaseId,
    date,
    featuredImage,
    id,
    info,
    modified,
    seo,
    title,
  } = rawPost;

  const dates = {
    publication: date,
    update: modified,
  };

  const topics = acfPosts.postsInTopic
    ? getFormattedTopicsPreview(acfPosts.postsInTopic)
    : [];

  const formattedPost: Article = {
    author: author.node,
    commentCount,
    content: contentParts.afterMore,
    databaseId,
    dates,
    featuredImage: featuredImage ? featuredImage.node : null,
    id,
    info,
    intro: contentParts.beforeMore,
    seo,
    topics,
    thematics: acfPosts.postsInThematic ? acfPosts.postsInThematic : [],
    title,
  };

  return formattedPost;
};

/**
 * Converts a date to a string by using the specified locale.
 * @param {string} date The date.
 * @param {string} locale A locale.
 * @returns {string} The formatted date to locale date string.
 */
export const getFormattedDate = (date: string, locale: string) => {
  const dateOptions: Intl.DateTimeFormatOptions = {
    day: 'numeric',
    month: 'long',
    year: 'numeric',
  };

  return new Date(date).toLocaleDateString(locale, dateOptions);
};

/**
 * Convert an array of slugs to an array of params with slug.
 * @param {Slug} array - An array of object with slug.
 * @returns {ParamsSlug} An array of params with slug.
 */
export const getFormattedPaths = (array: Slug[]): ParamsSlug[] => {
  return array.map((object) => {
    return { params: { slug: object.slug } };
  });
};

/**
 * Convert a number of pages to an array of params with ids.
 * @param {number} totalPages - The total pages.
 * @returns {ParamsIds} An array of params with ids.
 */
export const getFormattedPageNumbers = (totalPages: number): ParamsIds[] => {
  const paths = [];

  for (let i = 1; i <= totalPages; i++) {
    paths.push({ params: { id: `${i}` } });
  }

  return paths;
};