Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
Database_project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Phrot Vedal
Database_project
Commits
4d60ee22
Commit
4d60ee22
authored
1 year ago
by
Tiago Brito
Browse files
Options
Downloads
Patches
Plain Diff
Implemented Header.js
parent
863a1e70
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/src/components/Header.js
+57
-10
57 additions, 10 deletions
frontend/src/components/Header.js
with
57 additions
and
10 deletions
frontend/src/components/Header.js
+
57
−
10
View file @
4d60ee22
import
React
from
'
react
'
;
import
{
Link
}
from
'
react-router-dom
'
;
import
React
,
{
useEffect
,
useState
}
from
'
react
'
;
import
{
Link
,
useNavigate
}
from
'
react-router-dom
'
;
import
{
useCart
}
from
'
../contexts/CartContext
'
;
// Import useCart
import
'
../css/Header.css
'
;
function
Header
()
{
const
{
cart
}
=
useCart
();
// Access the cart context
const
[
searchTerm
,
setSearchTerm
]
=
useState
(
''
);
const
[
brands
,
setBrands
]
=
useState
([]);
const
[
selectedBrand
,
setSelectedBrand
]
=
useState
(
'
All Brands
'
);
const
navigate
=
useNavigate
();
useEffect
(()
=>
{
fetch
(
'
http://localhost:5000/brand
'
)
.
then
(
response
=>
response
.
json
())
.
then
(
data
=>
setBrands
(
data
))
.
catch
(
console
.
error
);
},
[]);
const
handleSearchChange
=
(
e
)
=>
{
setSearchTerm
(
e
.
target
.
value
);
};
const
handleSearchSubmit
=
(
e
)
=>
{
e
.
preventDefault
();
navigate
(
`/search?query=
${
searchTerm
}
&brand=
${
selectedBrand
}
`
);
};
const
handleBrandChange
=
(
e
)
=>
{
setSelectedBrand
(
e
.
target
.
value
);
};
// Calculate total items in the cart
const
totalItems
=
cart
.
items
.
reduce
((
total
,
item
)
=>
total
+
item
.
quantity
,
0
);
return
(
<
header
>
<
nav
>
<
ul
>
<
li
><
Link
to
=
"
/
"
>
Home
<
/Link></
li
>
<
li
><
Link
to
=
"
/products
"
>
Products
<
/Link></
li
>
<
li
><
Link
to
=
"
/orders
"
>
Orders
<
/Link></
li
>
<
/ul
>
<
/nav
>
<
div
className
=
"
logo
"
>
<
Link
to
=
"
/
"
>
ElectroMart
<
/Link
>
<
/div
>
<
form
className
=
"
search-bar
"
onSubmit
=
{
handleSearchSubmit
}
>
<
select
value
=
{
selectedBrand
}
onChange
=
{
handleBrandChange
}
className
=
"
brand-select
"
>
<
option
value
=
"
All Brands
"
>
All
Brands
<
/option
>
{
brands
.
map
(
brand
=>
(
<
option
key
=
{
brand
.
BrandID
}
value
=
{
brand
.
Name
}
>
{
brand
.
Name
}
<
/option
>
))}
<
/select
>
<
input
type
=
"
text
"
placeholder
=
"
Search ElectroMart
"
value
=
{
searchTerm
}
onChange
=
{
handleSearchChange
}
/
>
<
button
type
=
"
submit
"
><
i
className
=
"
fa-solid fa-magnifying-glass
"
><
/i></
button
>
<
/form
>
<
div
className
=
"
user-controls
"
>
<
Link
to
=
""
className
=
"
signin-button
"
>
<
i
className
=
"
fa-solid fa-user
"
><
/i
>
<
span
>
Sign
In
<
/span
>
<
/Link
>
<
Link
to
=
"
/cart
"
className
=
"
cart-button
"
>
<
i
className
=
"
fa-solid fa-cart-shopping
"
><
/i
>
<
span
>
Cart
{
totalItems
>
0
?
"
:
"
+
totalItems
:
''
}
<
/span
>
<
/Link
>
<
/div
>
<
/header
>
);
}
export
default
Header
;
\ No newline at end of file
export
default
Header
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment