From 8522aab5833ca7a72b25857da1c09271b95be14a Mon Sep 17 00:00:00 2001 From: FlorianSpeicher Date: Wed, 18 Jun 2025 14:45:12 +0200 Subject: [PATCH] Added Customer edit query to Account page --- .../src/helper/adminpanel/AccountsInfo.tsx | 25 +++++++++++++------ 01-frontend/src/helper/query/Queries.tsx | 14 +++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/01-frontend/src/helper/adminpanel/AccountsInfo.tsx b/01-frontend/src/helper/adminpanel/AccountsInfo.tsx index 3bf7a51..bf4043c 100644 --- a/01-frontend/src/helper/adminpanel/AccountsInfo.tsx +++ b/01-frontend/src/helper/adminpanel/AccountsInfo.tsx @@ -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([]); @@ -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))); diff --git a/01-frontend/src/helper/query/Queries.tsx b/01-frontend/src/helper/query/Queries.tsx index 49d7b6d..c21bf2c 100644 --- a/01-frontend/src/helper/query/Queries.tsx +++ b/01-frontend/src/helper/query/Queries.tsx @@ -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(); +}