-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85c4393
commit 2d371f2
Showing
4 changed files
with
315 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import * as React from "react"; | ||
import { useQuery, useQueryClient } from "@tanstack/react-query"; | ||
|
||
let isOpenCache = false; | ||
let syncedWithQueryParam = false; | ||
|
||
export type UseCartOpenStateOptions = { | ||
syncWithQueryParam?: boolean | { key: string }; | ||
}; | ||
|
||
export const useCartOpenState = ({ | ||
syncWithQueryParam, | ||
}: UseCartOpenStateOptions) => { | ||
const queryClient = useQueryClient(); | ||
|
||
const queryParamKey = React.useMemo(() => { | ||
if (syncWithQueryParam === true) { | ||
return "cart"; | ||
} | ||
if (typeof syncWithQueryParam === "object") { | ||
return syncWithQueryParam.key; | ||
} | ||
return null; | ||
}, [syncWithQueryParam]); | ||
|
||
const { data: isOpen } = useQuery(["cart-open-state"], () => { | ||
if (syncWithQueryParam && !syncedWithQueryParam) return undefined; | ||
return isOpenCache; | ||
}); | ||
|
||
const applyCache = React.useCallback(() => { | ||
queryClient.setQueriesData(["cart-open-state"], isOpenCache); | ||
}, [queryClient]); | ||
|
||
const open = React.useCallback(() => { | ||
isOpenCache = true; | ||
applyCache(); | ||
|
||
if (queryParamKey) { | ||
const url = new URL(window.location.href); | ||
url.searchParams.set(queryParamKey, "open"); | ||
window.history.replaceState({}, "", url.toString()); | ||
} | ||
}, [queryParamKey, applyCache]); | ||
|
||
const close = React.useCallback(() => { | ||
isOpenCache = false; | ||
applyCache(); | ||
|
||
if (queryParamKey) { | ||
const url = new URL(window.location.href); | ||
url.searchParams.delete(queryParamKey); | ||
window.history.replaceState({}, "", url.toString()); | ||
} | ||
}, [queryParamKey, applyCache]); | ||
|
||
const toggle = React.useCallback(() => { | ||
isOpenCache = !isOpenCache; | ||
applyCache(); | ||
|
||
if (queryParamKey) { | ||
const url = new URL(window.location.href); | ||
if (isOpenCache) { | ||
url.searchParams.set(queryParamKey, "open"); | ||
} else { | ||
url.searchParams.delete(queryParamKey); | ||
} | ||
window.history.replaceState({}, "", url.toString()); | ||
} | ||
}, [queryParamKey, applyCache]); | ||
|
||
React.useEffect(() => { | ||
if (queryParamKey) { | ||
const urlParams = new URLSearchParams(window.location.search); | ||
const value = urlParams.get(queryParamKey); | ||
isOpenCache = value === "open"; | ||
syncedWithQueryParam = true; | ||
applyCache(); | ||
} | ||
}, [queryParamKey, applyCache]); | ||
|
||
return { isOpen, open, close, toggle }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
2d371f2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
commerce-toolkit-nextjs-shopify – ./
commerce-toolkit-nextjs-shopify-git-main-basement.vercel.app
commerce-toolkit-nextjs-shopify-basement.vercel.app
commerce-toolkit-nextjs-shopify.vercel.app