- totals.service: multiply weight/cost sums by quantity in category and global totals - setup.service: multiply by quantity in getAllSetups SQL subqueries; expose quantity in getSetupWithItems item list - thread.service: explicitly pass quantity: 1 when inserting resolved item - ItemForm: add Quantity number input (min=1, default=1) after price field - ItemCard: show ×N badge next to item name when quantity > 1 - CollectionView: pass quantity prop to ItemCard in both filtered and grouped views - $setupId.tsx: pass quantity to ItemCard; multiply by quantity in client-side per-setup totals - WeightSummaryCard: multiply by quantity in all chart and legend weight calculations - useItems / useSetups: add quantity to ItemWithCategory / SetupItemWithCategory interfaces Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
73 lines
2.0 KiB
TypeScript
73 lines
2.0 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;
|
|
quantity: number;
|
|
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"] });
|
|
},
|
|
});
|
|
}
|