Minor Fix (order submit)

This commit is contained in:
FlorianSpeicher
2025-06-12 14:59:17 +02:00
parent 5b88f42f26
commit 140c00fefd
3 changed files with 24 additions and 19 deletions

View File

@@ -1,7 +1,7 @@
type OrderType = {
id: string;
date: string;
status: "active" | "inactive" | "running" | "cancelled";
status:"CANCELLED"| "ISSUES"| "DELIVERED"| "ORDERED" | "IN_PROGRESS";
items: { name: string; quantity: number; price: number }[];
total: number;
address: string;
@@ -30,6 +30,6 @@ export type SubmitOrder = {
id: number;
customerId: number;
time: number;
status: "active" | "inactive" | "running" | "cancelled";
status: "CANCELLED"| "ISSUES"| "DELIVERED"| "ORDERED" | "IN_PROGRESS";
orderItems: OrderItem[];
}

View File

@@ -23,7 +23,7 @@ import Item from '../components/Item';
import { BasketItem, useBasket } from '../helper/BasketProvider';
import { ShippingDetails, SubmitOrder } from '../components/Order';
import { useAccount } from '../helper/AccountProvider';
import { useQuery } from '@tanstack/react-query';
import { useQuery, useMutation } from '@tanstack/react-query';
import { submitCustomer, submitOrder } from '../helper/query/Queries';
import { CustomerType } from '../components/Account';
@@ -95,7 +95,7 @@ export default function Payment() {
id: 0, // This will be set by the backend
customerId: user ? user.customerId : 0, // Use user ID if logged in, otherwise 0
time: Date.now(),
status: "active",
status: "ORDERED",
orderItems: basket.map(item => ({
id: item.item.id,
amount: item.quantity,
@@ -117,13 +117,10 @@ export default function Payment() {
</Alert>
};
const { refetch: refetchSubmit, isError: orderIsError } = useQuery({
queryKey: ["submitOrder", submitOrderData],
queryFn: () => submitOrder(submitOrderData),
retry: 0,
retryDelay: 1000,
enabled: false,
});
// Verwende useMutation statt useQuery für submitOrder
const { mutateAsync: submitOrderMutation } = useMutation({
mutationFn: (orderData: SubmitOrder) => submitOrder(orderData),
});
const handleNext = async () => {
let next: boolean = true;
@@ -132,16 +129,24 @@ export default function Payment() {
// Simulate order placement and generate order number
const generatedOrderNumber = `ORD-${Math.floor(Math.random() * 1000000)}`;
setOrderNumber(generatedOrderNumber);
if (user) {
submitOrderData.customerId = user.customerId; // Use logged-in user's customer ID
} else {
console.log(await (await refetchCustomer()).data.id);
submitOrderData.customerId = await (await refetchCustomer()).data.id; // Get the customer ID from the response
let customerId = user ? user.customerId : 0;
if (!customerId) {
const customerResponse = await refetchCustomer();
customerId = customerResponse.data.id;
}
void refetchSubmit(); // Submit order data
if(orderIsError) {
next = false
// Erzeuge die Orderdaten mit der richtigen customerId
const orderData: SubmitOrder = {
...submitOrderData,
customerId,
};
try {
await submitOrderMutation(orderData);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) {
next = false;
}
}
if(next) {