Added Customer edit query to Account page
This commit is contained in:
@@ -5,17 +5,29 @@ import { DataGrid, GridColDef, GridRowId, GridRowSelectionModel } from "@mui/x-d
|
|||||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import AccountType, { AdminAccountOperation } from "../../components/Account";
|
import AccountType, { AdminAccountOperation, CustomerType } from "../../components/Account";
|
||||||
import { useAccount } from "../AccountProvider";
|
import { useAccount } from "../AccountProvider";
|
||||||
import { deleteAccountAdmin, fetchAccounts } from "../query/Queries";
|
import { deleteAccountAdmin, fetchAccounts, updateCustomer } from "../query/Queries";
|
||||||
|
|
||||||
export default function AccountsInfo() {
|
export default function AccountsInfo() {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
function handleCustomerEdit(account: AccountType) {
|
const changeCustomer = useMutation({
|
||||||
//TODO: implement
|
mutationFn: (customer: CustomerType) =>
|
||||||
console.log("CustomerEdit", account);
|
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[]>([]);
|
const [rows, setRows] = useState<AccountType[]>([]);
|
||||||
@@ -47,8 +59,7 @@ export default function AccountsInfo() {
|
|||||||
|
|
||||||
const handleDeleteSelected = async () => {
|
const handleDeleteSelected = async () => {
|
||||||
selectedRows.forEach(async (row) => {
|
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)));
|
setRows(rows.filter((row) => !selectedRows.has(row.id)));
|
||||||
|
|||||||
@@ -251,3 +251,17 @@ export const fetchOrdersAdmin = async (loginData: User) => {
|
|||||||
}
|
}
|
||||||
return response.json();
|
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();
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user