Skip to content

Commit

Permalink
Fix: product page does not scroll to top on product click
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Jan 10, 2025
1 parent 2b32e0a commit 80aeeb0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
20 changes: 14 additions & 6 deletions frontend/src/app/(pages)/(public)/product/[id]/[name]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as UserService from '@/lib/UserService'
import * as WishlistService from '@/lib/WishlistService'
import ProductComponent from '@/components/Product'
import Indicator from '@/components/Indicator'
import ScrollToTop from '@/components/ScrollToTop'

const Product = async (props: { params: Promise<{ id: string, name: string }> }) => {
const params = await props.params
Expand All @@ -30,12 +31,19 @@ const Product = async (props: { params: Promise<{ id: string, name: string }> })
console.error(err)
}

return product ? (
<Suspense fallback={<Indicator />}>
<ProductComponent product={product} />
</Suspense>
) : (
<NoMatch />
return (
<>
<ScrollToTop />
{
product ? (
<Suspense fallback={<Indicator />}>
<ProductComponent product={product} />
</Suspense>
) : (
<NoMatch />
)
}
</>
)
}

Expand Down
13 changes: 13 additions & 0 deletions frontend/src/components/ScrollToTop.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use client'

import { useEffect } from 'react'

const ScrollToTop = () => {
useEffect(() => {
window.scrollTo(0, 0)
}, [])

return null
}

export default ScrollToTop

0 comments on commit 80aeeb0

Please sign in to comment.