first commit
This commit is contained in:
30
features/search/hooks/useSearch.ts
Normal file
30
features/search/hooks/useSearch.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { apiClient } from "@/lib/api";
|
||||
import type { SearchResponse, SearchParams } from "../types";
|
||||
|
||||
export function useSearchProducts(params: SearchParams) {
|
||||
const { q, barcode } = params;
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["search", { q, barcode }],
|
||||
queryFn: async () => {
|
||||
if (barcode) {
|
||||
const response = await apiClient.get<SearchResponse>(
|
||||
`/search-product-barcode?barcode=${barcode}`
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
if (q) {
|
||||
const response = await apiClient.get<SearchResponse>(
|
||||
`/search-product?q=${encodeURIComponent(q)}`
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
return { message: "success", data: [] };
|
||||
},
|
||||
enabled: !!(q && q.length > 0) || !!barcode,
|
||||
staleTime: 1000 * 60 * 5,
|
||||
});
|
||||
}
|
||||
30
features/search/types.ts
Normal file
30
features/search/types.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
// Search Types
|
||||
export interface SearchProduct {
|
||||
id: number;
|
||||
name: string;
|
||||
stock: number;
|
||||
cost_amount: string;
|
||||
price_amount: string;
|
||||
brand: {
|
||||
id: number;
|
||||
name: string;
|
||||
};
|
||||
thumbnail: string;
|
||||
media: Array<{
|
||||
thumbnail: string;
|
||||
images_400x400: string;
|
||||
images_720x720: string;
|
||||
images_800x800: string;
|
||||
images_1200x1200: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
export interface SearchResponse {
|
||||
message: string;
|
||||
data: SearchProduct[];
|
||||
}
|
||||
|
||||
export interface SearchParams {
|
||||
q?: string;
|
||||
barcode?: string;
|
||||
}
|
||||
Reference in New Issue
Block a user