#Filter URL

1 messages · Page 1 of 1 (latest)

frigid surge
#

I have a topbar where my user can open menu items. One of the items has subitems and they will be directly forwarded to a URL with filter products. So for example they click on the item "Apaszka" they will be routed to the following URL
/products?page=1&category=Apaszka

But now when they click on a item the category gets removed and the id is only attached /products/2
but it should be following /products/2?category=Apaszka so when the user is clicking to the next item they will only see the filter items and not all. Here is my code for the filter
const handleShowAll = useCallback(() => {
setCategoryVisibility(
categories.reduce(
(acc, category) => {
acc[category] = true; // Show all categories.
return acc;
},
{} as { [key: string]: boolean }
)
);
setClickedCategory(null);
newParams.delete('category');
newParams.set('page', '1');
router.push(createUrl(locale + '/products', newParams));
}, [categories, newParams, router, locale]);

const handleFilter = useCallback(
(category: string) => {
const currentCategory = newParams.get('category');

  if (currentCategory === category) {
    handleShowAll();
    return; 
  } else {
    newParams.set('category', category);
    setClickedCategory(category);
    setCategoryVisibility({
      ...Object.keys(categoryVisibility).reduce<{
        [key: string]: boolean;
      }>((acc, cat) => {
        acc[cat] = cat === category;
        return acc;
      }, {}),
    });
  }

  newParams.set('page', '1');
  router.push(createUrl(locale + '/products', newParams));
},
[newParams, router, locale, categoryVisibility, handleShowAll]

);

return null;
}

bold prismBOT
#

Hi @frigid surge. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the provided knowledge sources, it seems like you're trying to maintain the category filter when navigating to a specific product. The issue seems to be that when you're navigating to a specific product, the category parameter is being removed from the URL.

In your handleFilter function, you're correctly setting the category in the URL parameters when a category is selected. However, when navigating to a specific product, you need to ensure that this category parameter is maintained.

Unfortunately, the provided knowledge sources do not contain a direct solution to your problem. However, they do provide some insights into how you can manipulate URL parameters with Next.js.

For instance, in this GitHub issue (https://github.com/vercel/next.js/issues/47123), a user demonstrates how to manipulate URL parameters in a search form. They use the URLSearchParams interface to create a new set of URL parameters based on form data, and then use router.push to navigate to the new URL.

In your case, you might need to ensure that when navigating to a specific product, you include the current category (if any) in the URL parameters. This could potentially be achieved by getting the current URL parameters with useSearchParams, checking if a category is present, and if so, including it in the URL when navigating to a specific product.

Please note that this is a general direction based on the provided knowledge sources, and you might need to adjust it to fit your specific use case. If you need more specific help, I would recommend checking the Next.js documentation (https://nextjs.org/docs) or asking for help in the Next.js GitHub discussions or on Stack Overflow.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@frigid surge kapa.ai is still learning and improving, please let us know how it did by reacting below

frigid surge
#

I think the issue might be somewhere here where I use the filter
export default function PaperCanvasDetails(props: PaperCanvasDetailsProps) {
const [activeImageIndex, setActiveImageIndex] = useState<number>(0);
const [isOpen, setIsOpen] = useState(false);
const router = useRouter();
const toggleOverlay = (id: number) => {
setActiveImageIndex(id);
setIsOpen(!isOpen);
console.log('Image clicked');
};

const newsearchParams = useSearchParams();
const selectedCategory = newsearchParams.get('category');

const filteredpaperCanvas = paperCanvas.filter(
(paperCanvas) =>
!selectedCategory || paperCanvas.category === selectedCategory
);

const id = parseInt(props.params.params.id, 10);
const paperCanvas_ = filteredpaperCanvas.find(
(paperCanvas) => paperCanvas.id === id
);

if (!paperCanvas_)
return (
<>
<div className="flex p-4 justify-center">
<div className="p-12 mb-10 md:m-8 lg:mt-10 shadow shadow-white">
<Link
className="bg-golden-gradient"
href="/paperCanvas"
>
{props.notFoundID}
</Link>
</div>
</div>
</>
);

const handleImageClick = () => {
setActiveImageIndex(
(prevIndex) => (prevIndex + 1) % paperCanvas_.images.length
);
};

const isMobile = typeof window !== 'undefined' && window.innerWidth <= 1024;
const handleThumbnailClick = (index: number) => {
setActiveImageIndex(index);
if (isMobile) {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
}
};

const isFirstImage = id === 1;
const isLastImage = id === paperCanvas.length;

const handlePrevClick = () => {
if (!isFirstImage) {
const prevId = id - 1;
router.push(/paperCanvas/${prevId});
}
};

const handleNextClick = () => {
if (!isLastImage) {
const nextId = id + 1;
router.push(/paperCanvas/${nextId});
}
};

#

I think the issue might be somewhere here where I use the filter @bold prism
export default function PaperCanvasDetails(props: PaperCanvasDetailsProps) {
const [activeImageIndex, setActiveImageIndex] = useState<number>(0);
const [isOpen, setIsOpen] = useState(false);
const router = useRouter();
const toggleOverlay = (id: number) => {
setActiveImageIndex(id);
setIsOpen(!isOpen);
console.log('Image clicked');
};

const newsearchParams = useSearchParams();
const selectedCategory = newsearchParams.get('category');

const filteredpaperCanvas = paperCanvas.filter(
(paperCanvas) =>
!selectedCategory || paperCanvas.category === selectedCategory
);

const id = parseInt(props.params.params.id, 10);
const paperCanvas = filteredpaperCanvas.find(
(paperCanvas) => paperCanvas.id === id
);

if (!paperCanvas)
return (
<>
<div className="flex p-4 justify-center">
<div className="p-12 mb-10">
<Link
className="bg-golden-gradient"
href="/paperCanvas"
>
{props.notFoundID}
</Link>
</div>
</div>
</>
);

const handleImageClick = () => {
setActiveImageIndex(
(prevIndex) => (prevIndex + 1) % paperCanvas_.images.length
);
};

const isMobile = typeof window !== 'undefined' && window.innerWidth <= 1024;
const handleThumbnailClick = (index: number) => {
setActiveImageIndex(index);
if (isMobile) {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
}
};

const isFirstImage = id === 1;
const isLastImage = id === paperCanvas.length;

const handlePrevClick = () => {
if (!isFirstImage) {
const prevId = id - 1;
router.push(/paperCanvas/${prevId});
}
};

const handleNextClick = () => {
if (!isLastImage) {
const nextId = id + 1;
router.push(/paperCanvas/${nextId});
}
};

bold prismBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Based on the provided code and knowledge sources, it seems like the issue might be in the handlePrevClick and handleNextClick functions. When you're navigating to the previous or next product, you're not including the category in the URL parameters.

Here's how you could modify these functions to include the category:

const handlePrevClick = () => {
  if (!isFirstImage) {
    const prevId = id - 1;
    const categoryParam = selectedCategory ? `?category=${selectedCategory}` : '';
    router.push(`/paperCanvas/${prevId}${categoryParam}`);
  }
};

const handleNextClick = () => {
  if (!isLastImage) {
    const nextId = id + 1;
    const categoryParam = selectedCategory ? `?category=${selectedCategory}` : '';
    router.push(`/paperCanvas/${nextId}${categoryParam}`);
  }
};

In this code, we're creating a categoryParam string that includes the category parameter if selectedCategory is not null. We then append this string to the URL when calling router.push.

Please note that this is a general direction based on the provided code and knowledge sources, and you might need to adjust it to fit your specific use case. If you need more specific help, I would recommend checking the Next.js documentation (https://nextjs.org/docs) or asking for help in the Next.js GitHub discussions or on Stack Overflow.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@frigid surge kapa.ai is still learning and improving, please let us know how it did by reacting below

frigid surge
#

I think here when I have my main page with all of the images I also need to adapt something to have the category in the params if available @bold prism
<Fragment key={paperCanvas.id}>
<div className="px-5 py-5">
<Link href={/paperCanvas/${paperCanvas.id}}>
<figure className="">
<Image
src={paperCanvas.coverImage}
title={Artwork ${paperCanvas.title}}
alt={Artwork or textile design products with a specific technique and the name ${paperCanvas.title}}
placeholder="blur"
/>
<div className="relative">
<div className="">
<button className="text-sm bg-golden-gradient text-transparent bg-clip-text ">
{props.seeMoreButton}
</button>
</div>
</div>
<figcaption className="">
<h1 className="">
{paperCanvas.title}
</h1>
<p className="">
{Format: ${paperCanvas.format}}
</p>
<p className="">
{props.price}
</p>
<p className="inline mx-1 italic text-sm font-light decoration-1 decoration-gold bg-golden-gradient text-transparent bg-clip-text">
{paperCanvas.price}
</p>
{/* <p className="inline mt-1 italic text-sm font-bold relative bg-golden-gradient text-transparent bg-clip-text">
{calculateDiscount(artwork)}€
</p> */}
</figcaption>
</figure>
</Link>
</div>
</Fragment>

bold prismBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Based on the provided code and knowledge sources, it seems like you want to include the category in the URL parameters when navigating to a specific product from the main page. You can achieve this by modifying the Link component in your code to include the category parameter if it's available.

Here's how you could modify the Link component:

<Fragment key={paperCanvas.id}>
  <div className="px-5 py-5">
    <Link href={`/paperCanvas/${paperCanvas.id}${selectedCategory ? `?category=${selectedCategory}` : ''}`}>
      {/* ... rest of your code ... */}
    </Link>
  </div>
</Fragment>

In this code, we're appending the category parameter to the URL if selectedCategory is not null.

Please note that this is a general direction based on the provided code and knowledge sources, and you might need to adjust it to fit your specific use case. If you need more specific help, I would recommend checking the Next.js documentation (https://nextjs.org/docs) or asking for help in the Next.js GitHub discussions or on Stack Overflow.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@frigid surge kapa.ai is still learning and improving, please let us know how it did by reacting below

frigid surge
#

I have now the correct URL when clicking on the image but when I want to move to the second one its not working. The issue is that my handlenext is incrementing by one but the second image in the filtering has id 16 @bold prism how can I solve this issue?

bold prismBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Based on the provided knowledge sources and code, it seems like your handleNextClick and handlePrevClick functions are incrementing and decrementing the id by 1. This works fine when there's no category filter applied and all products are displayed in order. However, when a category filter is applied, the products are not necessarily in order of their id.

One way to solve this issue is to find the index of the current product in the filteredpaperCanvas array, and then use this index to navigate to the next or previous product. Here's how you could modify your handleNextClick and handlePrevClick functions:

const currentIndex = filteredpaperCanvas.findIndex((paperCanvas) => paperCanvas.id === id);

const handlePrevClick = () => {{
  if (currentIndex > 0) {{
    const prevId = filteredpaperCanvas[currentIndex - 1].id;
    const categoryParam = selectedCategory ? `?category=${{selectedCategory}}` : '';
    router.push(`/paperCanvas/${{prevId}}${{categoryParam}}`);
  }}
}};

const handleNextClick = () => {{
  if (currentIndex < filteredpaperCanvas.length - 1) {{
    const nextId = filteredpaperCanvas[currentIndex + 1].id;
    const categoryParam = selectedCategory ? `?category=${{selectedCategory}}` : '';
    router.push(`/paperCanvas/${{nextId}}${{categoryParam}}`);
  }}
}};

In this code, we're first finding the index of the current product in the filteredpaperCanvas array. Then, in the handlePrevClick and handleNextClick functions, we're using this index to get the id of the previous or next product.

#

Please note that this is a general direction based on the provided code and knowledge sources, and you might need to adjust it to fit your specific use case. If you need more specific help, I would recommend checking the Next.js documentation (https://nextjs.org/docs) or asking for help in the Next.js GitHub discussions or on Stack Overflow.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@frigid surge kapa.ai is still learning and improving, please let us know how it did by reacting below

frigid surge
#

I would also need to modify my constant so that when a category is selected as well the last item is different then without category

because now when I have my cateogory I have only 3 items and on the image with the index 18 is currently my last element for the category but the next button is still visible as in the whole list I have 20
{!isLastImage && (
<button
className="bg-golden-gradient text-transparent bg-clip-text text-6xl mx-10"
onClick={handleNextClick}
>
>
</button>
)}
@bold prism

bold prismBOT
#

Let me check the knowledge base for you. One moment! :books:

frigid surge
#

const isLastImage = id === filteredpaperCanvas.length;

bold prismBOT
#

Based on the provided knowledge sources and code, it seems like you're displaying a "next" button if the current product is not the last one in the paperCanvas array. However, when a category filter is applied, you should instead check if the current product is the last one in the filteredpaperCanvas array.

Here's how you could modify your code:

const isLastImage = currentIndex === filteredpaperCanvas.length - 1;

{!isLastImage && (
  <button
    className="bg-golden-gradient text-transparent bg-clip-text text-6xl mx-10"
    onClick={handleNextClick}
  >
    &gt;
  </button>
)}

In this code, we're checking if the current product is the last one in the filteredpaperCanvas array by comparing currentIndex to filteredpaperCanvas.length - 1.

Please note that this is a general direction based on the provided code and knowledge sources, and you might need to adjust it to fit your specific use case. If you need more specific help, I would recommend checking the Next.js documentation (https://nextjs.org/docs) or asking for help in the Next.js GitHub discussions or on Stack Overflow.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@frigid surge kapa.ai is still learning and improving, please let us know how it did by reacting below

frigid surge
#

@bold prism I have this as my firstItem const isFirstImage = id === 1;
is this correct or how should I do it when for a category the image with id 2 is the first one then the button will be as well available or?

bold prismBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Based on the provided knowledge sources and code, it seems like you're checking if the current product is the first one by comparing its id to 1. However, similar to the issue with the "next" button, when a category filter is applied, the first product might not have an id of 1.

You should instead check if the current product is the first one in the filteredpaperCanvas array. Here's how you could modify your code:

const isFirstImage = currentIndex === 0;

{{isFirstImage && (
  // ... your code for the "previous" button ...
)}}

In this code, we're checking if the current product is the first one in the filteredpaperCanvas array by checking if currentIndex is 0.

Please note that this is a general direction based on the provided code and knowledge sources, and you might need to adjust it to fit your specific use case. If you need more specific help, I would recommend checking the Next.js documentation (https://nextjs.org/docs) or asking for help in the Next.js GitHub discussions or on Stack Overflow.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: