-
Notifications
You must be signed in to change notification settings - Fork 18
implement a async function to get all the products and set categorys … #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import React from "react"; | ||
| import { Product } from "../../types"; | ||
| import { MODAL_CONSTANTS } from "../../constants/Modal"; | ||
|
|
||
| interface ModalProps { | ||
| product: Product; | ||
| closeModal: () => void; | ||
| } | ||
|
|
||
| const Modal = ({ product, closeModal }: ModalProps) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Destruct product |
||
| const { image, title, category, price, rating } = product; | ||
| return ( | ||
| <div className="modal-container" onClick={closeModal}> | ||
|
|
||
| <div | ||
| className="modal" | ||
| onClick={(e) => e.stopPropagation()} | ||
| style={{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No inline style, your styles should come from css |
||
| backgroundImage: `url(${image})`, | ||
| }} | ||
| > | ||
| <div className="modal-header"> | ||
| <p className="close" onClick={closeModal}> | ||
| {MODAL_CONSTANTS.X} | ||
| </p> | ||
| </div> | ||
| <div className="modal-content"> | ||
| <p> | ||
| {MODAL_CONSTANTS.TITLE} {title} | ||
| </p> | ||
| <p> | ||
| {MODAL_CONSTANTS.CATEGORY} {category} | ||
| </p> | ||
| <p> | ||
| {MODAL_CONSTANTS.PRICE} {price} | ||
| </p> | ||
| <p> | ||
| {MODAL_CONSTANTS.RATING_COUNT} {rating.count} | ||
| </p> | ||
| <p> | ||
| {MODAL_CONSTANTS.RATING_RATE} {rating.rate} | ||
| </p> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Modal; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export const HOME_CONSTANTS = { | ||
| URL_API: "https://fakestoreapi.com/products", | ||
| FAILED: "failed", | ||
| ALL: "All", | ||
| ERROR: "Error:", | ||
| }as const; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export const MODAL_CONSTANTS = { | ||
| X: "x", | ||
| TITLE: "Title:", | ||
| CATEGORY: "Category:", | ||
| PRICE: "Price:", | ||
| RATING_COUNT: "Rating Count:", | ||
| RATING_RATE: "Rating Rate:", | ||
| } as const; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,56 @@ | ||
| import React, { useState, useEffect } from 'react'; | ||
| import axios from 'axios'; | ||
| import '../assets/css/common.css'; | ||
| import '../assets/css/main.css'; | ||
| import Navbar from '../components/ui-elements/Navbar'; | ||
| import DropDownFilter from '../components/filters/DropDownFilter'; | ||
| import Loading from '../components/ui-elements/Loading'; | ||
| import Card from '../components/product-components/Card'; | ||
| import { Product } from '../types'; | ||
| import React, { useState, useEffect } from "react"; | ||
| import axios from "axios"; | ||
| import "../assets/css/common.css"; | ||
| import "../assets/css/main.css"; | ||
| import Navbar from "../components/ui-elements/Navbar"; | ||
| import DropDownFilter from "../components/filters/DropDownFilter"; | ||
| import Loading from "../components/ui-elements/Loading"; | ||
| import Card from "../components/product-components/Card"; | ||
| import { Product } from "../types"; | ||
| import Modal from "../components/product-components/Modal"; | ||
| import { HOME_CONSTANTS } from "../constants/Home"; | ||
|
|
||
| const Home: React.FC = () => { | ||
| const [allProducts, setAllProducts] = useState<Product[]>([]); | ||
| const [products, setProducts] = useState<Product[]>([]); | ||
| const [isDropdownOpen, setIsDropdownOpen] = useState<boolean>(false); | ||
| const [selectedCategory, setSelectedCategory] = useState<string>('All'); | ||
| const [selectedCategory, setSelectedCategory] = useState<string>("All"); | ||
| const [loading, setLoading] = useState<boolean>(true); | ||
| const [error, setError] = useState<string | null>(null); | ||
|
|
||
| //TODO - useEffect + axios | ||
|
|
||
| const [isModalOpen, setIsModalOpen] = useState<boolean>(false); | ||
| const [modalProduct, setModalProduct] = useState<Product | null>(null); | ||
| useEffect(() => { | ||
| const getData = async () => { | ||
| try { | ||
| const res = await axios.get(HOME_CONSTANTS.URL_API); | ||
| setProducts(res.data); | ||
| setAllProducts(res.data); | ||
| } catch (error) { | ||
| setError(HOME_CONSTANTS.FAILED); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
| getData(); | ||
| }, []); | ||
| const showFilterByCategory = () => { | ||
| setIsDropdownOpen(!isDropdownOpen); | ||
| }; | ||
|
|
||
| // TODO - filter products by category - BONUS | ||
| const openModal = (product: Product): void => { | ||
| setIsModalOpen(true); | ||
| setModalProduct(product); | ||
| }; | ||
| const closeModal = (): void => { | ||
| setIsModalOpen(false); | ||
| setModalProduct(null); | ||
| }; | ||
| const filterByCategory = (category: string) => { | ||
| setSelectedCategory(category); | ||
| //TODO - filter products by category | ||
| if (category === HOME_CONSTANTS.ALL) { | ||
| setProducts(allProducts); | ||
| return; | ||
| } | ||
| setProducts(allProducts.filter((p) => p.category === category)); | ||
| }; | ||
|
|
||
| if (loading) { | ||
|
|
@@ -34,17 +59,21 @@ const Home: React.FC = () => { | |
|
|
||
| if (error) { | ||
| return ( | ||
| <div className='error-screen'> | ||
| <p>Error: {error}</p> | ||
| <div className="error-screen"> | ||
| <p>{HOME_CONSTANTS.ERROR} {error}</p> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| {isModalOpen && modalProduct && ( | ||
| <Modal product={modalProduct} closeModal={closeModal} /> | ||
| )} | ||
|
|
||
| <Navbar /> | ||
| <section className='filters'> | ||
| <div className='container'> | ||
| <section className="filters"> | ||
| <div className="container"> | ||
| <DropDownFilter | ||
| onClick={showFilterByCategory} | ||
| isOpen={isDropdownOpen} | ||
|
|
@@ -53,11 +82,19 @@ const Home: React.FC = () => { | |
| /> | ||
| </div> | ||
| </section> | ||
| <main className='main'> | ||
| <div className='container'> | ||
| <main className="main"> | ||
| <div className="container"> | ||
| <section> | ||
| <div className='products-grid' id='products-list'> | ||
| {/* TODO - map products */} | ||
| <div className="products-grid" id="products-list"> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not critical but you could extract this entire block of jsx into separate component |
||
| {products.map((product) => { | ||
| return ( | ||
| <Card | ||
| key={product.id} | ||
| product={product} | ||
| openModal={openModal} | ||
| /> | ||
| ); | ||
| })} | ||
| </div> | ||
| </section> | ||
| </div> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is your only z-index you can have 1m you dont need to have 10, its large starting point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.filters .dropdown-wrapper.open .dropdown-body {
opacity: 1;
visibility: visible;
transform: translateY(0);
z-index: 9;
}
this is why i used z-index 10