Skip to content

Commit

Permalink
feat(266): Create a general component for search
Browse files Browse the repository at this point in the history
  • Loading branch information
techeclecticdesign committed Jul 7, 2024
1 parent 0ced622 commit 02d2f26
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
16 changes: 9 additions & 7 deletions frontend/src/Components/inputs/DropdownControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ export function DropdownControl({
enumType,
}: DropdownControlProps) {
return (
<label className="form-control w-full">
{label && (
<div className="label">
<span className="label-text">{label}</span>
</div>
)}
<label className="form-control">
<select
className="select select-bordered"
onChange={(e) => callback(e.target.value)}
>
{label ? (
<option value="" disabled selected>
{label}
</option>
) : (
""
)}
{Object.entries(enumType).map(([key, value]) => (
<option key={key} value={value}>
{value}
{key}
</option>
))}
</select>
Expand Down
24 changes: 24 additions & 0 deletions frontend/src/Components/inputs/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { MagnifyingGlassIcon } from "@heroicons/react/24/solid";

export default function SearchBar({
searchTerm,
changeCallback,
}: {
searchTerm: string;
changeCallback: (arg: React.ChangeEvent<HTMLInputElement>) => void;
}) {
return (
<label className="form-control">
<div className="relative">
<MagnifyingGlassIcon className="absolute top-1/2 right-2 transform -translate-y-1/2 w-5 h-5 text-secondary" />
<input
type="text"
placeholder="Search..."
className="input input-bordered w-full max-w-xs"
value={searchTerm}
onChange={(e) => changeCallback(e.target.value)}
/>
</div>
</label>
);
}
19 changes: 8 additions & 11 deletions frontend/src/Pages/CourseCatalog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ToggleView, { ViewType } from "@/Components/ToggleView";
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
import { useEffect, useState } from "react";
import CatalogCourseCard from "@/Components/CatalogCourseCard";
import SearchBar from "@/Components/inputs/SearchBar";
import { Program, ServerResponse } from "@/common";
import useSWR from "swr";

Expand All @@ -13,6 +14,7 @@ import useSWR from "swr";
export default function CourseCatalog() {
const { user } = useAuth();
const [activeView, setActiveView] = useState<ViewType>(ViewType.Grid);
const [searchTerm, setSearchTerm] = useState("");

const { data, mutate } = useSWR<ServerResponse<Program>>(
`/api/users/${user.id}/catalogue`,
Expand All @@ -27,6 +29,11 @@ export default function CourseCatalog() {
mutate();
}

function handleSearch(e: React.ChangeEvent<HTMLInputElement>) {
setSearchTerm(e.target.value);
// setPageQuery(1);
}

if (!data) return <div></div>;

return (
Expand All @@ -38,17 +45,7 @@ export default function CourseCatalog() {
<ToggleView activeView={activeView} setActiveView={setActiveView} />
</div>
<div className="flex flex-row items-center mt-4 justify-between">
{/* TO DO: REPLACE WITH MADE SEARCH BAR */}
<input
type="text"
placeholder="Search..."
className="input input-bordered w-full max-w-xs input-sm"
// value={searchTerm}
// onChange={(e) => {
// setSearchTerm(e.target.value);
// setPageQuery(1);
// }}
/>
<SearchBar searchTerm={searchTerm} changeCallback={handleSearch} />
</div>
{/* render on gallery or list view */}
<div
Expand Down
17 changes: 7 additions & 10 deletions frontend/src/Pages/Users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Modal, { ModalType } from "../Components/Modal";
import DeleteForm from "../Components/DeleteForm";
import ResetPasswordForm from "../Components/forms/ResetPasswordForm";
import ShowTempPasswordForm from "../Components/forms/ShowTempPasswordForm";
import SearchBar from "../Components/inputs/SearchBar";
import { useAuth } from "../AuthContext";
import { useDebounceValue } from "usehooks-ts";
import Pagination from "@/Components/Pagination";
Expand Down Expand Up @@ -142,21 +143,17 @@ export default function Users() {
resetModal();
};

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchTerm(e.target.value);
setPageQuery(1);
};

return (
<AuthenticatedLayout title="Users">
<PageNav user={auth.user!} path={["Settings", "Users"]} />
<div className="flex flex-col space-y-6 overflow-x-auto rounded-lg p-4">
<div className="flex justify-between">
<input
type="text"
placeholder="Search..."
className="input input-bordered w-full max-w-xs input-sm"
value={searchTerm}
onChange={(e) => {
setSearchTerm(e.target.value);
setPageQuery(1);
}}
/>
<SearchBar searchTerm={searchTerm} changeCallback={handleChange} />
<div className="tooltip tooltip-left" data-tip="Add User">
<button
className="btn btn-primary btn-sm"
Expand Down

0 comments on commit 02d2f26

Please sign in to comment.