Skip to content

Commit

Permalink
fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilia991 committed Aug 7, 2023
1 parent ac5572a commit 019143f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 35 deletions.
32 changes: 15 additions & 17 deletions src/app/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import {
useCallback,
useRef,
useEffect,
useState,
} from 'react';
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import { AppDispatch, RootState } from './store';

// eslint-disable-next-line
export function useDebounce<T extends (...args: any[]) => void>(callback: T, delay: number) : (...args: Parameters<T>) => void {
const timer = useRef<NodeJS.Timeout | undefined>();

return useCallback(
(...args: Parameters<T>) => {
if (timer.current) {
clearTimeout(timer.current);
}

timer.current = setTimeout(() => {
callback(...args);
}, delay);
},
[callback, delay],
);
export function useDebounceValue(value: string, delay: number) {
const [debouncedValue, setDebouncedValue] = useState(value);

useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);

return () => {
clearTimeout(handler);
};
}, [value, delay]);

return debouncedValue;
}

export function useLocaleStorage<T>(
Expand Down
26 changes: 15 additions & 11 deletions src/components/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
import {
useAppDispatch,
useAppSelector,
useDebounce,

useDebounceValue,

useLocaleStorage,
} from '../../app/hooks';
import { basketItems, favoriteItems } from '../../app/store';
Expand All @@ -39,10 +41,12 @@ const App = () => {
const [menu, setMenu] = useState(false);
const [searchValue, setSearchValue]
= useState(searchParams.get('query') || '');
const debouncedValue = useDebounceValue(searchValue, 2000);

const query = searchParams.get('query') || '';

const refMenu = useRef<HTMLElement>(null);
const refBtnMenu = useRef<HTMLButtonElement>(null);
const favoriteProduct = useAppSelector(favoriteItems);
const basketProduct = useAppSelector(basketItems);

Expand Down Expand Up @@ -75,8 +79,6 @@ const App = () => {
setSearchParams(searchParams);
};

const debounceQuery = useDebounce(querySearch, 2000);

const onChangeSearch = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchValue(e.target.value);
};
Expand All @@ -88,8 +90,8 @@ const App = () => {
};

useEffect(() => {
debounceQuery(searchValue);
}, [searchValue]);
querySearch(debouncedValue);
}, [debouncedValue]);

useEffect(() => {
dispatch(addAllFavorite(favorite));
Expand All @@ -98,7 +100,8 @@ const App = () => {

useEffect(() => {
const handleOutClickMenu = (e: MouseEvent) => {
if (refMenu.current && !refMenu.current.contains(e.target as Node)) {
if (refMenu.current && !refMenu.current.contains(e.target as Node)
&& refBtnMenu.current && !refBtnMenu.current.contains(e.target as Node)) {
setMenu(false);
}
};
Expand Down Expand Up @@ -241,6 +244,7 @@ const App = () => {
onClick={() => setMenu(!menu)}
type="button"
aria-label="btn"
ref={refBtnMenu}
/>
</div>
</header>
Expand Down Expand Up @@ -327,7 +331,7 @@ const App = () => {
/>
<Route path="/home" element={<Navigate to="/" replace />} />

<Route path="phones">
<Route path="/phones">
<Route
index
element={(
Expand All @@ -344,7 +348,7 @@ const App = () => {
/>
</Route>

<Route path="tablets">
<Route path="/tablets">
<Route
index
element={(
Expand All @@ -361,7 +365,7 @@ const App = () => {
/>
</Route>

<Route path="accessories">
<Route path="/accessories">
<Route
index
element={(
Expand All @@ -379,13 +383,13 @@ const App = () => {
</Route>

<Route
path="favorites"
path="/favorites"
element={
<ProductsList query={query} />
}
/>

<Route path="cart" element={<PageCart />} />
<Route path="/cart" element={<PageCart />} />
</Routes>
</div>
</main>
Expand Down
1 change: 1 addition & 0 deletions src/components/BlockBuyBtn/BlockBuyBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const BlockBuyBtn: React.FC<Props> = ({ item }) => {
{basket.some((i) => i.id === item.id) ? 'Selected' : 'Add to cart'}
{findProduct
&& Object.hasOwnProperty.call(findProduct, 'quantity')
&& findProduct.quantity > 1
? `(${findProduct.quantity})`
: ''}
</button>
Expand Down
2 changes: 1 addition & 1 deletion src/components/PageCart/PageCart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const PageCart = () => {

<button
className="breadcrumbs__go-back"
onClick={() => handGoBack()}
onClick={handGoBack}
type="button"
>
Back
Expand Down
6 changes: 6 additions & 0 deletions src/components/ProductList/ProductsList.scss
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,9 @@
color: $Grey;
}
}

.main {
&__not-found {
height: calc(100vh - 185px);
}
}
13 changes: 10 additions & 3 deletions src/components/ProductList/ProductsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const ProductsList: React.FC<Props> = ({ query }) => {

const [items, setItems] = useState<Product[]>([]);
const [visibleProd, setVisibleProd] = useState<Product[]>([]);
const [filterProducts, setFilterProducts] = useState<Product[]>([]);

const { favoriteItem } = useAppSelector(favoriteItems);

Expand Down Expand Up @@ -107,6 +108,8 @@ export const ProductsList: React.FC<Props> = ({ query }) => {
const filterProduct = cloneP
.filter((c) => c.name.toLowerCase().includes(query.toLowerCase()));

setFilterProducts(filterProduct);

const sortedProducts = filterProduct.sort((p1, p2) => {
switch (sort.toLowerCase()) {
case 'newest':
Expand Down Expand Up @@ -202,7 +205,11 @@ export const ProductsList: React.FC<Props> = ({ query }) => {
}

return (
<>
<div
className={
classNames({ 'main__not-found': visibleProd.length === 0 })
}
>
<div className="breadcrumbs">
<Link to="/home" className="breadcrumbs__home" />
<span className="breadcrumbs__arrow" />
Expand Down Expand Up @@ -258,7 +265,7 @@ export const ProductsList: React.FC<Props> = ({ query }) => {
))}
</ul>
</div>
{perPage !== 'All' && +perPage < items.length && (
{perPage !== 'All' && +perPage < filterProducts.length && (
<div className="mobiel__pagination pagination">
<ul className="pagination__list">
<li className="pagination__item">
Expand Down Expand Up @@ -302,6 +309,6 @@ export const ProductsList: React.FC<Props> = ({ query }) => {
) : (
<h1 className="page__title">Product not found</h1>
)}
</>
</div>
);
};
8 changes: 5 additions & 3 deletions src/components/ProductPage/ProductPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ export const ProductPage = () => {
useEffect(() => {
getProducts()
.then(prod => setAllProducts(prod));
}, []);

useEffect(() => {
getItemProduct(id)
.then(prod => {
setProduct(prod);
setActiveImg(prod.images[0]);
});
}, []);
}, [id]);

const nextImg = (img: string, arr: string[]) => {
const checked = arr.findIndex(imges => imges === img) === arr.length - 1;
Expand Down Expand Up @@ -92,15 +94,15 @@ export const ProductPage = () => {
<span className="breadcrumbs__arrow" />

{arrLocation.map(l => (
<>
<span key={l}>
<Link to={{ pathname: `/${l}` }} className="breadcrumbs__location">
{l.slice(0, 1).toUpperCase() + l.slice(1)}
</Link>

{l !== arrLocation[arrLocation.length - 1] && (
<span className="breadcrumbs__arrow" />
)}
</>
</span>
))}
</div>

Expand Down

0 comments on commit 019143f

Please sign in to comment.