Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : add search bar #130

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 61 additions & 26 deletions src/routes/events/index.tsx
Original file line number Diff line number Diff line change
@@ -1,51 +1,82 @@
import { createFileRoute } from '@tanstack/react-router';
import { Clock } from "flowbite-react-icons/solid";
import { Clock, Search } from "flowbite-react-icons/outline";
import { useState } from 'react';
import { BellIcon, Header, PinIcon, PlusIcon } from '../../components';
import { AuthGuard } from '../../utils/auth';
import { supabase } from '../../utils/supabase';

interface Event {
created_at: string;
description: string | null;
end_time: string | null;
fee: number | null;
id: number;
name: string | null;
start_time: string | null;
type: number | null;
user_id: string;
location: string | null;
created_at: string;
description: string | null;
end_time: string | null;
fee: number | null;
id: number;
name: string | null;
start_time: string | null;
type: number | null;
user_id: string;
location: string | null;
}

export const Route = createFileRoute('/events/')({
beforeLoad: AuthGuard,
loader: async () => {
const { data, error } = await supabase
.from('events')
.select('*')
const { data, error } = await supabase.from('events').select('*');

if (error !== null) {
throw error
throw error;
}
return { events: data }
return { events: data };
},
component: EventIndex
})
component: EventIndex,
});

function EventIndex() {
const { events } = Route.useLoaderData()
const { events } = Route.useLoaderData();
const navigate = Route.useNavigate();

// console.log(events[0].start_time)
const [searchTerm, setSearchTerm] = useState('');
const [isFocused, setIsFocused] = useState(false);

const filteredEvents = events.filter(
(event) =>
event.name &&
event.name.toLowerCase().includes(searchTerm.toLowerCase())
);
Comment on lines +41 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve event filtering logic

The current filtering logic has potential issues:

  1. Doesn't handle null event names properly
  2. Could use optional chaining as suggested by static analysis
- const filteredEvents = events.filter(
-   (event) =>
-     event.name &&
-     event.name.toLowerCase().includes(searchTerm.toLowerCase())
- );
+ const filteredEvents = events.filter((event) =>
+   event.name?.toLowerCase().includes(searchTerm.toLowerCase()) ?? false
+ );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const filteredEvents = events.filter(
(event) =>
event.name &&
event.name.toLowerCase().includes(searchTerm.toLowerCase())
);
const filteredEvents = events.filter((event) =>
event.name?.toLowerCase().includes(searchTerm.toLowerCase()) ?? false
);
🧰 Tools
🪛 Biome (1.9.4)

[error] 43-44: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


return (
<>
<div className="container mx-auto">
<Header />
<div className="search-bar p-4 relative">
<input
type="text"
placeholder={isFocused ? '請輸入關鍵字' : ''}
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
className="input input-bordered w-full pl-12 pr-12"
/>
{!searchTerm && !isFocused && (
<div className="absolute left-4 top-1/2 transform -translate-y-1/2 flex items-center pointer-events-none">
<Search className="h-5 w-10 text-gray-400" />
<span className="ml-2 text-gray-400">搜尋</span>
</div>
)}
{searchTerm && (
<button
className="absolute right-6 top-1/2 transform -translate-y-1/2 text-gray-500 hover:text-gray-700"
onClick={() => setSearchTerm('')}
>清除</button>
)}
</div>

<div className="flex-1 bg-gray-800 p-4">
<h1 className="ml-4 text-xl text-white">最新揪人</h1>
<div className="overflow-x-auto mt-2">
<div className="flex space-x-4">
{events.map((event) => (
{filteredEvents.map((event) => (
<EventCard key={event.id} event={event} />
))}
</div>
Expand All @@ -54,7 +85,7 @@ function EventIndex() {
<h1 className="ml-4 text-xl text-white mt-8">最新活動</h1>
<div className="overflow-x-auto mt-2">
<div className="flex space-x-4">
{events.map((event) => (
{filteredEvents.map((event) => (
<EventCard key={event.id} event={event} />
))}
</div>
Expand All @@ -65,7 +96,9 @@ function EventIndex() {
className="btn btn-circle fixed right-4 bottom-4"
onClick={() => {
if (document) {
(document.getElementById('my_modal_4') as HTMLFormElement).showModal();
(
document.getElementById('my_modal_4') as HTMLFormElement
).showModal();
}
}}
>
Expand Down Expand Up @@ -94,11 +127,13 @@ function EventIndex() {
</button>
</div>
</>
)
);
}

function EventCard({ event }: { event: Event }) {
const startTime = event.start_time ? new Date(event.start_time) : new Date();
const startTime = event.start_time
? new Date(event.start_time)
: new Date();
return (
<div className="flex-shrink-0 w-40 bg-gray-700 rounded-lg overflow-hidden text-white">
<div className="h-32 bg-gray-500" />
Expand All @@ -120,4 +155,4 @@ function EventCard({ event }: { event: Event }) {
</div>
</div>
);
}
}
Loading