Skip to content

Commit e734188

Browse files
committed
Add deferred props
1 parent dd7c8f6 commit e734188

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

app/controllers/topics_controller.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ def index
99
messages: { include: :user }
1010
])
1111

12-
render inertia: { topics: }
12+
render inertia: {
13+
topics: topics,
14+
trending_topics: calculate_trending_topics(topics)
15+
# trending_topics: InertiaRails.defer { calculate_trending_topics(topics) }
16+
}
1317
end
1418

1519
def show
@@ -23,4 +27,11 @@ def show
2327
])
2428
render inertia: { topic: }
2529
end
30+
31+
protected
32+
33+
def calculate_trending_topics(topics)
34+
sleep 2 # this is a very manual process!
35+
topics.sample(3).pluck("id")
36+
end
2637
end

app/frontend/components/TopicsTable.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Link } from '@inertiajs/react'
22
import { Topic } from '../types'
33
import Avatar from './Avatar'
4+
import TrendingIcon from './TrendingIcon';
45

5-
function TopicRow({ topic }: { topic: Topic }) {
6+
function TopicRow({ topic, isTrending }: { topic: Topic, isTrending?: boolean }) {
67
const {
78
id,
89
title,
@@ -23,6 +24,7 @@ function TopicRow({ topic }: { topic: Topic }) {
2324
<td className="px-6 py-4">
2425
<Link href={`/topics/${id}`} className="text-sm font-medium text-sky-700">
2526
{title}
27+
{isTrending && <TrendingIcon className="mx-1" />}
2628
</Link>
2729
<br />
2830
<span className="text-sm text-gray-500 mt-1">{categoryName}</span>
@@ -44,7 +46,7 @@ function TopicRow({ topic }: { topic: Topic }) {
4446
)
4547
}
4648

47-
export default function TopicsTable({ topics }: { topics: Topic[] }) {
49+
export default function TopicsTable({ topics, trendingTopics = [] }: { topics: Topic[], trendingTopics?: number[] }) {
4850
return (
4951
<table className="min-w-full divide-y divide-gray-200">
5052
<thead className="bg-gray-50">
@@ -65,7 +67,7 @@ export default function TopicsTable({ topics }: { topics: Topic[] }) {
6567
</thead>
6668
<tbody className="bg-white divide-y divide-gray-200">
6769
{topics.map((topic: Topic) => (
68-
<TopicRow key={topic.id} topic={topic} />
70+
<TopicRow key={topic.id} topic={topic} isTrending={trendingTopics.includes(topic.id)} />
6971
))}
7072
{topics.length === 0 && (
7173
<tr>

app/frontend/pages/topics/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import AppLayout from '../../layouts/AppLayout'
33
import { Topic } from '../../types'
44
import TopicsTable from '../../components/TopicsTable'
55

6-
function TopicsIndex({ topics }: { topics: Topic[] }) {
6+
function TopicsIndex({ topics, trending_topics }: { topics: Topic[], trending_topics: number[] }) {
77
return (
88
<AppLayout>
99
<Head title="Topics - Pups & Pourovers" />
1010

1111
<div className="max-w-7xl mx-auto">
1212
<div className="bg-white shadow-sm rounded-lg overflow-hidden">
13-
<TopicsTable topics={topics} />
13+
<TopicsTable topics={topics} trendingTopics={trending_topics} />
1414
</div>
1515
</div>
1616
</AppLayout>

0 commit comments

Comments
 (0)