|
1 | 1 | import React from 'react'; |
2 | 2 | import { fetchMoviesWithComments, fetchMoviesByYear, fetchDirectorStats } from '../lib/api'; |
| 3 | +import { MovieWithComments, YearlyStats, DirectorStats } from '../types/aggregations'; |
3 | 4 | import styles from './aggregations.module.css'; |
4 | 5 |
|
5 | | -// Type definitions for better type safety |
6 | | -interface MovieWithComments { |
7 | | - _id: string; |
8 | | - title: string; |
9 | | - year: number; |
10 | | - genres: string[]; |
11 | | - imdbRating: number; |
12 | | - totalComments: number; |
13 | | - recentComments: Array<{ |
14 | | - userName: string; |
15 | | - userEmail: string; |
16 | | - text: string; |
17 | | - date: string; |
18 | | - }>; |
19 | | -} |
20 | | - |
21 | | -interface YearlyStats { |
22 | | - year: number; |
23 | | - movieCount: number; |
24 | | - averageRating: number; |
25 | | - highestRating: number; |
26 | | - lowestRating: number; |
27 | | - totalVotes: number; |
28 | | -} |
29 | | - |
30 | | -interface DirectorStats { |
31 | | - director: string; |
32 | | - movieCount: number; |
33 | | - averageRating: number; |
34 | | -} |
35 | | - |
36 | 6 | export default async function AggregationsPage() { |
| 7 | + const MOVIES_WITH_COMMENTS_LIMIT = 5; |
| 8 | + const DIRECTOR_STATS_LIMIT = 15; |
37 | 9 |
|
38 | 10 | // Fetch all aggregation data with error handling |
39 | 11 | const [commentsResult, yearResult, directorsResult] = await Promise.allSettled([ |
40 | | - fetchMoviesWithComments(5), |
| 12 | + fetchMoviesWithComments(MOVIES_WITH_COMMENTS_LIMIT), |
41 | 13 | fetchMoviesByYear(), |
42 | | - fetchDirectorStats(15) |
| 14 | + fetchDirectorStats(DIRECTOR_STATS_LIMIT) |
43 | 15 | ]); |
44 | 16 |
|
45 | 17 | // Process results with fallbacks |
46 | 18 | const commentsData = commentsResult.status === 'fulfilled' ? commentsResult.value : { success: false, error: 'Failed to fetch comments data' }; |
47 | 19 | const yearData = yearResult.status === 'fulfilled' ? yearResult.value : { success: false, error: 'Failed to fetch year data' }; |
48 | 20 | const directorsData = directorsResult.status === 'fulfilled' ? directorsResult.value : { success: false, error: 'Failed to fetch directors data' }; |
49 | 21 |
|
50 | | - console.log('Aggregations SSR: Data fetch completed', { |
51 | | - comments: commentsData.success, |
52 | | - year: yearData.success, |
53 | | - directors: directorsData.success |
54 | | - }); |
| 22 | + if (process.env.NODE_ENV === 'development') { |
| 23 | + console.log('Aggregations SSR: Data fetch completed', { |
| 24 | + comments: commentsData.success, |
| 25 | + year: yearData.success, |
| 26 | + directors: directorsData.success |
| 27 | + }); |
| 28 | + } |
55 | 29 |
|
56 | 30 | return ( |
57 | 31 | <div className={styles.container}> |
@@ -85,7 +59,7 @@ export default async function AggregationsPage() { |
85 | 59 | <td> |
86 | 60 | <div className={styles.commentsContainer}> |
87 | 61 | {movie.recentComments?.slice(0, 2).map((comment, index) => ( |
88 | | - <div key={index} className={styles.comment}> |
| 62 | + <div key={`${movie._id}-${comment.date}-${index}`} className={styles.comment}> |
89 | 63 | <div className={styles.commentText}> |
90 | 64 | “{(comment.text || 'No text').slice(0, 80)}{comment.text?.length > 80 ? '...' : ''}” |
91 | 65 | </div> |
|
0 commit comments