-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure tags are fetched properly on public facing pages
- Loading branch information
1 parent
3968975
commit a4f4d55
Showing
5 changed files
with
36 additions
and
276 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { default as useTags } from './useTags' | ||
export { default as useTagsPublic } from './useTagsPublic' | ||
export { default as useCreateTag } from './useCreateTag' | ||
export { default as useUpdateTag } from './useUpdateTag' | ||
export { default as useDeleteTag } from './useDeleteTag' |
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,33 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { Tag } from '@prisma/client' | ||
import { useIsAdminMode } from '@hooks/application' | ||
import useSelectedWebSlug from '@hooks/application/useSelectedWebSlug' | ||
|
||
async function fetchTagsRequest({ queryKey }) { | ||
const [_key, { webSlug, all }] = queryKey | ||
const response = await fetch(`/api/tags?web=${webSlug}`) | ||
const { data: tags } = await response.json() | ||
|
||
return all ? tags : tags.filter((tag) => tag.listings.length > 0) | ||
} | ||
|
||
export default function useTags() { | ||
const isAdminMode = useIsAdminMode() | ||
const selectedWebSlug = useSelectedWebSlug() | ||
const { | ||
data: tags, | ||
isPending, | ||
isError, | ||
} = useQuery<Tag[]>({ | ||
queryKey: ['tags', { webSlug: selectedWebSlug, all: isAdminMode }], | ||
queryFn: fetchTagsRequest, | ||
refetchOnWindowFocus: false, | ||
enabled: selectedWebSlug !== undefined, | ||
}) | ||
|
||
return { | ||
tags, | ||
isPending, | ||
isError, | ||
} | ||
} |