All checks were successful
CI / ci (push) Successful in 15s
Run biome check --write --unsafe to fix tabs, import ordering, and non-null assertions across entire codebase. Disable a11y rules not applicable to this single-user app. Exclude auto-generated routeTree. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import type { CreateItem } from "../../shared/types";
|
|
import { apiDelete, apiGet, apiPost, apiPut } from "../lib/api";
|
|
|
|
interface ItemWithCategory {
|
|
id: number;
|
|
name: string;
|
|
weightGrams: number | null;
|
|
priceCents: number | null;
|
|
categoryId: number;
|
|
notes: string | null;
|
|
productUrl: string | null;
|
|
imageFilename: string | null;
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
categoryName: string;
|
|
categoryIcon: string;
|
|
}
|
|
|
|
export function useItems() {
|
|
return useQuery({
|
|
queryKey: ["items"],
|
|
queryFn: () => apiGet<ItemWithCategory[]>("/api/items"),
|
|
});
|
|
}
|
|
|
|
export function useItem(id: number | null) {
|
|
return useQuery({
|
|
queryKey: ["items", id],
|
|
queryFn: () => apiGet<ItemWithCategory>(`/api/items/${id}`),
|
|
enabled: id != null,
|
|
});
|
|
}
|
|
|
|
export function useCreateItem() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (data: CreateItem) =>
|
|
apiPost<ItemWithCategory>("/api/items", data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["items"] });
|
|
queryClient.invalidateQueries({ queryKey: ["totals"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateItem() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, ...data }: { id: number } & Partial<CreateItem>) =>
|
|
apiPut<ItemWithCategory>(`/api/items/${id}`, data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["items"] });
|
|
queryClient.invalidateQueries({ queryKey: ["totals"] });
|
|
queryClient.invalidateQueries({ queryKey: ["setups"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteItem() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: number) =>
|
|
apiDelete<{ success: boolean }>(`/api/items/${id}`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["items"] });
|
|
queryClient.invalidateQueries({ queryKey: ["totals"] });
|
|
queryClient.invalidateQueries({ queryKey: ["setups"] });
|
|
},
|
|
});
|
|
}
|