34 lines
831 B
TypeScript
34 lines
831 B
TypeScript
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
|
|
export type SearchGame = {
|
|
name: string;
|
|
steamAppId: number;
|
|
image: string;
|
|
releaseDate: string;
|
|
};
|
|
|
|
export const useSearchGamesQuery = (params: { search: string }) => {
|
|
return useQuery({
|
|
queryKey: ["search-games", params],
|
|
queryFn: async () => {
|
|
const q = new URLSearchParams();
|
|
q.set("q", params.search);
|
|
|
|
const resp = await fetch(`/api/search/games?${q.toString()}`);
|
|
return (await resp.json()) as SearchGame[];
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useAddGameMutation = () => {
|
|
return useMutation({
|
|
mutationFn: async (data: { steamAppId: number }) => {
|
|
const resp = await fetch("/api/user/games", {
|
|
method: "POST",
|
|
body: JSON.stringify(data),
|
|
});
|
|
return await resp.json();
|
|
},
|
|
});
|
|
};
|