Skip to content

Commit

Permalink
feat: reset dialog state on route change and clean up unused click ha…
Browse files Browse the repository at this point in the history
…ndlers in cart component
  • Loading branch information
franco14lorenzo committed Nov 17, 2024
1 parent bdc0fb6 commit 401cdec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 0 additions & 6 deletions src/app/(store)/components/layout/header/cart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ const Cart = () => {
<Link
className="mt-4 h-10 max-w-fit rounded-full border border-white bg-black px-4 py-2 font-medium text-white hover:bg-opacity-80"
href="/degustaciones"
onClick={() => setDialogOpen(null)}
>
Descubre nuestras catas
<ArrowRight className="ml-2 inline size-5" />
Expand Down Expand Up @@ -143,11 +142,6 @@ const Cart = () => {
items.length === 0 &&
'pointer-events-none cursor-not-allowed opacity-50'
}`}
onClick={() => {
if (items.length !== 0) {
setDialogOpen(null)
}
}}
>
Finalizar compra
</Link>
Expand Down
12 changes: 12 additions & 0 deletions src/app/contexts/dialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import React, {
Dispatch,
SetStateAction,
useContext,
useEffect,
useState
} from 'react'

import { usePathname } from 'next/navigation'

export enum Dialogs {
Menu,
Cart
Expand All @@ -23,6 +26,15 @@ const DialogContext = createContext<DialogContextType | undefined>(undefined)
export function DialogProvider({ children }: { children: React.ReactNode }) {
const [dialogOpen, setDialogOpen] = useState<Dialogs | null>(null)

const pathname = usePathname()
useEffect(() => {
setDialogOpen(null)

return () => {
setDialogOpen(null)
}
}, [pathname])

return (
<DialogContext.Provider value={[dialogOpen, setDialogOpen]}>
{children}
Expand Down

0 comments on commit 401cdec

Please sign in to comment.