Skip to content

Commit a84b1ae

Browse files
committed
Basic search
1 parent dd7c8f6 commit a84b1ae

File tree

5 files changed

+58
-11
lines changed

5 files changed

+58
-11
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class SearchController < ApplicationController
2+
def index
3+
query = params[:query].to_s
4+
5+
topics = Topic.search(query)
6+
.as_json(include: [
7+
:user,
8+
:category,
9+
messages: { include: :user }
10+
])
11+
12+
render inertia: { topics:, query: }
13+
end
14+
end

app/frontend/layouts/AppLayout.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ReactNode, useState } from 'react'
2-
import { Link, usePage, router } from '@inertiajs/react'
2+
import { Link, usePage, router, Form } from '@inertiajs/react'
33
import { Menu, MenuButton, MenuItems, MenuItem } from '@headlessui/react'
44
import {
55
Bars3Icon,
@@ -25,24 +25,27 @@ export default function AppLayout({ children }: AppLayoutProps) {
2525

2626
<div className="flex-1 flex flex-col overflow-hidden">
2727
<header className="bg-white border-b border-gray-200 h-16 flex items-center justify-between px-6">
28-
<div className="flex items-center space-x-4">
28+
<div className="flex items-center space-x-4 flex-1">
2929
<button
3030
onClick={() => setSidebarOpen(!sidebarOpen)}
3131
className="p-2 rounded-md hover:bg-gray-100 text-gray-600 hover:text-gray-900"
3232
>
3333
<Bars3Icon className="h-6 w-6" />
3434
</button>
3535
<div className="flex-1 max-w-lg">
36-
<div className="relative">
37-
<div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
38-
<MagnifyingGlassIcon className="h-5 w-5 text-gray-400" />
36+
<Form action="/search" method="get">
37+
<div className="relative">
38+
<div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
39+
<MagnifyingGlassIcon className="h-5 w-5 text-gray-400" />
40+
</div>
41+
<input
42+
type="text"
43+
name="query"
44+
placeholder="Search"
45+
className="block w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md leading-5 bg-white placeholder-gray-500 focus:outline-none focus:placeholder-gray-400 focus:ring-1 focus:ring-sky-500 focus:border-sky-500 sm:text-sm"
46+
/>
3947
</div>
40-
<input
41-
type="text"
42-
placeholder="Search"
43-
className="block w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md leading-5 bg-white placeholder-gray-500 focus:outline-none focus:placeholder-gray-400 focus:ring-1 focus:ring-sky-500 focus:border-sky-500 sm:text-sm"
44-
/>
45-
</div>
48+
</Form>
4649
</div>
4750
</div>
4851

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Head } from '@inertiajs/react'
2+
import AppLayout from '../../layouts/AppLayout'
3+
import { Topic } from '../../types'
4+
import TopicsTable from '../../components/TopicsTable'
5+
6+
function TopicsIndex({ topics, query }: { topics: Topic[], query: string }) {
7+
return (
8+
<AppLayout>
9+
<Head title={`Search results for "${query}" - Pups & Pourovers`} />
10+
11+
<div className="max-w-7xl mx-auto">
12+
<h1 className="text-2xl font-bold mb-4">Search results for "{query}"</h1>
13+
<div className="bg-white shadow-sm rounded-lg overflow-hidden">
14+
<TopicsTable topics={topics} />
15+
</div>
16+
</div>
17+
</AppLayout>
18+
)
19+
}
20+
21+
export default TopicsIndex

app/models/topic.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@ class Topic < ApplicationRecord
44
belongs_to :category, counter_cache: true
55

66
validates :title, presence: true
7+
8+
scope :search, ->(query) {
9+
includes(:user, :category, messages: :user)
10+
.left_joins(:messages)
11+
.where("lower(topics.title) LIKE :query OR lower(messages.body) LIKE :query", query: "%#{query.downcase}%")
12+
.order(created_at: :desc)
13+
.distinct
14+
}
715
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
resources :categories, only: [ :show ]
2323

2424
resources :my_topics, only: [ :index ]
25+
get "search", to: "search#index"
2526
end

0 commit comments

Comments
 (0)