Create get product composable
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
// imports
|
||||||
|
|
||||||
|
import { useQuery } from "@tanstack/vue-query";
|
||||||
|
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
|
||||||
|
|
||||||
|
// types
|
||||||
|
|
||||||
|
export type GetProductResponse = Product;
|
||||||
|
|
||||||
|
const useGetDoc = (id: string | number | undefined) => {
|
||||||
|
|
||||||
|
// state
|
||||||
|
|
||||||
|
const { $axios: axios } = useNuxtApp();
|
||||||
|
|
||||||
|
// methods
|
||||||
|
|
||||||
|
const handleGetProduct = async (id: string | number | undefined) => {
|
||||||
|
const { data } = await axios.get<GetProductResponse>(`${API_ENDPOINTS.product.get}/${id}`);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
return useQuery({
|
||||||
|
queryKey: [QUERY_KEYS.product, id],
|
||||||
|
queryFn: () => handleGetProduct(id)
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useGetDoc;
|
||||||
Reference in New Issue
Block a user