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

Added dynamic Item deletion based on permission.

parent 5819a6d4
No related branches found
No related tags found
No related merge requests found
......@@ -19,3 +19,18 @@
margin-bottom: 10px;
text-align: left; /* Align text to the left within each span */
}
.delete-item-button {
display: block;
margin-top: 20px;
padding: 10px 20px;
color: #fff;
background-color: #d9534f; /* Dark red color */
border: 1px solid #d9534f;
border-radius: 5px;
cursor: pointer;
}
.delete-item-button:hover {
background-color: #c9302c; /* Darker red color on hover */
}
\ No newline at end of file
import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import {useNavigate, useParams} from 'react-router-dom';
import Axios from 'axios';
import './OrganizationItemDetails.css'
const OrganizationItemDetails = ({ token }) => {
const { orgId, itemId } = useParams();
const [itemInfo, setItemInfo] = useState(null);
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.`)) {
console.log('Deleting item...');
try {
const response = await Axios.post('http://localhost:8080/item/user/oneitem/delete', {
orgId: parseInt(orgId), // Convert orgId to integer
jwt: token.jwt,
itemId: parseInt(itemId), // Convert itemId to integer
});
if (response.data.result === 'success') {
navigate(`/organizations/${orgId}/items`);
} else {
console.error('Error deleting item');
}
} catch (error) {
console.error('Some unexpected error occured:', error);
}
}
};
// Add the necessary Axios request to delete the item
useEffect(() => {
const fetchItemDetails = async () => {
try {
......@@ -16,7 +38,7 @@ const OrganizationItemDetails = ({ token }) => {
});
if (response.data.result === 'success') {
setItemInfo(response.data.data);
setItemInfo(response.data);
} else {
console.error('Error fetching item information');
}
......@@ -36,14 +58,19 @@ const OrganizationItemDetails = ({ token }) => {
<div className="organization-item-details">
<h2>Item Details</h2>
<div className="item-details">
<span><strong>Name:</strong> {itemInfo[1]}</span>
<span><strong>Description:</strong> {itemInfo[2]}</span>
<span><strong>Owner Email:</strong> {itemInfo[3]}</span>
<span><strong>Quantity:</strong> {itemInfo[4]}</span>
<span><strong>Category:</strong> {itemInfo[5]}</span>
<span><strong>Status:</strong> {itemInfo[6]}</span>
<span><strong>Location:</strong> {itemInfo[9]}</span>
<span>Name: {itemInfo.data[1]}</span>
<span>Description: {itemInfo.data[2]}</span>
<span>Owner Email: {itemInfo.data[3]}</span>
<span>Quantity: {itemInfo.data[4]}</span>
<span>Category: {itemInfo.data[5]}</span>
<span>Status: {itemInfo.data[6]}</span>
<span>Location: {itemInfo.data[9]}</span>
</div>
{(itemInfo.type === 'OWNER' || itemInfo.type === 'MANAGER') && (
<button onClick={handleDeleteItem} className="delete-item-button">
Delete Item
</button>
)}
</div>
);
};
......
......@@ -38,5 +38,13 @@ public class CustomItemRepository {
return query.getSingleResult();
}
@Transactional
public Object deleteItem(Integer itemId)
{
String nativeQuery = "DELETE FROM ITEM I WHERE I.item_id = :itemId";
Query query = entityManager.createNativeQuery(nativeQuery)
.setParameter("itemId", itemId);
return query.executeUpdate();
}
}
......@@ -90,7 +90,6 @@ public class ItemController {
public @ResponseBody Map<String, Object> getItemToken(@RequestBody Map<String, Object> json)
{
Map<String, Object> response = new HashMap<>();
System.out.println(json.entrySet());
if (!json.containsKey("orgId") || !json.containsKey("jwt") || !json.containsKey("itemId"))
{
response.put("result", "failure - bad request");
......@@ -123,6 +122,40 @@ public class ItemController {
return response;
}
@PostMapping(path="/user/oneitem/delete")
public @ResponseBody Map<String, Object> deleteItemToken(@RequestBody Map<String, Object> json)
{
Map<String, Object> response = new HashMap<>();
System.out.println(json.entrySet());
if (!json.containsKey("orgId") || !json.containsKey("jwt") || !json.containsKey("itemId"))
{
response.put("result", "failure - bad request");
return response;
}
Map<String, Object> map = getUserOrg(json);
System.out.println(map.entrySet());
if (map.get("result").equals("success"))
{
Integer itemId;
if (map.get("type") != OrganizationRoster.Type.MANAGER && map.get("type") != OrganizationRoster.Type.OWNER)
{
response.put("result", "failure - not authorized, user cannot delete");
return response;
}
if (json.get("itemId") instanceof Integer)
itemId = (Integer) json.get("itemId");
else
itemId = Integer.parseInt((String)(json.get("itemId")));
response.put("data", customItemRepository.deleteItem(itemId));
response.put("result", "success");
}
else
{
response.put("result", "failure - not authorized");
}
return response;
}
/*
Private helper function to validate that the user is supposed to see this data
......
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