Added Customer edit query to Account page

This commit is contained in:
FlorianSpeicher
2025-06-18 14:45:12 +02:00
parent fe956513f2
commit 8522aab583
2 changed files with 32 additions and 7 deletions

View File

@@ -5,17 +5,29 @@ import { DataGrid, GridColDef, GridRowId, GridRowSelectionModel } from "@mui/x-d
import { useMutation, useQuery } from "@tanstack/react-query";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import AccountType, { AdminAccountOperation } from "../../components/Account";
import AccountType, { AdminAccountOperation, CustomerType } from "../../components/Account";
import { useAccount } from "../AccountProvider";
import { deleteAccountAdmin, fetchAccounts } from "../query/Queries";
import { deleteAccountAdmin, fetchAccounts, updateCustomer } from "../query/Queries";
export default function AccountsInfo() {
const theme = useTheme();
const { t } = useTranslation();
function handleCustomerEdit(account: AccountType) {
//TODO: implement
console.log("CustomerEdit", account);
const changeCustomer = useMutation({
mutationFn: (customer: CustomerType) =>
updateCustomer(customer),
});
async function handleCustomerEdit(account: AccountType) {
const customer: CustomerType = {
id: account.customer.id,
name: account.customer.name,
surname: account.customer.surname,
address: account.customer.address,
zip: account.customer.zip,
country: account.customer.country,
};
await changeCustomer.mutateAsync(customer);
}
const [rows, setRows] = useState<AccountType[]>([]);
@@ -47,8 +59,7 @@ export default function AccountsInfo() {
const handleDeleteSelected = async () => {
selectedRows.forEach(async (row) => {
await deleteAccount.mutateAsync({ email: loginData?.email || '', session: loginData?.session || '', accountId: row.id as number });
await deleteAccount.mutateAsync({ email: loginData?.email || '', session: loginData?.session || '', accountId: row.id as number });
})
setRows(rows.filter((row) => !selectedRows.has(row.id)));

View File

@@ -251,3 +251,17 @@ export const fetchOrdersAdmin = async (loginData: User) => {
}
return response.json();
};
export const updateCustomer = async (customer: CustomerType) => {
const response = await fetch('http://localhost:8085/customer?id=' + customer.id, {
method: 'PUT',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(customer),
});
if (!response.ok) {
throw new Error('Fehler beim Ändern des Customers');
}
return await response.json();
}