From f985dae234816decad423c8e4d45d754738e79a5 Mon Sep 17 00:00:00 2001 From: FlorianSpeicher Date: Wed, 18 Jun 2025 15:24:28 +0200 Subject: [PATCH] Added CustomerEditDialog in admin panel. --- .../public/locales/de/translation.json | 3 +- .../public/locales/en/translation.json | 3 +- .../src/helper/adminpanel/AccountsInfo.tsx | 116 ++++++++++-------- .../helper/adminpanel/CustomerEditDialog.tsx | 102 +++++++++++++++ 4 files changed, 171 insertions(+), 53 deletions(-) create mode 100644 01-frontend/src/helper/adminpanel/CustomerEditDialog.tsx diff --git a/01-frontend/public/locales/de/translation.json b/01-frontend/public/locales/de/translation.json index ac36804..cda3bd6 100644 --- a/01-frontend/public/locales/de/translation.json +++ b/01-frontend/public/locales/de/translation.json @@ -141,5 +141,6 @@ "ISSUES": "Probleme", "DELIVERED": "Zugesendet", "CANCELLED": "Storniert", - "fsImage": "FS-Bild" + "fsImage": "FS-Bild", + "changeCustomer": "Kundendaten ändern" } \ No newline at end of file diff --git a/01-frontend/public/locales/en/translation.json b/01-frontend/public/locales/en/translation.json index 239e140..54bbea9 100644 --- a/01-frontend/public/locales/en/translation.json +++ b/01-frontend/public/locales/en/translation.json @@ -141,5 +141,6 @@ "ISSUES": "Issues", "DELIVERED": "Delivered", "CANCELLED": "Cancelled", - "fsImage": "FS-Image" + "fsImage": "FS-Image", + "changeCustomer": "Change Customer Data" } \ No newline at end of file diff --git a/01-frontend/src/helper/adminpanel/AccountsInfo.tsx b/01-frontend/src/helper/adminpanel/AccountsInfo.tsx index bf4043c..00cdc8c 100644 --- a/01-frontend/src/helper/adminpanel/AccountsInfo.tsx +++ b/01-frontend/src/helper/adminpanel/AccountsInfo.tsx @@ -7,27 +7,25 @@ import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import AccountType, { AdminAccountOperation, CustomerType } from "../../components/Account"; import { useAccount } from "../AccountProvider"; -import { deleteAccountAdmin, fetchAccounts, updateCustomer } from "../query/Queries"; +import { deleteAccountAdmin, fetchAccounts } from "../query/Queries"; +import CustomerEditDialog from "./CustomerEditDialog"; export default function AccountsInfo() { const theme = useTheme(); const { t } = useTranslation(); - const changeCustomer = useMutation({ - mutationFn: (customer: CustomerType) => - updateCustomer(customer), + const [customerData, setCustomerData] = useState({ + id: 0, + name: "", + surname: "", + address: "", + zip: "", + country: "" }); 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); + setCustomerData(account.customer); + setOpen(true); } const [rows, setRows] = useState([]); @@ -35,7 +33,7 @@ export default function AccountsInfo() { const { user: loginData } = useAccount(); - const { data } = useQuery({ + const { data, refetch } = useQuery({ queryKey: ["fetchAccounts", loginData], queryFn: () => fetchAccounts(loginData ? loginData : { email: "", password: "", session: "", customerId: -1, isAdmin: false }), retry: 3, @@ -97,45 +95,61 @@ export default function AccountsInfo() { } ]; + const [open, setOpen] = useState(false); + + const handleCustomerEditSubmit = async () => { + setOpen(false); + await refetch(); + }; + return ( - - ( - - - - ) - }} - showToolbar - processRowUpdate={(updatedRow) => { - setRows(rows.map(row => row.id === updatedRow.id ? updatedRow : row)); - return updatedRow; + <> + + ( + + + + ) + }} + showToolbar + processRowUpdate={(updatedRow) => { + setRows(rows.map(row => row.id === updatedRow.id ? updatedRow : row)); + return updatedRow; + }} + /> + + setOpen(false)} + onSubmit={handleCustomerEditSubmit} + customerData={customerData} + setCustomerData={setCustomerData} /> - + ); } diff --git a/01-frontend/src/helper/adminpanel/CustomerEditDialog.tsx b/01-frontend/src/helper/adminpanel/CustomerEditDialog.tsx new file mode 100644 index 0000000..f6b9fa8 --- /dev/null +++ b/01-frontend/src/helper/adminpanel/CustomerEditDialog.tsx @@ -0,0 +1,102 @@ +import { Button, Dialog, DialogActions, DialogContent, DialogTitle, TextField } from "@mui/material"; +import { useMutation } from "@tanstack/react-query"; +import React, { useEffect } from "react"; +import { useTranslation } from "react-i18next"; +import { CustomerType } from "../../components/Account"; +import { updateCustomer } from "../query/Queries"; // Importiere die Funktion für die Registrierung + +type CustomerDialogProps = { + open: boolean; + onClose: () => void; + onSubmit: () => void; // Funktion, die aufgerufen wird, wenn der Login erfolgreich ist + customerData: CustomerType; + setCustomerData: React.Dispatch>; +}; + +const CustomerEditDialog: React.FC = ({ open, onClose, customerData, setCustomerData, onSubmit }) => { + + const { t } = useTranslation(); + + useEffect(() => { + if (open) { + const active = document.activeElement as HTMLElement | null; + if (active && typeof active.blur === "function") { + active.blur(); + } + } + }, [open]); + + const changeCustomer = useMutation({ + mutationFn: (customer: CustomerType) => + updateCustomer(customer), + }); + + const handleSave = async () => { + const customer: CustomerType = { + id: customerData.id, + name: customerData.name, + surname: customerData.surname, + address: customerData.address, + zip: customerData.zip, + country: customerData.country, + }; + await changeCustomer.mutateAsync(customer); + onSubmit(); // Rufe die onSubmit-Funktion auf, um den Dialog zu schließen und die Änderungen zu übernehmen + onClose(); // Schließe den Dialog + } + + return ( + + {t('changeCustomer')} + + setCustomerData(prev => ({ ...prev, name: e.target.value }))} + /> + setCustomerData(prev => ({ ...prev, surname: e.target.value }))} + /> + setCustomerData(prev => ({ ...prev, address: e.target.value }))} + /> + setCustomerData(prev => ({ ...prev, zip: e.target.value }))} + /> + setCustomerData(prev => ({ ...prev, country: e.target.value }))} + /> + + + + + + ); +}; + +export default CustomerEditDialog;