Skip to content

Commit

Permalink
🔨 Filtrando categorías con JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Klauditha committed Dec 7, 2023
1 parent d814c12 commit e58c38a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/Components/Navbar/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const Navbar = () => {
<li>
<NavLink
to="/"
onClick={() => context.setSearchByCategory()}
className={({ isActive }) => (isActive ? activeStyle : undefined)}
>
All
Expand All @@ -24,6 +25,7 @@ const Navbar = () => {
<li>
<NavLink
to="/clothes"
onClick={() => context.setSearchByCategory('clothes')}
className={({ isActive }) => (isActive ? activeStyle : undefined)}
>
Clothes
Expand All @@ -32,6 +34,7 @@ const Navbar = () => {
<li>
<NavLink
to="/electronics"
onClick={() => context.setSearchByCategory('electronics')}
className={({ isActive }) => (isActive ? activeStyle : undefined)}
>
Electronics
Expand All @@ -40,6 +43,7 @@ const Navbar = () => {
<li>
<NavLink
to="/furnitures"
onClick={() => context.setSearchByCategory('furnitures')}
className={({ isActive }) => (isActive ? activeStyle : undefined)}
>
Furnitures
Expand All @@ -48,6 +52,7 @@ const Navbar = () => {
<li>
<NavLink
to="/toys"
onClick={() => context.setSearchByCategory('toys')}
className={({ isActive }) => (isActive ? activeStyle : undefined)}
>
Toys
Expand All @@ -56,6 +61,7 @@ const Navbar = () => {
<li>
<NavLink
to="/others"
onClick={() => context.setSearchByCategory('others')}
className={({ isActive }) => (isActive ? activeStyle : undefined)}
>
{" "}
Expand Down
70 changes: 59 additions & 11 deletions src/Context/Context.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ export const ShoppingCartProvider = ({ children }) => {
const [filteredItems, setFilteredItems] = useState(null);

//Get products by title
const [seachByTitle, setSeachByTitle] = useState(null);

const [searchByTitle, setSearchByTitle] = useState(null);

//Get products by category
const [searchByCategory, setSearchByCategory] = useState(null);

useEffect(() => {
fetch('https://api.escuelajs.co/api/v1/products')
.then((res) => res.json())
Expand All @@ -42,15 +45,58 @@ export const ShoppingCartProvider = ({ children }) => {
}, []);

const filteredItemsByTitle = (items, searchByTitle) => {
return items?.filter(item => item.title.toLowerCase().includes(searchByTitle.toLowerCase()));
}
return items?.filter((item) =>
item.title.toLowerCase().includes(searchByTitle.toLowerCase())
);
};

const filteredItemsByCategory = (items, searchByCategory) => {
return items?.filter((item) =>
item.category.name.toLowerCase().includes(searchByCategory.toLowerCase())
);
};

const filterBy = (searchType, items, searchByTitle, searchByCategory) => {
if (searchType === 'BY_TITLE') {
return filteredItemsByTitle(items, searchByTitle);
}
if (searchType === 'BY_CATEGORY') {
return filteredItemsByCategory(items, searchByCategory);
}
if (searchType === 'BY_TITLE_AND_CATEGORY') {
return filteredItemsByCategory(items, searchByCategory).filter((item) =>
item.title.toLowerCase().includes(searchByTitle.toLowerCase())
);
}
if (!searchType) {
return items;
}
};
useEffect(() => {
if(seachByTitle) {
setFilteredItems(filteredItemsByTitle(items, seachByTitle));
if (searchByTitle && searchByCategory) {
setFilteredItems(
filterBy(
'BY_TITLE_AND_CATEGORY',
items,
searchByTitle,
searchByCategory
)
);
}
},[items, seachByTitle]);

if (searchByTitle && !searchByCategory) {
setFilteredItems(
filterBy('BY_TITLE', items, searchByTitle, searchByCategory)
);
}
if (!searchByTitle && searchByCategory) {
setFilteredItems(
filterBy('BY_CATEGORY', items, searchByTitle, searchByCategory)
);
}
if (!searchByTitle && !searchByCategory) {
setFilteredItems(filterBy(null, items, searchByTitle, searchByCategory));
}
}, [items, searchByTitle, searchByCategory]);

return (
<ShoppingCartContext.Provider
Expand All @@ -71,9 +117,11 @@ export const ShoppingCartProvider = ({ children }) => {
setOrder,
items,
setItems,
seachByTitle,
setSeachByTitle,
filteredItems
searchByTitle,
setSearchByTitle,
filteredItems,
searchByCategory,
setSearchByCategory,
}}
>
{children}
Expand Down
20 changes: 20 additions & 0 deletions src/Pages/App/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ const AppRoutes = () => {
path: "/",
element: <Home />,
},
{
path: "/clothes",
element: <Home />,
},
{
path: "/electronics",
element: <Home />,
},
{
path: "/furnitures",
element: <Home />,
},
{
path: "/toys",
element: <Home />,
},
{
path: "/others",
element: <Home />,
},
{
path: "/my-orders",
element: <MyOrders />,
Expand Down
4 changes: 2 additions & 2 deletions src/Pages/Home/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Home = () => {
const context = useContext(ShoppingCartContext);

const renderView = () => {
if (context.seachByTitle?.length > 0) {
if (context.searchByTitle?.length > 0) {
if (context.filteredItems?.length > 0) {
return context.filteredItems?.map((item) => (
<Card key={item.id} data={item} />
Expand All @@ -29,7 +29,7 @@ const Home = () => {
type="text"
placeholder="Search a product"
className="rounded-lg border border-black w-80 p-4 mb-4 focus:outline-none"
onChange={(event) => context.setSeachByTitle(event.target.value)}
onChange={(event) => context.setSearchByTitle(event.target.value)}
/>
<div className="grid gap-4 grid-cols-4 w-full max-w-screen-lg">
{renderView()}
Expand Down

0 comments on commit e58c38a

Please sign in to comment.