Added query for chnage item

This commit is contained in:
FlorianSpeicher
2025-06-22 13:38:40 +02:00
parent 93e7af4ea6
commit e538dd305e
3 changed files with 58 additions and 36 deletions

View File

@@ -9,7 +9,7 @@ import {useTranslation} from "react-i18next";
import Item from "../../components/Item"; import Item from "../../components/Item";
import { mapValueToColor } from "../../util/ColorUtil.tsx"; import { mapValueToColor } from "../../util/ColorUtil.tsx";
import { useAccount } from "../AccountProvider.tsx"; import { useAccount } from "../AccountProvider.tsx";
import {deleteItemQuery, fetchItems} from "../query/Queries.tsx"; import { deleteItemQuery, fetchItems, updateItemAdmin } from "../query/Queries.tsx";
import ItemImageDialog from "./ItemImageDialog.tsx"; import ItemImageDialog from "./ItemImageDialog.tsx";
import NewItemDialog from "./NewItemDialog.tsx"; import NewItemDialog from "./NewItemDialog.tsx";
@@ -79,6 +79,18 @@ export default function ItemsInfo() {
setRows(rows.filter((row) => !selectedRows.has(row.id))); setRows(rows.filter((row) => !selectedRows.has(row.id)));
}; };
const updateItem = useMutation({
mutationFn: (item: Item) =>
updateItemAdmin(item),
});
const handleRowUpdate = async (updatedRow: Item) => {
setRows(rows.map(row => row.id === updatedRow.id ? updatedRow : row));
await updateItem.mutateAsync(updatedRow);
return updatedRow;
}
const columns: GridColDef<(typeof rows)[number]>[] = [ const columns: GridColDef<(typeof rows)[number]>[] = [
{ field: 'id', headerName: 'ID', width: 60 }, { field: 'id', headerName: 'ID', width: 60 },
{ {
@@ -86,7 +98,7 @@ export default function ItemsInfo() {
headerName: t('uuid'), headerName: t('uuid'),
type: "string", type: "string",
width: 120, width: 120,
editable: true editable: false
}, },
{ {
field: 'name', field: 'name',
@@ -221,11 +233,7 @@ export default function ItemsInfo() {
) )
}} }}
showToolbar showToolbar
processRowUpdate={(updatedRow) => { processRowUpdate={handleRowUpdate}
setRows(rows.map(row => row.id === updatedRow.id ? updatedRow : row));
//TODO: make REST callback
return updatedRow;
}}
/> />
{selectedItem && ( {selectedItem && (
<ItemImageDialog <ItemImageDialog

View File

@@ -299,7 +299,7 @@ export const deleteItemQuery = async (uuid: string) => {
}; };
export const updateAccountAdmin = async (account: AccountType, user: User) => { export const updateAccountAdmin = async (account: AccountType, user: User) => {
const response = await fetch('http://localhost:8085/account/admin?email=' + user.email + "&password=" + user.password + "&id=" + account.id + "&admin=" + account.admin, { const response = await fetch('http://localhost:8085/account/admin?email=' + user.email + "&uuid=" + user.session + "&id=" + account.id + "&admin=" + account.admin, {
method: 'POST', method: 'POST',
}); });
if (!response.ok) { if (!response.ok) {
@@ -307,3 +307,17 @@ export const updateAccountAdmin = async (account: AccountType, user: User) => {
} }
return await response.json(); return await response.json();
} }
export const updateItemAdmin = async (item: Item) => {
const response = await fetch('http://localhost:8085/article?uuid=' + item.uuid, {
method: 'PUT',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(item),
});
if (!response.ok) {
throw new Error('Fehler beim Ändern des Items');
}
return await response.json();
}