Skip to content

Commit

Permalink
added some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sheva10barca committed Aug 4, 2023
1 parent e0d2578 commit 0fa317b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 61 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# React Phone catalog
[DEMO LINK](https://sheva10barca.github.io/react_phone-catalog/)

- If you work alone follow the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline)
- If you work in a team follow the [Work in a team guideline](https://github.com/mate-academy/react_task-guideline/blob/master/team-flow.md#how-to-work-in-a-team)

Expand Down
4 changes: 2 additions & 2 deletions src/components/NoResults/NoResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type Props = {
category: string;
};

export const NoResults: React.FC<Props> = ({ category }) => (
export const NoResults: React.FC<Props> = React.memo(({ category }) => (
<div className="NoResults">
<h1 className="NoResults__title">{`${category} not found`}</h1>
</div>
);
));
103 changes: 53 additions & 50 deletions src/components/ProductsSlider/ProductsSlider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';
import cn from 'classnames';

import { Product } from '../../types/Product';
Expand All @@ -14,61 +14,64 @@ type Props = {
products: Product[];
};

export const ProductsSlider: React.FC<Props> = ({ title, products }) => {
const [startIndex, setStartIndex] = useState(0);
export const ProductsSlider: React.FC<Props> = React.memo(
({ title, products }) => {
const [startIndex, setStartIndex] = useState(0);

const handlePrevClick = () => {
setStartIndex((prevStartIndex) => Math.max(0, prevStartIndex - 1));
};
const handlePrevClick = () => {
setStartIndex((prevStartIndex) => Math.max(0, prevStartIndex - 1));
};

const handleNextClick = () => {
setStartIndex((prevStartIndex) => Math.min(
prevStartIndex + 1,
products.length - 4,
));
};
const handleNextClick = () => {
setStartIndex((prevStartIndex) => Math.min(
prevStartIndex + 1, products.length - 4,
));
};

const visibleCards = products.slice(startIndex, startIndex + 4);
const visibleCards = useMemo(() => {
return products.slice(startIndex, startIndex + 4);
}, [products, startIndex]);

const isPrevButtonDisabled = startIndex === 0;
const isNextButtonDisabled = startIndex === products.length - 4;
const isPrevButtonDisabled = startIndex === 0;
const isNextButtonDisabled = startIndex === products.length - 4;

return (
<div className="ProductsSlider">
<div className="ProductsSlider__content">
<h2 className="ProductsSlider__title">{title}</h2>
return (
<div className="ProductsSlider">
<div className="ProductsSlider__content">
<h2 className="ProductsSlider__title">{title}</h2>

<div className="ProductsSlider__buttons">
<button
type="button"
onClick={handlePrevClick}
disabled={isPrevButtonDisabled}
className={cn('ProductsSlider__button', {
disabled: isPrevButtonDisabled,
})}
>
<img src={arrowLeft} alt="arrow-left" />
</button>
<button
type="button"
onClick={handleNextClick}
disabled={isNextButtonDisabled}
className={cn('ProductsSlider__button', {
disabled: isNextButtonDisabled,
})}
>
<img src={arrowRight} alt="arrow-right" />
</button>
<div className="ProductsSlider__buttons">
<button
type="button"
onClick={handlePrevClick}
disabled={isPrevButtonDisabled}
className={cn('ProductsSlider__button', {
disabled: isPrevButtonDisabled,
})}
>
<img src={arrowLeft} alt="arrow-left" />
</button>
<button
type="button"
onClick={handleNextClick}
disabled={isNextButtonDisabled}
className={cn('ProductsSlider__button', {
disabled: isNextButtonDisabled,
})}
>
<img src={arrowRight} alt="arrow-right" />
</button>
</div>
</div>
</div>

<div className="ProductsSlider__cards" data-cy="cardsContainer">
{visibleCards.map((card) => (
<div className="ProductsSlider__card" key={card.id}>
<ProductCard product={card} />
</div>
))}
<div className="ProductsSlider__cards" data-cy="cardsContainer">
{visibleCards.map((card) => (
<div className="ProductsSlider__card" key={card.id}>
<ProductCard product={card} />
</div>
))}
</div>
</div>
</div>
);
};
);
},
);
4 changes: 2 additions & 2 deletions src/components/ShopByCategory/ShopByCategory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Props = {
products: Product[];
};

export const ShopByCategory: React.FC<Props> = ({ products }) => {
export const ShopByCategory: React.FC<Props> = React.memo(({ products }) => {
const getLengthByCategory = (
arrProducts: Product[],
type: ProductType,
Expand Down Expand Up @@ -66,4 +66,4 @@ export const ShopByCategory: React.FC<Props> = ({ products }) => {
</div>
</div>
);
};
});
5 changes: 5 additions & 0 deletions src/helpers/sortOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const sortOptions = [
{ value: 'age', label: 'Newest' },
{ value: 'name', label: 'Alphabetically' },
{ value: 'price', label: 'Cheapest' },
];
11 changes: 4 additions & 7 deletions src/pages/PhonesPage/PhonesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useSearchParams } from 'react-router-dom';

import { Product } from '../../types/Product';
import { getSearchWith } from '../../helpers/searchHelper';
import { sortOptions } from '../../helpers/sortOptions';

import { ProductCard } from '../../components/ProductCard/ProductCard';
import { DropDown } from '../../components/DropDown/DropDown';
Expand All @@ -17,12 +18,6 @@ type Props = {
phones: Product[];
};

const sortOptions = [
{ value: 'age', label: 'Newest' },
{ value: 'name', label: 'Alphabetically' },
{ value: 'price', label: 'Cheapest' },
];

const smallestPageSize = 4;

export const PhonesPage: React.FC<Props> = React.memo(({ phones }) => {
Expand Down Expand Up @@ -91,7 +86,9 @@ export const PhonesPage: React.FC<Props> = React.memo(({ phones }) => {

const startIndex = (+currentPage - 1) * +pageSize;
const endIndex = startIndex + +pageSize;
const visiblePhones = sortedPhones.slice(startIndex, +endIndex);
const visiblePhones = useMemo(() => {
return sortedPhones.slice(startIndex, +endIndex);
}, [sortedPhones, startIndex, endIndex]);

const phonesLength = sortedPhones.length;
const pageSizes = [smallestPageSize, 8, phonesLength]; // add 16 when will be more phones on API
Expand Down

0 comments on commit 0fa317b

Please sign in to comment.