Skip to content

Commit

Permalink
Upgrade to React Query 5
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathonhawkins92 committed Nov 2, 2023
1 parent faf8c3d commit a917319
Show file tree
Hide file tree
Showing 38 changed files with 416 additions and 252 deletions.
2 changes: 1 addition & 1 deletion components/admin/permissions-list/PermissionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const PermissionsList = ({ permissions }) => {
const { webs } = useWebs()
const { data: session } = useSession()
const { selectedWebId } = useAppContext()
const { mutate: updatePermission, isLoading: isUpdatingPermission } =
const { mutate: updatePermission, isPending: isUpdatingPermission } =
useUpdatePermission()

const onSubmit = useCallback(
Expand Down
12 changes: 4 additions & 8 deletions hooks/categories/useCategories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,21 @@ async function fetchCategoriesRequest({ queryKey }) {
return categories
}

export default function useCategories(): {
categories: Category[]
isLoading: boolean
isError: boolean
} {
export default function useCategories() {
const { selectedWebSlug: webSlug } = useAppContext()
const {
data: categories,
isLoading,
isPending,
isError,
} = useQuery({
} = useQuery<Category[]>({
queryKey: ['categories', { webSlug }],
queryFn: fetchCategoriesRequest,
refetchOnWindowFocus: false,
})

return {
categories,
isLoading,
isPending,
isError,
}
}
8 changes: 6 additions & 2 deletions hooks/categories/useCreateCategory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ export default function useCreateCategory() {
return useMutation({
mutationFn: createCategoryRequest,
onMutate: async (newCategory) => {
await queryClient.cancelQueries(['categories'])
await queryClient.cancelQueries({
queryKey: ['categories'],
})
const previousCategories = queryClient.getQueryData(['categories'])
queryClient.setQueryData(['categories'], newCategory)
return { previousCategories }
},
onSettled: () => {
void queryClient.invalidateQueries(['categories'])
void queryClient.invalidateQueries({
queryKey: ['categories'],
})
},
})
}
4 changes: 3 additions & 1 deletion hooks/categories/useDeleteCategory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export default function useDeleteCategory() {
queryClient.setQueryData(['categories', { webSlug, id: data.id }], data)
},
onSettled: () => {
void queryClient.invalidateQueries(['categories', { webSlug }])
void queryClient.invalidateQueries({
queryKey: ['categories', { webSlug }],
})
},
})
}
8 changes: 6 additions & 2 deletions hooks/categories/useUpdateCategory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export default function useUpdateCategory() {
return useMutation({
mutationFn: updateCategoryRequest,
onMutate: async (newCategory) => {
await queryClient.cancelQueries(['categories'])
await queryClient.cancelQueries({
queryKey: ['categories'],
})
const previousCategories = queryClient.getQueryData(['categories'])
queryClient.setQueryData(['categories'], newCategory)
return { previousCategories, newCategory }
Expand All @@ -33,7 +35,9 @@ export default function useUpdateCategory() {
)
},
onSettled: () => {
void queryClient.invalidateQueries(['categories'])
void queryClient.invalidateQueries({
queryKey: ['categories'],
})
},
})
}
8 changes: 6 additions & 2 deletions hooks/listings/useAddRelation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export default function useAddRelation() {
return useMutation({
mutationFn: addRelationRequest,
onMutate: async (newRelation) => {
await queryClient.cancelQueries(['relations'])
await queryClient.cancelQueries({
queryKey: ['relations'],
})
const previousRelations = queryClient.getQueryData(['relations'])
queryClient.setQueryData(['relations', newRelation.id], newRelation)
return { previousRelations, newRelation }
Expand All @@ -30,7 +32,9 @@ export default function useAddRelation() {
queryClient.setQueryData(['relations'], context.previousRelations)
},
onSettled: () => {
void queryClient.invalidateQueries(['relations'])
void queryClient.invalidateQueries({
queryKey: ['relations'],
})
},
})
}
4 changes: 2 additions & 2 deletions hooks/listings/useAllListings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ async function fetchListingsRequest() {
export default function useAllListings() {
const {
data: listings,
isLoading,
isPending,
isError,
} = useQuery({ queryKey: ['listings'], queryFn: fetchListingsRequest })

return {
listings,
isLoading,
isPending,
isError,
}
}
8 changes: 6 additions & 2 deletions hooks/listings/useCreateListing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export default function useCreateListing() {
return useMutation({
mutationFn: createListingRequest,
onMutate: async (newListing) => {
await queryClient.cancelQueries(['listings'])
await queryClient.cancelQueries({
queryKey: ['listings'],
})
const previousListings = queryClient.getQueryData(['listings'])
queryClient.setQueryData(['listings', newListing.id], newListing)
return { previousListings, newListing }
Expand All @@ -32,7 +34,9 @@ export default function useCreateListing() {
queryClient.setQueryData(['listings'], context.previousListings)
},
onSettled: () => {
void queryClient.invalidateQueries(['listings'])
void queryClient.invalidateQueries({
queryKey: ['listings'],
})
},
})
}
4 changes: 3 additions & 1 deletion hooks/listings/useDeleteListing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export default function useDeleteListing() {
queryClient.setQueryData(['listings', { id: data.id }], data)
},
onSettled: () => {
void queryClient.invalidateQueries(['listings'])
void queryClient.invalidateQueries({
queryKey: ['listings'],
})
},
})
}
4 changes: 2 additions & 2 deletions hooks/listings/useListing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function useListing(listingSlug) {
const { selectedWebSlug: webSlug } = useAppContext()
const {
data: listing,
isLoading,
isPending,
isError,
} = useQuery({
queryKey: ['listing', { webSlug, listingSlug }],
Expand All @@ -28,7 +28,7 @@ export default function useListing(listingSlug) {

return {
listing,
isLoading,
isPending,
isError,
}
}
4 changes: 2 additions & 2 deletions hooks/listings/useListings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function useListings() {
const { selectedWebSlug: webSlug } = useAppContext()
const {
data: listings,
isLoading,
isPending,
isError,
} = useQuery({
queryKey: ['listings', { webSlug }],
Expand All @@ -23,7 +23,7 @@ export default function useListings() {

return {
listings,
isLoading,
isPending,
isError,
}
}
8 changes: 6 additions & 2 deletions hooks/listings/useUpdateListing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default function useUpdateListing() {
queryClient.setQueryData(['listings', { id: data.id }], data)
},
onMutate: async (newListing) => {
await queryClient.cancelQueries(['listings', newListing.id])
await queryClient.cancelQueries({
queryKey: ['listings', newListing.id],
})
const previousListing = queryClient.getQueryData([
'listings',
newListing.id,
Expand All @@ -40,7 +42,9 @@ export default function useUpdateListing() {
)
},
onSettled: () => {
void queryClient.invalidateQueries(['listings'])
void queryClient.invalidateQueries({
queryKey: ['listings'],
})
},
})
}
4 changes: 2 additions & 2 deletions hooks/ownership/useMyOwnerships.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function useMyOwnerships() {

const {
data: ownerships,
isLoading,
isPending,
isError,
} = useQuery({
queryKey: ['my-ownerships'],
Expand All @@ -23,7 +23,7 @@ export default function useMyOwnerships() {

return {
ownerships,
isLoading,
isPending,
isError,
}
}
4 changes: 2 additions & 2 deletions hooks/ownership/useOwnerships.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function useOwnerships() {

const {
data: ownerships,
isLoading,
isPending,
isError,
} = useQuery({
queryKey: ['ownerships', { webSlug }],
Expand All @@ -26,7 +26,7 @@ export default function useOwnerships() {

return {
ownerships,
isLoading,
isPending,
isError,
}
}
4 changes: 2 additions & 2 deletions hooks/permissions/useAllPermissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function useAllPermissions() {

const {
data: permissions,
isLoading,
isPending,
isError,
} = useQuery({
queryKey: ['all-permissions'],
Expand All @@ -23,7 +23,7 @@ export default function useAllPermissions() {

return {
permissions,
isLoading,
isPending,
isError,
}
}
4 changes: 2 additions & 2 deletions hooks/permissions/usePermissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function usePermissions() {

const {
data: permissions,
isLoading,
isPending,
isError,
} = useQuery({
queryKey: ['permission'],
Expand All @@ -24,7 +24,7 @@ export default function usePermissions() {

return {
permissions,
isLoading,
isPending,
isError,
}
}
4 changes: 2 additions & 2 deletions hooks/permissions/usePermissionsForCurrentWeb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function usePermissionsForCurrentWeb() {

const {
data: permissions,
isLoading,
isPending,
isError,
} = useQuery({
queryKey: ['current-web-permissions', { webSlug }],
Expand All @@ -26,7 +26,7 @@ export default function usePermissionsForCurrentWeb() {

return {
data: permissions,
isLoading,
isPending,
isError,
}
}
11 changes: 6 additions & 5 deletions hooks/permissions/useUpdatePermission.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ export default function useUpdatePermission() {
return useMutation({
mutationFn: updatePermissionRequest,
onMutate: async (newPermission) => {
await queryClient.cancelQueries([
'permission',
{ email: newPermission.email },
])
await queryClient.cancelQueries({
queryKey: ['permission', { email: newPermission.email }],
})
const previousPermissions = queryClient.getQueryData([
'permission',
{ email: newPermission.email },
Expand All @@ -42,7 +41,9 @@ export default function useUpdatePermission() {
)
},
onSettled: () => {
void queryClient.invalidateQueries(['permission'])
void queryClient.invalidateQueries({
queryKey: ['permission'],
})
},
})
}
8 changes: 6 additions & 2 deletions hooks/tags/useCreateTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ export default function useCreateTag() {
return useMutation({
mutationFn: createTagRequest,
onMutate: async (newTag) => {
await queryClient.cancelQueries(['tags'])
await queryClient.cancelQueries({
queryKey: ['tags'],
})
const previousTags = queryClient.getQueryData(['tags'])
queryClient.setQueryData(['tags'], newTag)
return { previousTags }
},
onSettled: () => {
void queryClient.invalidateQueries(['tags'])
void queryClient.invalidateQueries({
queryKey: ['tags'],
})
},
})
}
12 changes: 4 additions & 8 deletions hooks/tags/useTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,21 @@ async function fetchTagsRequest({ queryKey }) {
return all ? tags : tags.filter((tag) => tag.listings.length > 0)
}

export default function useTags(): {
tags: Tag[]
isLoading: boolean
isError: boolean
} {
export default function useTags() {
const { selectedWebSlug: webSlug, isAdminMode } = useAppContext()
const {
data: tags,
isLoading,
isPending,
isError,
} = useQuery({
} = useQuery<Tag[]>({
queryKey: ['tags', { webSlug, all: isAdminMode }],
queryFn: fetchTagsRequest,
refetchOnWindowFocus: false,
})

return {
tags,
isLoading,
isPending,
isError,
}
}
Loading

0 comments on commit a917319

Please sign in to comment.