Skip to content
Merged
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions client/app/components/checkbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Checkbox = ({
label,
onChange,
checked,
}: {
label: string;
onChange: () => void;
checked: boolean;
}) => {
return (
<div className="col-lg-2 col-xl-2" role="category-group">
<input
type="checkbox"
onChange={() => onChange()}
checked={checked}
id={`check-${label.toLowerCase()}`}
/>
<label htmlFor={`check-${label.toLowerCase()}`}></label> {label}
</div>
);
};

export default Checkbox;
4 changes: 3 additions & 1 deletion client/app/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ const Header: React.FC<HeaderProps> = ({
id="userDropdown"
data-bs-toggle="dropdown"
aria-expanded="false"
>{loginForm?.username}</a>
>
{loginForm?.username}
</a>
<ul className="dropdown-menu" aria-labelledby="userDropdown">
<li>
<a
Expand Down
22 changes: 21 additions & 1 deletion client/app/context/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type LoginState = {
error?: string | null;
};

export const submitAction = async (
export const submitLoginAction = async (
prevState: LoginState,
formData: FormData
): Promise<LoginState> => {
Expand Down Expand Up @@ -55,3 +55,23 @@ export const submitAction = async (
};
}
};

export const submitSearchAction = async ({ categories, query }: { categories: string[], query: string }) => {
const params = new URLSearchParams({ query });
categories.forEach((category) => params.append("category", category));

try {
const response = await tekdbApi.get("/api/search/", {
params,
});

if (response.status === 200) {
return response.data;
}

throw new Error("Search failed");
} catch (error) {
console.error("Search error", error);
throw error;
}
};
19 changes: 12 additions & 7 deletions client/app/context/loginContext.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, {
useEffect,
createContext,
useContext,
use,
useCallback,
} from "react";
import { submitAction } from "./action";
import type { ReactNode } from "react";
import { submitLoginAction } from "./action";
import { getAuthSession } from "../api/tekdbApi";

type UseLoginFormReturn = {
Expand All @@ -24,7 +25,11 @@ type useLogin = {

const LoginContext = createContext<useLogin | null>(null);

export const LoginProvider: React.FC<React.PropsWithChildren<{}>> = ({
interface LoginProviderProps {
children: ReactNode;
}

export const LoginProvider: React.FC<LoginProviderProps> = ({
children,
}) => {
const [showLoginModal, setShowLoginModal] = React.useState(false);
Expand Down Expand Up @@ -66,7 +71,7 @@ export const LoginProvider: React.FC<React.PropsWithChildren<{}>> = ({
const formAction = useCallback(async (formData: FormData) => {
setIsPending(true);
try {
const loginState = await submitAction(
const loginState = await submitLoginAction(
{
success: false,
error: null,
Expand All @@ -77,7 +82,7 @@ export const LoginProvider: React.FC<React.PropsWithChildren<{}>> = ({
if (loginState.success) {
setIsAuthenticated(true);
setError(null);
setShowLoginModal(false);
setShowLoginModal(false);
return;
}

Expand All @@ -104,12 +109,12 @@ export const LoginProvider: React.FC<React.PropsWithChildren<{}>> = ({
};

return (
<LoginContext.Provider value={value}>{children}</LoginContext.Provider>
<LoginContext value={value}>{children}</LoginContext>
);
};

export const useLogin = () => {
const context = useContext(LoginContext);
const context = use(LoginContext);
if (!context) {
throw new Error("useLogin must be used within a LoginProvider");
}
Expand Down
72 changes: 72 additions & 0 deletions client/app/context/searchContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React, { useCallback, use } from "react";
import type { ReactNode } from "react";
import { useNavigate } from "react-router";
import { submitSearchAction } from "./action";

type useSearch = {
categories: string[];
results: any[];
query: string;
handleCategoryTypeChange: (type: string) => void;
handleSearch: (formData: FormData) => Promise<void>;
handleQueryChange: (query: string) => void;
};

const SearchContext = React.createContext<useSearch | null>(null);

interface SearchProviderProps {
children: ReactNode;
}

export const SearchProvider: React.FC<SearchProviderProps> = ({ children }) => {
const navigate = useNavigate();
const [categories, setCategories] = React.useState<string[]>([]);
const [results, setResults] = React.useState<any[]>([]);
const [query, setQuery] = React.useState<string>("");

const handleCategoryTypeChange = useCallback(
(type: string) => {
if (categories.includes(type)) {
setCategories(
categories.filter((category: string) => category !== type),
);
} else {
setCategories([...categories, type]);
}
},
[categories],
);

const handleQueryChange = useCallback((query: string) => {
setQuery(query);
}, []);

const handleSearch = useCallback(async () => {
try {
const response = await submitSearchAction({ categories, query });
const { results } = response;
setResults(results);
Comment on lines +44 to +48
await navigate("/search");
} catch (error) {
console.error("Search failed", error);
}
}, [categories, query, navigate]);

const value = {
categories,
results,
query,
handleCategoryTypeChange,
handleSearch,
handleQueryChange,
};
return <SearchContext value={value}>{children}</SearchContext>;
};

export const useSearch = () => {
const context = use(SearchContext);
if (!context) {
throw new Error("useSearch must be used within a SearchProvider");
}
return context;
};
1 change: 0 additions & 1 deletion client/app/layouts/private-route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useLogin } from "../context/loginContext";
/** Return a 404 if the user is not authenticated */
const PrivateRoute: React.FC = () => {
const { loginForm } = useLogin();
console.log("PrivateRoute", loginForm);
if (loginForm.isCheckingAuth) {
return <></>;
}
Expand Down
7 changes: 6 additions & 1 deletion client/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ScrollRestoration,
useLoaderData,
} from "react-router";
import type { ReactNode } from "react";

import type { Route } from "./+types/root";
import "./App.css";
Expand All @@ -16,8 +17,10 @@ import {
type PageContentAndSiteInfo,
} from "./api/pageContent";
import { LoginProvider } from "./context/loginContext";
import { SearchProvider } from "./context/searchContext";

export function Layout({ children }: { children: React.ReactNode }) {

export function Layout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<head>
Expand Down Expand Up @@ -77,9 +80,11 @@ export default function Root() {
}}
/>
<LoginProvider>
<SearchProvider>
<Outlet
context={{ pageContent, siteInfo } satisfies PageContentAndSiteInfo}
/>
</SearchProvider>
</LoginProvider>
</>
);
Expand Down
76 changes: 26 additions & 50 deletions client/app/routes/explore.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import React, { useState } from "react";
import React from "react";
import { useSearch } from "../context/searchContext";
import Checkbox from "../components/checkbox";
import { CATEGORIES } from "../utils/constants";

import "./explore.css";

const ExploreRoute: React.FC = () => {
const [categories, setCategories] = useState<string[]>([]);
const {
handleCategoryTypeChange,
handleSearch,
handleQueryChange,
categories,
query,
} = useSearch();

const handleCategoryTypeChange = (type: string) => {
if (categories.includes(type)) {
setCategories(categories.filter((category) => category !== type));
} else {
setCategories([...categories, type]);
}
const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
handleSearch(new FormData(e.currentTarget));
};
console.log("Selected categories:", categories);

return (
<div className="container-fluid content-container">
<div className="row content-row">
<div className="col well content-well accent-bg">
<section className="container">
<div className="explore-form-row row min-full-height">
<div className="col form-wrapper">
<form className="explore-form" method="post" action="/api/search/">
<form className="explore-form" onSubmit={onSubmit}>
<h1>Search &amp; Browse</h1>
<p>
You can search by term alone and/or add category filters
Expand Down Expand Up @@ -65,6 +72,7 @@ const ExploreRoute: React.FC = () => {
name="query"
placeholder="Search"
aria-describedby="search-icon-label"
onChange={(e) => handleQueryChange(e.target.value)}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

this is the intended functionality!

/>
<input type="submit" id="query-form-submit" value="GO" />
</div>
Expand All @@ -73,7 +81,7 @@ const ExploreRoute: React.FC = () => {
type="text"
id="search-category-type"
name="category"
value=""
value={categories.join(",")}
hidden
/>
<div
Expand All @@ -84,46 +92,14 @@ const ExploreRoute: React.FC = () => {
<div className="col-12 col-xl-2" role="category-group">
<p>Categories:</p>
</div>
<div className="col-lg-2 col-xl-2" role="category-group">
<input
type="checkbox"
onChange={() => handleCategoryTypeChange("places")}
id="check-places"
/>
<label htmlFor="check-places"></label> Places
</div>
<div className="col-lg-3 col-xl-2" role="category-group">
<input
type="checkbox"
onChange={() => handleCategoryTypeChange("resources")}
id="check-resources"
/>
<label htmlFor="check-resources"></label> Resources
</div>
<div className="col-lg-3 col-xl-2" role="category-group">
<input
type="checkbox"
onChange={() => handleCategoryTypeChange("activities")}
id="check-activities"
{CATEGORIES.map((category) => (
<Checkbox
key={category}
label={category}
checked={categories.includes(category)}
onChange={() => handleCategoryTypeChange(category)}
/>
<label htmlFor="check-activities"></label> Activity
</div>
<div className="col-lg-2 col-xl-2" role="category-group">
<input
type="checkbox"
onChange={() => handleCategoryTypeChange("sources")}
id="check-sources"
/>
<label htmlFor="check-sources"></label> Source
</div>
<div className="col-lg-2 col-xl-2" role="category-group">
<input
type="checkbox"
onChange={() => handleCategoryTypeChange("media")}
id="check-media"
/>
<label htmlFor="check-media"></label> Media
</div>
))}
</div>

<div className="button-wrapper">
Expand Down
Loading
Loading