Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:03,000 --> 00:00:05,160
Our Reviews page automatically
2
00:00:05,160 --> 00:00:06,960
displays all the reviews,
3
00:00:07,032 --> 00:00:08,767
loading the data from the
4
00:00:08,767 --> 00:00:10,501
available Markdown files.
5
00:00:10,571 --> 00:00:13,086
But our Home page always displays
6
00:00:13,086 --> 00:00:14,382
the same content,
7
00:00:14,458 --> 00:00:16,857
because, in that page component,
8
00:00:17,378 --> 00:00:20,705
we still have some hard-coded review data.
9
00:00:20,705 --> 00:00:23,446
So, here's a little exercise for you:
10
00:00:23,446 --> 00:00:25,868
change this component to load the
11
00:00:25,868 --> 00:00:28,070
featured review automatically.
12
00:00:28,143 --> 00:00:30,804
You can write a new function for that,
13
00:00:30,804 --> 00:00:32,555
in the "reviews.js" file,
14
00:00:32,625 --> 00:00:35,805
called "getFeaturedReview" for example.
15
00:00:35,805 --> 00:00:38,243
This should return a single object,
16
00:00:38,243 --> 00:00:40,802
with all the properties, like "title",
17
00:00:40,802 --> 00:00:42,014
"body", and so on.
18
00:00:42,082 --> 00:00:45,830
As the "featured" review we could pick the latest one,
19
00:00:45,830 --> 00:00:47,080
based on the date.
20
00:00:47,150 --> 00:00:49,855
So, the easiest way to do this is
21
00:00:49,855 --> 00:00:52,984
modify the existing getReviews() function
22
00:00:52,984 --> 00:00:57,142
to sort all the reviews by most recent first.
23
00:00:57,142 --> 00:00:59,113
You can use the "sort" method
24
00:00:59,113 --> 00:01:00,744
on the array to do that,
25
00:01:00,812 --> 00:01:03,942
comparing the "date" property of each review.
26
00:01:03,942 --> 00:01:06,480
At that point, getFeaturedReview
27
00:01:06,480 --> 00:01:08,542
can simply call getReviews
28
00:01:08,622 --> 00:01:11,241
and take the first element in the array.
29
00:01:11,241 --> 00:01:14,758
This way the HomePage should automatically display
30
00:01:14,758 --> 00:01:17,164
the latest review we published
31
00:01:17,164 --> 00:01:18,929
as the "featured" one.
32
00:01:19,009 --> 00:01:21,256
If you want to do this exercise,
33
00:01:21,256 --> 00:01:23,372
stop this video, and start it
34
00:01:23,372 --> 00:01:25,342
again once you're finished.
35
00:01:25,414 --> 00:01:29,350
I'll show you my implementation
36
00:01:29,350 --> 00:01:31,763
in about 3 seconds.
37
00:01:31,890 --> 00:01:34,108
Ok, here's what I've done.
38
00:01:34,108 --> 00:01:36,752
Let's start from "reviews.js".
39
00:01:36,752 --> 00:01:39,075
In the "getReviews" function,
40
00:01:39,075 --> 00:01:41,834
I've added a line to sort the "reviews"
41
00:01:41,834 --> 00:01:43,390
before returning them.
42
00:01:43,460 --> 00:01:46,731
This is using the "sort" method on the array,
43
00:01:46,731 --> 00:01:49,749
which takes a "compare" function as argument,
44
00:01:49,749 --> 00:01:52,968
that specifies how to sort the elements.
45
00:01:52,968 --> 00:01:55,775
In this case, I'm comparing the "date"
46
00:01:55,775 --> 00:01:58,360
property of the two review objects.
47
00:01:58,434 --> 00:02:01,643
This way the Reviews page should also show
48
00:02:01,643 --> 00:02:03,946
the most recent review first,
49
00:02:03,946 --> 00:02:06,200
and the oldest one at the bottom.
50
00:02:06,200 --> 00:02:09,371
Before this change, the order depended on
51
00:02:09,371 --> 00:02:11,883
whatever the file system returned
52
00:02:11,883 --> 00:02:13,862
to the "readdir" function.
53
00:02:13,938 --> 00:02:15,320
Just to remind you,
54
00:02:15,320 --> 00:02:18,512
the date for Hellblade is May 6th,
55
00:02:18,512 --> 00:02:21,198
while, if we look at the Hollow Knight page,
56
00:02:21,198 --> 00:02:22,911
this one is May 5th,
57
00:02:22,911 --> 00:02:24,897
and, for Stardew Valley,
58
00:02:24,951 --> 00:02:26,403
it's May 4th.
59
00:02:26,403 --> 00:02:28,652
So you can see that the dates are
60
00:02:28,652 --> 00:02:30,765
in reverse chronological order.
61
00:02:30,834 --> 00:02:34,649
Also, since they're strings in the ISO format,
62
00:02:34,649 --> 00:02:37,552
we can simply compare them alphabetically,
63
00:02:37,552 --> 00:02:39,003
with "localeCompare".
64
00:02:39,072 --> 00:02:41,337
If we were to compare review
65
00:02:41,337 --> 00:02:43,441
"a" to review "b" by date,
66
00:02:43,521 --> 00:02:46,826
then we'll see the oldest review at the top,
67
00:02:46,826 --> 00:02:48,934
and the latest one at the bottom.
68
00:02:48,934 --> 00:02:51,273
But we want the reverse order,
69
00:02:51,273 --> 00:02:54,301
so we can compare "b" to "a" instead,
70
00:02:54,301 --> 00:02:56,294
and they will be sorted with
71
00:02:56,294 --> 00:02:57,859
the most recent first.
72
00:02:57,930 --> 00:03:01,269
Anyway, let's go back to our Home page now.
73
00:03:01,269 --> 00:03:03,456
Since getReviews returns the
74
00:03:03,456 --> 00:03:05,331
data in the right order,
75
00:03:05,409 --> 00:03:07,363
to get the latest review
76
00:03:07,363 --> 00:03:10,453
we can simply take the first element in the array.
77
00:03:10,453 --> 00:03:12,880
And that will be the featured review
78
00:03:12,880 --> 00:03:15,182
that we show in the HomePage.
79
00:03:15,182 --> 00:03:16,922
"review" will be an object,
80
00:03:16,922 --> 00:03:18,705
with all the properties that
81
00:03:18,705 --> 00:03:20,170
we then need to display
82
00:03:20,234 --> 00:03:23,232
in the right places in the page elements.
83
00:03:23,232 --> 00:03:26,248
And this way the HomePage will always display
84
00:03:26,248 --> 00:03:28,393
the latest review automatically.
5974
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.