-
Notifications
You must be signed in to change notification settings - Fork 18
liron's pr #6
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?
liron's pr #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,25 @@ | ||
| import React from 'react'; | ||
| import { Product } from '../../types'; | ||
| import { categories } from '../filters/DropDownFilter'; | ||
|
|
||
| interface CardProps { | ||
| product: Product | ||
| } | ||
|
|
||
| const Card: React.FC<CardProps> = () => { | ||
| const Card: React.FC<CardProps> = (props: CardProps) => { | ||
|
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. Destructure Props: Destructure props to improve readability:
|
||
| const maxRatings = 5 | ||
|
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. Magic Numbers (maxRatings): Use constants or make the logic more descriptive. |
||
|
|
||
| return ( | ||
| <div className='product scale-effect'> | ||
| <div className='product-image'> | ||
| <img /> | ||
| <img src={props.product.image}/> | ||
|
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. Error Handling for Loading Image: props.product.image is used directly. If the image URL fails, it might display a broken image. Add an onError:
|
||
| </div> | ||
| <div className='product-info'> | ||
| <h2 className='product-title'></h2> | ||
| <h2 className='product-title'>{props.product.title}</h2> | ||
| <div className='product-brief'> | ||
| <p><strong>Price: </strong></p> | ||
| <p><strong>Rating: </strong></p> | ||
| <p><strong>Category: </strong></p> | ||
| <p><strong>{`Price: ${props.product.price}`}</strong></p> | ||
| <p><strong>{`Rating: ${props.product.rating.rate}/${maxRatings} (${props.product.rating.count} Reviews)`}</strong></p> | ||
| <p><strong>{`Category: ${categories.find((category) => category.id == props.product.category)?.name}`}</strong></p> | ||
|
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. Accessing categories: Good use of categories.find. However, watch out for cases where find might return undefined. To make it more robust, provide a fallback value:
|
||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,20 @@ | ||
| export interface Rating { | ||
| //TODO | ||
| rate: number, | ||
| count: number | ||
| } | ||
|
|
||
| export interface Product { | ||
| //TODO | ||
| id: number, | ||
| title: string, | ||
| price: number, | ||
| desctiption: string, | ||
| category: string, | ||
| image: string, | ||
| rating: Rating | ||
| } | ||
|
|
||
| export interface Category { | ||
| //TODO | ||
| id: string, | ||
| name: string | ||
| } | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.
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.
Exporting categories: You exported categories, which is good for reusability. However, keep in mind:
1.1. If categories is only used in this component, exporting might introduce dead code elsewhere.
1.2. If it is reused (as we see in Card), this solution is appropriate. Consider adding a comment or utility
folder structure for constants (like categories).
Performance Optimization: The categories array is recreated on every render, which is unnecessary. Instead:
You can define it as a const outside the component to ensure it's only created once.
export const categories: Category[] = [...]; // Move outside the function.