diff --git a/components/SearchBar.tsx b/components/SearchBar.tsx index cd10f2f..031ee21 100644 --- a/components/SearchBar.tsx +++ b/components/SearchBar.tsx @@ -1,21 +1,69 @@ "use client"; -const SearchBar = () => { - const handleSubmit = () => {}; +import { FormEvent, useState } from "react"; +import { scrapeAndStoreProduct } from "@/lib/actions"; + +const isValidAmazonProductURL = (url: string) => { + try { + const parsedURL = new URL(url); + const hostname = parsedURL.hostname; + + if ( + hostname.includes("amazon.com") || + hostname.includes("amazon.") || + hostname.endsWith("amazon") + ) { + return true; + } + } catch (error) { + return false; + } + + return false; +}; + +const Searchbar = () => { + const [searchPrompt, setSearchPrompt] = useState(""); + const [isLoading, setIsLoading] = useState(false); + + const handleSubmit = async (event: FormEvent) => { + event.preventDefault(); + + const isValidLink = isValidAmazonProductURL(searchPrompt); + + if (!isValidLink) return alert("Please provide a valid Amazon link"); + + try { + setIsLoading(true); + + // Scrape the product page + const product = await scrapeAndStoreProduct(searchPrompt); + } catch (error) { + console.log(error); + } finally { + setIsLoading(false); + } + }; return (
setSearchPrompt(e.target.value)} placeholder="Enter product link" className="searchbar-input" /> -
); }; -export default SearchBar; +export default Searchbar;