Skip to content
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

Moayad Mohammed - React - Week2 #11

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

TheYodash
Copy link

@TheYodash TheYodash commented Sep 4, 2024

@crevulus crevulus self-assigned this Sep 11, 2024
Copy link

@crevulus crevulus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Moayad,

Good work on this week's homework. You are successfully calling for data and rendering it programmatically.

You have some changes that will need to be made around how you render components, though. You generally should avoid tightly coupling when you can - this is when one component depends on another in order to render: https://dev.to/gvegacl/understanding-coupling-in-react-practices-and-examples-1h1n

Please make the edits and push them to your PR.

padding: 0.5rem .5rem;
background-color: #f1f1f1;
color: #333;
cursor: pointer;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're using the right HTML elements (like a and button), you won't need to set the cursor type. Also in this case, it's causing misleading UX. The whole category box has cursor: pointer, but only the text is actually clickable. This is a little frustrating for the user. 🟠

}

.product-item {
flex: 1 1 300px;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This style is causing you issues. Using a flex-basis of 300px shouldn't be necessary for you. It's also causing the hitbox to be extra large. If you want your items to be nicely centered, try applying flex styles on the parent element (.products) instead of on the children. 🟠

}, [id]);

return (
error ? <h1>There was an error fetching product</h1> :

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you've created a nested ternary. These are not great for the readability and maintainability of your code. What if there's another condition, e.g. if the network call is successful but the data is empty? Instead, try a rendering pattern like the one suggested in this article: https://blog.logrocket.com/ui-design-best-practices-loading-error-empty-state-react/ 🟠


const categoryApiUrl = 'https://fakestoreapi.com/products/categories';

const CategoryController = () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite an odd component structure. You have ProductsController and Categories inside CategoryController. The categories and product should be in separate components, as you've correctly rendered below. Currently, if there's an error in the network call to fetch categories, it will cause the ProductsController not to render. Those two network calls should work independently from one another. 🔴

const Categories = ({ categories, selectedCategory, handleCategoryClick }) => {
return (
<div className="categories">
{ categories.length !== 0 ? (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the network request returns 0 results, for whatever reason? Your data will not be "loading", but your app will show "Loading Categories...". You should instead use a loading state variable and call setIsLoading (or something similar) when the network call is being made. You will need to do this in all places where you do a network call, instead of relying on the length or existence of data to determine if loading is happening. 🔴


return (
errorFetch ? <h1>There was an error fetching products</h1> :
<Products products={products} selectedCategory={selectedCategory}/>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By adding this ProductsController you've managed to keep the Products component as "dumb" as possible - no network logic, just simple UI rendering. This is very smart component-based design. Well done.

@@ -0,0 +1,59 @@
* {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you add some mobile breakpoints, because at the moment you have some odd padding and margin on small screens - and small screens are more commonly used than big screens these days! 🟢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants