From 5bf523bc1b2a8f14dcdf852ec9cf6b4cf4b5e307 Mon Sep 17 00:00:00 2001 From: Leonid Date: Sat, 15 Jul 2023 09:58:45 +0300 Subject: [PATCH] solution --- .../pages/DetailsPage/ProductDetailsPage.tsx | 13 +++++++++++-- src/utils/searchActions.ts | 3 ++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/components/pages/DetailsPage/ProductDetailsPage.tsx b/src/components/pages/DetailsPage/ProductDetailsPage.tsx index 8b4afa965f..417ffddc45 100644 --- a/src/components/pages/DetailsPage/ProductDetailsPage.tsx +++ b/src/components/pages/DetailsPage/ProductDetailsPage.tsx @@ -1,4 +1,6 @@ -import { useEffect, useMemo, useState } from 'react'; +import { + useEffect, useMemo, useRef, useState, +} from 'react'; import { useParams } from 'react-router-dom'; import classnames from 'classnames'; import { Product } from '../../../utils/types/Product'; @@ -18,6 +20,7 @@ type Props = { export const ProductDetailsPage: React.FC = ({ products }) => { const [details, setDetails] = useState
(); const { id } = useParams(); + const refPrevId = useRef(''); const [isLoading, setIsLoading] = useState(false); const currentProduct = useMemo(() => { @@ -30,7 +33,13 @@ export const ProductDetailsPage: React.FC = ({ products }) => { }, 100); getDetails(id) - .then((response) => setDetails(response)) + .then((response) => { + setDetails(response); + refPrevId.current = window.location.hash; + }) + .catch(() => { + (window.location.hash = refPrevId.current); + }) .finally(() => { clearInterval(Id); setIsLoading(false); diff --git a/src/utils/searchActions.ts b/src/utils/searchActions.ts index 876b533ca3..fe79400c24 100644 --- a/src/utils/searchActions.ts +++ b/src/utils/searchActions.ts @@ -22,7 +22,8 @@ const SortList = (list: Product[], sortBy: string | null) => { const filterByQuery = (list: Product[], query: string) => { return list.filter(product => product.name .toLocaleLowerCase() - .replaceAll(' ', '').includes(query.toLocaleLowerCase())); + .replaceAll(' ', '').includes(query.toLocaleLowerCase() + .replaceAll(' ', ''))); }; const sliceProductList = (list: Product[], page: string, perPage: string) => {