Skip to content

Commit

Permalink
feat(search-form): implement loading
Browse files Browse the repository at this point in the history
  • Loading branch information
alanrochagomes committed Nov 7, 2024
1 parent 4d3c3ca commit 9d04978
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/components/search-form.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { SearchResult } from "@/model/SearchResult";
import { FormEvent } from "react";
import { FormEvent, useState } from "react";

interface SearchFormProps {
onSearchResultsReady: (searchResults: SearchResult[]) => void;
}

export default function SearchForm(props: SearchFormProps) {
const [isLoading, setIsLoading] = useState<boolean>(false);

async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();

setIsLoading(true);

const formData = new FormData(event.currentTarget);
const response = await fetch("/api/submit", {
method: "POST",
Expand All @@ -17,6 +21,8 @@ export default function SearchForm(props: SearchFormProps) {

const data: SearchResult[] = await response.json();
props.onSearchResultsReady(data);

setIsLoading(false);
}

return (
Expand All @@ -26,12 +32,14 @@ export default function SearchForm(props: SearchFormProps) {
name="search_input"
placeholder="Search for learning resources, tutorials..."
className="border-2 border-gray-300 px-2 py-1 w-full rounded-lg"
disabled={isLoading}
/>

<input
type="submit"
className="bg-indigo-500 text-white px-4 rounded-lg hover:bg-indigo-400 cursor-pointer"
value="Search"
value={isLoading ? "Loading..." : "Search"}
disabled={isLoading}
/>
</form>
);
Expand Down

0 comments on commit 9d04978

Please sign in to comment.