-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarketMavenHeader.jsx
89 lines (80 loc) · 2.53 KB
/
MarketMavenHeader.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Import necessary dependencies from React and Supabase
import React, { useState, useEffect } from "react";
import { createClient } from "@supabase/supabase-js";
import ProductCategory from "./ProductCategory";
// Supabase configuration
const supabaseUrl = "https://ocimdzpalqvkaseuoyrj.supabase.co";
const supabaseKey = "###";
const supabase = createClient(supabaseUrl, supabaseKey);
// MarketMavenHeader component definition
const MarketMavenHeader = ({
handleSearchChange,
handleSearchSubmit,
handleCategoryClick,
}) => {
// State for managing active tab and product categories
const [activeTab, setActiveTab] = useState("all");
const [productCategories, setproductCategories] = useState([]);
// Handle tab click and category selection
const handleTabClick = (category) => {
setActiveTab(category.id);
handleCategoryClick(category.id);
};
// Fetch product categories on component mount
useEffect(() => {
fetchProductCategories();
}, []);
// Function to fetch product categories from Supabase
const fetchProductCategories = async () => {
try {
const { data, error } = await supabase
.from("Product Category")
.select("*");
if (data) {
setproductCategories(data);
}
if (error) {
throw error;
}
} catch (error) {
console.error("Error fetching data:", error.message);
}
};
// Render the header component
return (
<header className="header">
<div>Welcome to MarketMaven</div>
{/* Search form */}
<form className="search-form" onSubmit={handleSearchSubmit}>
<input
type="text"
className="search-input"
placeholder="Search..."
onChange={handleSearchChange}
/>
<button type="submit" className="browse-button">
Search
</button>
</form>
{/* Category tab bar */}
<div className="tab-bar">
<button
className={activeTab === "all" ? "active" : ""}
onClick={() => handleTabClick("all")}
>
All
</button>
{/* Render ProductCategory components for each category */}
{productCategories.map((category, index) => (
<ProductCategory
key={index}
id={category.id}
CategoryName={category.CategoryName}
handleCategoryClick={handleCategoryClick}
/>
))}
</div>
</header>
);
};
export default MarketMavenHeader;