Skip to content

Commit

Permalink
fix: resolve type related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Victor committed Nov 23, 2023
1 parent 3138c93 commit 081d145
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
17 changes: 14 additions & 3 deletions src/api/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createQuery, createInfiniteQuery } from "react-query-kit";
import { AxiosError } from "axios";
import {
getCityName,
getCurrentLocation,
getQuotes,
getWeather,
getPhotos,
getCloudPhotos,
getLocation,
} from "@api";

export const useQuotes = createQuery({
Expand All @@ -27,7 +27,6 @@ export const useQuotes = createQuery({
},
initialDataUpdatedAt: Date.now() + 1000 * 60 * 60 * 4,
staleTime: 1000 * 60 * 60 * 4, // 4 hours
suspense: true,
});

export const useCurrentLocation = createQuery({
Expand All @@ -50,7 +49,7 @@ export const useCityName = createQuery<
});

export const useWeather = createQuery<
ReturnType<typeof getWeather> extends Promise<infer T> ? T : AxiosError,
Awaited<ReturnType<typeof getWeather>>,
{ longitude: number; latitude: number }
>({
primaryKey: "weather",
Expand All @@ -68,6 +67,7 @@ export const usePhotos = createQuery({
});

export const useCloudPhotos = createInfiniteQuery({
initialPageParam: 0,
primaryKey: "cloud-photos",
queryFn: ({ pageParam = 0 }) => {
// if variable.fetchmore is true, append the result to the already existing photos
Expand All @@ -81,3 +81,14 @@ export const useCloudPhotos = createInfiniteQuery({
return pages.length + 1;
},
});

export const useLocation = createQuery<
Awaited<ReturnType<typeof getLocation>>,
{ query: string }
>({
primaryKey: "location",
queryFn: ({ queryKey }) => {
return getLocation(queryKey[1].query);
},
staleTime: 1000 * 60 * 60 * 24, // 24 hours
});
25 changes: 14 additions & 11 deletions src/components/inc/SideBar/WeatherTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { Box, Text, Flex } from "@components/base";
import { styled } from "stitches.config";
import { SearchIcon } from "@components/icons";
import { Combobox } from "@headlessui/react";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { getLocation } from "@api";
import { useQueryClient } from "@tanstack/react-query";
import { Coordinates } from "@api/types";
import useStore from "@store";
import { shallow } from "zustand/shallow";
import { useCityName, useCurrentLocation, useWeather } from "@api/hooks";
import {
useCityName,
useCurrentLocation,
useLocation,
useWeather,
} from "@api/hooks";

const WeatherTab = () => {
return (
Expand Down Expand Up @@ -51,7 +55,9 @@ function WeatherUnitSelect() {
const queryClient = useQueryClient();
function setUnitAndRefetch(unit: "fahrenheit" | "celsius") {
setUnit(unit);
queryClient.invalidateQueries(useWeather.getKey());
queryClient.invalidateQueries({
queryKey: useWeather.getKey(),
});
}
return (
<Flex gap={2}>
Expand All @@ -74,13 +80,10 @@ function LocationFinder() {
const [value, setValue] = useState("");
const deferredValue = useDeferredValue(value);
const queryClient = useQueryClient();
const { isError, isLoading, data } = useQuery(
["location", deferredValue],
() => getLocation(value),
{
keepPreviousData: true,
},
);

const { isError, isLoading, data } = useLocation({
variables: { query: deferredValue },
});
const [selectedLocation, setSelectedLocation] = useState<null | Coordinates>(
null,
);
Expand Down
5 changes: 2 additions & 3 deletions src/components/inc/ThemeCategories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { Button } from "@components/base/Button";
import useStore from "@store";
import { useQueryClient } from "@tanstack/react-query";
import React from "react";
import { styled } from "stitches.config";
import { shallow } from "zustand/shallow";

const ThemeCategories = () => {
const [keywords, setKeywords] = useStore(
(state) => [state.keywords, state.setKeywords],
shallow
shallow,
);
const queryClient = useQueryClient();
function onClick(keyword: string) {
Expand All @@ -21,7 +20,7 @@ const ThemeCategories = () => {
// add keyword
setKeywords([...keywords, keyword]);
}
queryClient.invalidateQueries(useCloudPhotos.getKey());
queryClient.invalidateQueries({ queryKey: useCloudPhotos.getKey() });
}

return (
Expand Down
5 changes: 2 additions & 3 deletions src/components/inc/widgets/Quotes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { withTour } from "@components/base/Tour";
function Quotes() {
const {
data: quote,
isLoading,
isError,
isSuccess,
refetch: getQuotes,
} = useQuotes({
select: (d) => ({ id: d._id, text: d.content, author: d.author }) as Quote,
Expand All @@ -27,7 +26,7 @@ function Quotes() {
],
shallow,
);
if (isLoading || isError) return null;
if (!isSuccess) return null;
const tweetText = `I love this quote by ${quote.author}!
${quote.text}”`;
const favourite = favouriteQuotes.includes(quote);
Expand Down
4 changes: 2 additions & 2 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createSyncStoragePersister } from "@tanstack/query-sync-storage-persist
export const queryClient = new QueryClient({
defaultOptions: {
queries: {
cacheTime: 1000 * 60 * 60 * 24, // 24 hours
gcTime: 1000 * 60 * 60 * 24, // 24 hours
},
},
});
Expand All @@ -38,5 +38,5 @@ ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<Router />
</ErrorBoundary>
</PersistQueryClientProvider>
</React.StrictMode>
</React.StrictMode>,
);

0 comments on commit 081d145

Please sign in to comment.