Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
49 changes: 49 additions & 0 deletions fetec-gda/src/components/FilterPopOver/FilterPopOver.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
IonCheckbox,
IonContent,
IonItem,
IonLabel,
IonList,
} from "@ionic/react";
import { Dispatch, SetStateAction } from "react";

interface ContainerProps {
categories: { val: string; isChecked: boolean }[];
setCheckBoxList: Dispatch<
SetStateAction<{ val: string; isChecked: boolean }[]>
>;
}

const FilterPopOver: React.FC<ContainerProps> = ({
categories,
setCheckBoxList,
}) => {
const handleCheckboxChange = (cat: any) => {
setCheckBoxList(
categories.map((c) =>
c.val === cat ? { val: c.val, isChecked: !c.isChecked } : c
)
);
};
return (
<IonContent>
<IonList>
{categories.map((category) => (
<IonItem key={category.val}>
<IonLabel>{category.val}</IonLabel>
<IonCheckbox
color="primary"
checked={category.isChecked}
slot="start"
onIonChange={() => {
handleCheckboxChange(category.val);
}}
></IonCheckbox>
</IonItem>
))}
</IonList>
</IonContent>
);
};

export default FilterPopOver;
6 changes: 6 additions & 0 deletions fetec-gda/src/data/brands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const brands = [
role: "Vicepresidente",
},
],
category: "Technology",
favorite: false,
},
{
Expand Down Expand Up @@ -51,6 +52,7 @@ const brands = [
role: "Vicepresidente",
},
],
category: "Food & Drinks",
favorite: false,
},
{
Expand Down Expand Up @@ -78,6 +80,7 @@ const brands = [
role: "Vicepresidente",
},
],
category: "Technology",
favorite: false,
},
{
Expand Down Expand Up @@ -105,6 +108,7 @@ const brands = [
role: "Vicepresidente",
},
],
category: "Fashion",
favorite: false,
},
{
Expand Down Expand Up @@ -132,6 +136,7 @@ const brands = [
role: "Vicepresidente",
},
],
category: "Art",
favorite: false,
},
{
Expand Down Expand Up @@ -159,6 +164,7 @@ const brands = [
role: "Vicepresidente",
},
],
category: "Green world",
favorite: false,
},
];
Expand Down
9 changes: 9 additions & 0 deletions fetec-gda/src/data/categories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const brands = [
{ val: "Technology", isChecked: true },
{ val: "Food & Drinks", isChecked: true },
{ val: "Fashion", isChecked: true },
{ val: "Art", isChecked: true },
{ val: "Green world", isChecked: true },
];

export default brands;
11 changes: 11 additions & 0 deletions fetec-gda/src/hooks/useIsMount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useRef, useEffect } from "react";

// Custom hook to detect if we are on the first render (return true)
// or not (return false)
export const useIsMount = () => {
const isMountRef = useRef(true);
useEffect(() => {
isMountRef.current = false;
}, []);
return isMountRef.current;
};
64 changes: 56 additions & 8 deletions fetec-gda/src/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,102 @@
import { useEffect, useState } from "react";
import {
IonButton,
IonCol,
IonContent,
IonGrid,
IonHeader,
IonIcon,
IonNote,
IonPage,
IonPopover,
IonRow,
IonSearchbar,
IonTitle,
IonToolbar,
} from "@ionic/react";
import { searchCircleOutline } from "ionicons/icons";
import { filterOutline, searchCircleOutline } from "ionicons/icons";

import ElementCard from "../components/ElementCard";
import FilterPopOver from "../components/FilterPopOver/FilterPopOver";
import { getBrandsByName } from "../selectors/getBrandsByName";
import { getBrandsByCategories } from "../selectors/getBrandsByCategories";
import { useIsMount } from "../hooks/useIsMount";

import brands from "../data/brands";
import categories from "../data/categories";

import "./Home.css";
import "../theme/styles.css";

const Home: React.FC = () => {
const [searchValue, setSearchValue] = useState("");
const [brandsArr, setBrandsArr] = useState(brands);
const [brandsArr, setBrandsArr] = useState([...brands]);

const [popoverState, setShowPopover] = useState({
showPopover: false,
event: undefined,
});

const [checkboxList, setCheckboxList] = useState([...categories]);

const isMount = useIsMount();

useEffect(() => {
if (searchValue === "") {
setBrandsArr(brands);
} else {
setBrandsArr(getBrandsByName(searchValue));
if (!isMount) {
if (searchValue === "") {
setBrandsArr([...brands]);
} else {
setBrandsArr(getBrandsByName(searchValue));
}
}
}, [searchValue]);

useEffect(() => {
if (!isMount) {
setBrandsArr(getBrandsByCategories(checkboxList));
}
}, [checkboxList]);

const handleFilterClicked = (e: any) => {
setShowPopover({ showPopover: !popoverState.showPopover, event: e });
};

return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle className="fontSize-25">Home</IonTitle>
</IonToolbar>
<IonToolbar>
<IonToolbar className="p-5">
<IonSearchbar
slot="start"
className="rounded-20"
type="text"
color="light"
placeholder={"Search"}
onIonChange={(e) => setSearchValue(e.detail.value!)}
/>
<IonButton
slot="end"
color="light"
size="small"
onClick={handleFilterClicked}
>
<IonIcon icon={filterOutline} />
</IonButton>
</IonToolbar>
<IonPopover
children={
<FilterPopOver
categories={checkboxList}
setCheckBoxList={setCheckboxList}
/>
}
event={popoverState.event}
isOpen={popoverState.showPopover}
onDidDismiss={handleFilterClicked}
showBackdrop={false}
></IonPopover>
</IonHeader>

<IonContent fullscreen>
Expand Down Expand Up @@ -76,7 +125,6 @@ const Home: React.FC = () => {
</div>
)}
</IonGrid>
{/* <CardsGrid cards={brands}/> */}
</IonContent>
</IonPage>
);
Expand Down
13 changes: 13 additions & 0 deletions fetec-gda/src/selectors/getBrandsByCategories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import brands from "../data/brands";

// Function to filter the brands by categories selected
export const getBrandsByCategories = (
categories: { val: string; isChecked: boolean }[],
array = brands
) => {
return array.filter((brand) =>
categories.some(
(cat) => cat["val"] === brand.category && cat["isChecked"] === true
)
);
};
4 changes: 4 additions & 0 deletions fetec-gda/src/theme/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
.fontSize-25 {
font-size: 25px;
}

.p-5 {
padding: 5px;
}