Skip to content
Snippets Groups Projects
Commit 4ec76f7c authored by Sarthak Shrivastava's avatar Sarthak Shrivastava
Browse files

Work in progress change item

parent 9c0b4863
No related branches found
No related tags found
No related merge requests found
// OrganizationItemDetails.jsx
import React, { useState, useEffect } from 'react';
import {useNavigate, useParams} from 'react-router-dom';
import { useNavigate, useParams } from 'react-router-dom';
import Axios from 'axios';
import './OrganizationItemDetails.css'
import './OrganizationItemDetails.css';
const OrganizationItemDetails = ({ token }) => {
const { orgId, itemId } = useParams();
const [itemInfo, setItemInfo] = useState(null);
const [modifiedFields, setModifiedFields] = useState({
status: '',
category: '',
description: '',
name: '',
quantity: 0,
});
const navigate = useNavigate();
const handleDeleteItem = async () => {
// Logic to handle item deletion
if (window.confirm(`Are you sure you want to permanently delete all quantities of ${itemInfo.data[1]}? This will delete the entire item.`)) {
if (
window.confirm(
`Are you sure you want to permanently delete all quantities of ${itemInfo.data[1]}? This will delete the entire item.`
)
) {
console.log('Deleting item...');
try {
const response = await Axios.post('http://localhost:8080/item/user/oneitem/delete', {
......@@ -23,18 +37,77 @@ const OrganizationItemDetails = ({ token }) => {
console.error('Error deleting item');
}
} catch (error) {
console.error('Some unexpected error occured:', error);
console.error('Some unexpected error occurred:', error);
}
}
};
// Add the necessary Axios request to delete the item
const handleModifyItem = async () => {
// Logic to handle item modification
try {
const modifiedFieldsToSend = {
orgId: parseInt(orgId),
jwt: token.jwt,
itemId: parseInt(itemId),
...modifiedFields,
};
// Check if a modified field is left blank and use the current value from itemInfo
Object.keys(modifiedFields).forEach((key) => {
if (modifiedFields[key] === '') {
console.log(key);
if (key === 'status')
modifiedFieldsToSend[key] = itemInfo.data[6];
else if (key === 'quantity')
modifiedFieldsToSend[key] = itemInfo.data[4];
else if (key === 'category')
modifiedFieldsToSend[key] = itemInfo.data[5];
else if (key === 'name')
modifiedFieldsToSend[key] = itemInfo.data[1];
else if (key === 'description')
modifiedFieldsToSend[key] = itemInfo.data[2];
}
});
const response = await Axios.put('http://localhost:8080/item/user/oneitem', modifiedFieldsToSend);
if (response.data.result === 'success') {
// Reload the item details after modification
const fetchItemDetails = async () => {
try {
const response = await Axios.post('http://localhost:8080/item/user/oneitem', {
orgId: parseInt(orgId), // Convert orgId to integer
jwt: token.jwt,
itemId: parseInt(itemId), // Convert itemId to integer
});
if (response.data.result === 'success') {
setItemInfo(response.data);
} else {
console.error('Error fetching item information');
}
} catch (error) {
console.error('Error fetching item information:', error);
}
};
fetchItemDetails();
} else {
console.error('Error modifying item');
}
} catch (error) {
console.error('Some unexpected error occurred:', error);
}
};
// Fetch item details on component mount
useEffect(() => {
const fetchItemDetails = async () => {
try {
const response = await Axios.post('http://localhost:8080/item/user/oneitem', {
orgId: parseInt(orgId), // Convert orgId to integer
orgId: parseInt(orgId),
jwt: token.jwt,
itemId: parseInt(itemId), // Convert itemId to integer
itemId: parseInt(itemId),
});
if (response.data.result === 'success') {
......@@ -66,6 +139,56 @@ const OrganizationItemDetails = ({ token }) => {
<span>Status: {itemInfo.data[6]}</span>
<span>Location: {itemInfo.data[9]}</span>
</div>
{(itemInfo.type === 'OWNER' || itemInfo.type === 'MANAGER') && (
<div>
<h3>Modify Item</h3>
<div className="modify-item-fields">
<label>
Status:
<input
type="text"
value={modifiedFields.status}
onChange={(e) => setModifiedFields({ ...modifiedFields, status: e.target.value })}
/>
</label>
<label>
Category:
<input
type="text"
value={modifiedFields.category}
onChange={(e) => setModifiedFields({ ...modifiedFields, category: e.target.value })}
/>
</label>
<label>
Description:
<input
type="text"
value={modifiedFields.description}
onChange={(e) => setModifiedFields({ ...modifiedFields, description: e.target.value })}
/>
</label>
<label>
Name:
<input
type="text"
value={modifiedFields.name}
onChange={(e) => setModifiedFields({ ...modifiedFields, name: e.target.value })}
/>
</label>
<label>
Quantity:
<input
type="number"
value={modifiedFields.quantity}
onChange={(e) => setModifiedFields({ ...modifiedFields, quantity: e.target.value })}
/>
</label>
</div>
<button onClick={handleModifyItem} className="modify-item-button">
Modify Item
</button>
</div>
)}
{(itemInfo.type === 'OWNER' || itemInfo.type === 'MANAGER') && (
<button onClick={handleDeleteItem} className="delete-item-button">
Delete Item
......
......@@ -115,7 +115,7 @@ CREATE TABLE IF NOT EXISTS ITEM (
owner_email VARCHAR(128),
quantity INT NOT NULL,
category ENUM('STATIONERY', 'MARKETING', 'ELECTRONICS', 'SUPPLIES', 'PERISHABLES', 'MERCHANDISE', 'TOOLS', 'CHEMICALS', 'FLAMMABLE', 'OTHER', 'UNIQUE', 'BOOKS'),
status ENUM('AVAILABLE', 'BORROWED', 'LISTED', 'SOLD') NOT NULL,
status ENUM('AVAILABLE', 'BORROWED') NOT NULL,
location_id INT NOT NULL,
organization_id INT NOT NULL,
PRIMARY KEY (item_id),
......
......@@ -59,10 +59,20 @@ public class CustomItemRepository {
public Object updateItem(Map<String, Object> json)
{
// Extract parameters from the JSON map
Integer itemId = (Integer) json.get("itemId");
Integer itemId;
if (json.get("itemId") instanceof Integer)
itemId = (Integer) json.get("itemId");
else {
itemId = Integer.parseInt((String)json.get("itemId"));
}
Integer quantity;
if (json.get("quantity") instanceof Integer)
quantity = (Integer) json.get("quantity");
else {
quantity = Integer.parseInt((String)json.get("quantity"));
}
String status = (String) json.get("status");
String description = (String) json.get("description");
Integer quantity = (Integer) json.get("quantity");
String category = (String) json.get("category");
String name = (String) json.get("name");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment