import React, { useEffect, useState } from 'react'; import… · The Chat Archive
Explore / Programming import React, { useEffect, useState } from 'react'; import axios from 'axios'; import {… Save Share Download ▾ Reportimport React, { useEffect, useState } from 'react';
import axios from 'axios';
import { Link } from 'react-router-dom';
const AssetInventory = () => {
const [assets, setAssets] = useState([]);
const [searchQuery, setSearchQuery] = useState('');
const [filteredAssets, setFilteredAssets] = useState([]);
Show full message · 369 lines Assistant2025-03-30 18:07 UTC Copy response
Assistant2025-03-30 18:15 UTC Copy response
Assistant2025-03-30 18:22 UTC Copy response
Assistant2025-03-30 18:28 UTC Copy response
Assistant2025-03-30 18:36 UTC Copy response
Copy first prompt
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 5 (10 messages)
Language en
Conversation date Mar 30, 2025
Added to archive Nov 16, 2026
ID 01m7h42jj18w4bdhyxv0kty843Source & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 7ac2eecd266c7c2abcd43a6ca6f3af27
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
useEffect(() => {
fetchData();
}, []);
useEffect(() => {
// Filter assets whenever searchQuery changes
const filtered = assets.filter(asset => {
return (
(asset.assetId && asset.assetId.toString().toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.vendor && asset.vendor.toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.applicationName && asset.applicationName.toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.serialNumber && asset.serialNumber.toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.contactPerson && asset.contactPerson.toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.location && asset.location.toLowerCase().includes(searchQuery.toLowerCase()))
);
});
setFilteredAssets(filtered);
}, [searchQuery, assets]);
const fetchData = async () => {
try {
const response = await axios.get('/api/assets');
setAssets(response.data);
setFilteredAssets(response.data); // Set initial filtered assets
} catch (error) {
console.error('Error fetching assets:', error);
}
};
const handleEditAsset = (assetId) => {
window.location.href = `/edit-asset/${assetId}`;
};
const handleDeleteAsset = async (assetId) => {
if (window.confirm('Are you sure you want to delete this asset?')) {
try {
await axios.delete(`/api/assets/${assetId}`);
setAssets(assets.filter(asset => asset.assetId !== assetId));
setFilteredAssets(filteredAssets.filter(asset => asset.assetId !== assetId));
alert('Asset deleted successfully!');
} catch (error) {
console.error('Error deleting asset:', error);
}
}
};
return (
<div className="bg-gray-100">
<nav className="flex justify-between items-center p-4 bg-white shadow-md">
<div className="space-x-4">
<a href="/admin-dashboard" className="text-blue-600 hover:text-gray-900">Home</a>
</div>
<div className="space-x-4">
<a href="/" className="text-blue-600 hover:text-gray-900">Logout</a>
</div>
</nav>
<div className="container max-w-6xl mt-30 p-6 bg-gray-100 rounded-lg ml-40">
<div className="text-2xl font-bold text-center mb-6 text-black">License Inventory Management</div>
<div className="text-right mb-6">
<Link to="/add-newAsset">
<button className="bg-blue-400 text-white rounded px-4 py-2 hover:bg-blue-600">Add New License</button>
</Link>
<input
type="text"
placeholder="Search..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="bg-gray-200 text-gray-800 rounded px-4 py-2 ml-2"
/>
</div>
<div className="overflow-x-auto">
<table className="min-w-full bg-white rounded-lg overflow-hidden shadow-md">
<thead>
<tr className="bg-gray-100 text-left">
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Line Item</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Application Name</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Quantity</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Start Date</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">End Date</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Serial Number</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Renewal Cost</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Vendor</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Contact Person</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Email</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Contact Number</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Actions</th>
</tr>
</thead>
<tbody>
{filteredAssets.map(asset => (
<tr key={asset.assetId} className="border-b border-gray-200">
<td className="px-6 py-4">{asset.assetId}</td>
<td className="px-6 py-4">{asset.vendor}</td>
<td className="px-6 py-4">{asset.applicationName}</td>
<td className="px-6 py-4">{asset.qty}</td>
<td className="px-6 py-4">{asset.used}</td>
<td className="px-6 py-4">{asset.remaining}</td>
<td className="px-6 py-4">{new Date(asset.startDate).toLocaleDateString()}</td>
<td className="px-6 py-4">{new Date(asset.endDate).toLocaleDateString()}</td>
<td className="px-6 py-4">{asset.contactPerson}</td>
<td className="px-6 py-4">{asset.serialNumber}</td> {/* Display the serial number */}
<td className="px-6 py-4">
<div className='flex'>
<button
className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
onClick={() => handleEditAsset(asset.assetId)}>Edit</button>
<button
className="bg-red-500 text-white px-4 py-2 rounded-md hover:bg-red-600 focus:outline-none focus:bg-red-600 ml-2"
onClick={() => handleDeleteAsset(asset.assetId)}>Delete</button>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
export default AssetInventory; in thid the things are not working perferclty as we fill the form of
import React, { useState } from 'react';
const AddNewAsset = () => {
const [assetData, setAssetData] = useState({
serialNumber: '', // Optional but unique
assetId: '', // Required
vendor: '', // Required
applicationName: '', // Required
qty: '', // Required
used: '', // Optional
remaining: '', // Optional
renewalCost: '', // Optional
startDate: '', // Required
endDate: '', // Required
contactPerson: '', // Required
emailId: '', // Required
contactNumber: '' // Required
});
code Copy code
const handleInputChange = (event) => {
const { name, value } = event.target;
setAssetData({
...assetData,
[name]: value,
});
};
const handleSubmit = async (event) => {
event.preventDefault();
try {
const response = await fetch('/api/assets', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(assetData),
});
if (!response.ok) {
throw new Error('Failed to add asset');
}
// Clear form fields on successful submission (optional)
setAssetData({
serialNumber: '', // Reset serial number
assetId: '',
vendor: '',
applicationName: '',
qty: '',
used: '',
remaining: '',
renewalCost: '',
startDate: '',
endDate: '',
contactPerson: '',
emailId: '',
contactNumber: ''
});
// Redirect or show success message
alert('Asset added successfully');
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
} catch (error) {
console.error('Error adding asset:', error);
alert('Failed to add asset. Please try again.');
}
};
return (
<div className="container mx-auto p-4 w-1/2 mt-10 p-6 bg-gray-100 rounded-lg">
<div className="text-2xl text-center font-bold mb-4 text-black">Add New License</div>
<form onSubmit={handleSubmit} className="max-w-md mx-auto" id="assetForm">
<div className="mb-4">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">Line Item:</label>
<input
type="text"
id="assetId"
name="assetId"
placeholder="Enter asset ID"
value={assetData.assetId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">Application Name:</label>
<input
type="text"
id="applicationName"
name="applicationName"
placeholder="Enter application name"
value={assetData.applicationName}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">Quantity:</label>
<input
type="number"
id="qty"
name="qty"
placeholder="Enter quantity"
value={assetData.qty}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date:</label>
<input
type="date"
id="startDate"
name="startDate"
value={assetData.startDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date:</label>
<input
type="date"
id="endDate"
name="endDate"
value={assetData.endDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">Serial Number:</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
placeholder="Enter serial number (optional, must be unique)"
value={assetData.serialNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost:</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
placeholder="Enter renewal cost (optional)"
value={assetData.renewalCost}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">Vendor Name:</label>
<input
type="text"
id="vendor"
name="vendor"
placeholder="Enter vendor name"
value={assetData.vendor}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person:</label>
<input
type="text"
id="contactPerson"
name="contactPerson"
placeholder="Enter contact person"
value={assetData.contactPerson}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID:</label>
<input
type="email"
id="emailId"
name="emailId"
placeholder="Enter email ID"
value={assetData.emailId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number:</label>
<input
type="text"
id="contactNumber"
name="contactNumber"
placeholder="Enter contact number"
value={assetData.contactNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="flex justify-center">
<button
type="submit"
className="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
<button
type="button"
className="bg-gray-400 text-gray-700 py-2 px-4 ml-2 rounded-md hover:bg-gray-300 focus:outline-none focus:bg-gray-300"
onClick={() => {
window.location.href = '/asset-inventory';
}}
>
Cancel
</button>
</div>
</form>
</div>
);
export default AddNewAsset; means not line up perefctly brlow the correct fields in inventory page
The issue you're describing seems to revolve around the proper alignment of fields in the AssetInventory component, particularly when displaying the assets in the inventory table. There may be multiple reasons for the misalignment, including issues related to the data being sent to the backend, the format of the data, or rendering inconsistencies in the table.
Here are steps to troubleshoot and fix potential alignment issues in your AssetInventory component:
1. Check the Data Being Returned
Ensure that the data you receive from your API matches the expected structure. The filteredAssets mapped in the tbody should correspond correctly to the dataset defined in the form for adding assets.
2. Verify Field Names
Make sure that when you are rendering the filteredAssets, the keys you are trying to access exist in the objects. For example, in your filteredAssets.map function, ensure that all keys are correct:
javascript Copy code
<td className="px-6 py-4">{asset.assetId}</td>
<td className="px-6 py-4">{asset.vendor}</td>
<td className="px-6 py-4">{asset.applicationName}</td>
<td className="px-6 py-4">{asset.qty}</td>
<td className="px-6 py-4">{asset.used}</td> // Make sure this is correct
<td className="px-6 py-4">{asset.remaining}</td> // Make sure this is correct
<td className="px-6 py-4">{new Date(asset.startDate).toLocaleDateString()}</td>
<td className="px-6 py-4">{new Date(asset.endDate).toLocaleDateString()}</td>
<td className="px-6 py-4">{asset.contactPerson}</td>
<td className="px-6 py-4">{asset.serialNumber}</td>
// Make sure that other fields are being accessed correctly.
3. Ensure Consistent Data Types
When dynamically rendering data, ensure the data types are consistent. For example, if qty, used, or remaining are numbers in the database, ensure you’re correctly passing those as numbers and handling the formatting if necessary.
4. Add CSS for Consistency
If your layout is inconsistent across different rows or columns, you can add additional CSS styles to ensure they align appropriately. For example, the width of your columns can be fixed:
css Copy code
table {
width: 100%; /* Make sure table spans full width */
}
th, td {
text-align: left; /* Align text */
padding: 8px; /* Add some padding */
}
th {
background-color: #f2f2f2; /* Different background for the header */
}
5. Ensure Valid JSX in the Return
Review your return statement in the AssetInventory component. Ensure that each piece of JSX inside the return statement is properly formatted. A small mistake in JSX can lead to unexpected behaviors.
6. Use Chrome DevTools
Use Chrome DevTools (or any developer tools in your browser) to inspect the rendered HTML. You can look for misalignment in the elements or spacing issues in the DOM tree.
Example of Corrections
Here's a cleaned-up structure and a few minor corrections:
javascript Copy code
{
filteredAssets.map(asset => (
<tr key={asset.assetId} className="border-b border-gray-200">
<td className="px-6 py-4">{asset.assetId}</td>
<td className="px-6 py-4">{asset.applicationName}</td>
<td className="px-6 py-4">{asset.qty}</td>
<td className="px-6 py-4">{new Date(asset.startDate).toLocaleDateString()}</td>
<td className="px-6 py-4">{new Date(asset.endDate).toLocaleDateString()}</td>
<td className="px-6 py-4">{asset.serialNumber}</td>
<td className="px-6 py-4">{asset.renewalCost}</td>
<td className="px-6 py-4">{asset.vendor}</td>
<td className="px-6 py-4">{asset.contactPerson}</td>
<td className="px-6 py-4">{asset.emailId}</td>
<td className="px-6 py-4">{asset.contactNumber}</td>
<td className="px-6 py-4">
<div className='flex'>
<button
className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600 focus:outline-none"
onClick={() => handleEditAsset(asset.assetId)}>Edit
</button>
<button
className="bg-red-500 text-white px-4 py-2 rounded-md hover:bg-red-600 focus:outline-none ml-2"
onClick={() => handleDeleteAsset(asset.assetId)}>Delete
</button>
</div>
</td>
</tr>
))
}
Conclusion
By going through these troubleshooting steps, you should be able to identify and rectify any alignment or data formatting issues you are having with your asset inventory display. If there are still issues, please provide specific examples of what is misaligned, and I can give more targeted help!
wait i will provode all three codes files and then fix all that and remove used and remaining from both form and fisplay of inventory
import React, { useState } from 'react';
const AddNewAsset = () => {
const [assetData, setAssetData] = useState({
serialNumber: '', // Optional but unique
assetId: '', // Required
vendor: '', // Required
applicationName: '', // Required
qty: '', // Required
used: '', // Optional
remaining: '', // Optional
renewalCost: '', // Optional
startDate: '', // Required
endDate: '', // Required
contactPerson: '', // Required
emailId: '', // Required
contactNumber: '' // Required
});
code Copy code
const handleInputChange = (event) => {
const { name, value } = event.target;
setAssetData({
...assetData,
[name]: value,
});
};
const handleSubmit = async (event) => {
event.preventDefault();
try {
const response = await fetch('/api/assets', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(assetData),
});
if (!response.ok) {
throw new Error('Failed to add asset');
}
// Clear form fields on successful submission (optional)
setAssetData({
serialNumber: '', // Reset serial number
assetId: '',
vendor: '',
applicationName: '',
qty: '',
used: '',
remaining: '',
renewalCost: '',
startDate: '',
endDate: '',
contactPerson: '',
emailId: '',
contactNumber: ''
});
// Redirect or show success message
alert('Asset added successfully');
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
} catch (error) {
console.error('Error adding asset:', error);
alert('Failed to add asset. Please try again.');
}
};
return (
<div className="container mx-auto p-4 w-1/2 mt-10 p-6 bg-gray-100 rounded-lg">
<div className="text-2xl text-center font-bold mb-4 text-black">Add New License</div>
<form onSubmit={handleSubmit} className="max-w-md mx-auto" id="assetForm">
<div className="mb-4">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">Line Item:</label>
<input
type="text"
id="assetId"
name="assetId"
placeholder="Enter asset ID"
value={assetData.assetId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">Application Name:</label>
<input
type="text"
id="applicationName"
name="applicationName"
placeholder="Enter application name"
value={assetData.applicationName}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">Quantity:</label>
<input
type="number"
id="qty"
name="qty"
placeholder="Enter quantity"
value={assetData.qty}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date:</label>
<input
type="date"
id="startDate"
name="startDate"
value={assetData.startDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date:</label>
<input
type="date"
id="endDate"
name="endDate"
value={assetData.endDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">Serial Number:</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
placeholder="Enter serial number (optional, must be unique)"
value={assetData.serialNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost:</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
placeholder="Enter renewal cost (optional)"
value={assetData.renewalCost}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">Vendor Name:</label>
<input
type="text"
id="vendor"
name="vendor"
placeholder="Enter vendor name"
value={assetData.vendor}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person:</label>
<input
type="text"
id="contactPerson"
name="contactPerson"
placeholder="Enter contact person"
value={assetData.contactPerson}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID:</label>
<input
type="email"
id="emailId"
name="emailId"
placeholder="Enter email ID"
value={assetData.emailId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number:</label>
<input
type="text"
id="contactNumber"
name="contactNumber"
placeholder="Enter contact number"
value={assetData.contactNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="flex justify-center">
<button
type="submit"
className="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
<button
type="button"
className="bg-gray-400 text-gray-700 py-2 px-4 ml-2 rounded-md hover:bg-gray-300 focus:outline-none focus:bg-gray-300"
onClick={() => {
window.location.href = '/asset-inventory';
}}
>
Cancel
</button>
</div>
</form>
</div>
);
};
export default AddNewAsset; import React, { useState, useEffect } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import axios from 'axios';
const AddAssetForm = () => {
const { assetId } = useParams();
const navigate = useNavigate();
};
export default AddAssetForm;
import React, { useEffect, useState } from 'react';
import axios from 'axios';
import { Link } from 'react-router-dom';
const AssetInventory = () => {
const [assets, setAssets] = useState([]);
const [searchQuery, setSearchQuery] = useState('');
const [filteredAssets, setFilteredAssets] = useState([]);
};
export default AssetInventory;
Line Item:
Enter asset ID
Application Name:
Enter application name
Quantity:
Enter quantity
Start Date:
dd-mm-yyyy
End Date:
dd-mm-yyyy
Serial Number:
Enter serial number (optional, must be unique)
Renewal Cost:
Enter renewal cost (optional)
Vendor Name:
Enter vendor name
Contact Person:
Enter contact person
Email ID:
Enter email ID
Contact Number:
Enter contact number
Save
Cancel keep this format in same order and also mark astrisk for important fielfs and provide all three upadted fiels
Show full message · 671 lines Sure! Below are the updated versions of the three components (AddNewAsset, AddAssetForm, and AssetInventory). I've removed the used and remaining fields as per your request and adjusted the markup to include asterisks for required fields while maintaining the requested format.
1. AddNewAsset.js
javascript Copy code
import React, { useState } from 'react';
const AddNewAsset = () => {
const [assetData, setAssetData] = useState({
serialNumber: '', // Optional but unique
assetId: '', // Required
vendor: '', // Required
applicationName: '', // Required
qty: '', // Required
renewalCost: '', // Optional
startDate: '', // Required
endDate: '', // Required
contactPerson: '', // Required
emailId: '', // Required
contactNumber: '' // Required
});
const handleInputChange = (event) => {
const { name, value } = event.target;
setAssetData({
...assetData,
[name]: value,
});
};
const handleSubmit = async (event) => {
event.preventDefault();
try {
const response = await fetch('/api/assets', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(assetData),
});
if (!response.ok) {
throw new Error('Failed to add asset');
}
// Clear form fields on successful submission (optional)
setAssetData({
serialNumber: '',
assetId: '',
vendor: '',
applicationName: '',
qty: '',
renewalCost: '',
startDate: '',
endDate: '',
contactPerson: '',
emailId: '',
contactNumber: '',
});
// Redirect or show success message
alert('Asset added successfully');
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
} catch (error) {
console.error('Error adding asset:', error);
alert('Failed to add asset. Please try again.');
}
};
return (
<div className="container mx-auto p-4 w-1/2 mt-10 p-6 bg-gray-100 rounded-lg">
<div className="text-2xl text-center font-bold mb-4 text-black">Add New License</div>
<form onSubmit={handleSubmit} className="max-w-md mx-auto" id="assetForm">
<div className="mb-4">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">Line Item: <span className="text-red-500">*</span></label>
<input
type="text"
id="assetId"
name="assetId"
placeholder="Enter asset ID"
value={assetData.assetId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">Application Name: <span className="text-red-500">*</span></label>
<input
type="text"
id="applicationName"
name="applicationName"
placeholder="Enter application name"
value={assetData.applicationName}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">Quantity: <span className="text-red-500">*</span></label>
<input
type="number"
id="qty"
name="qty"
placeholder="Enter quantity"
value={assetData.qty}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="startDate"
name="startDate"
value={assetData.startDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="endDate"
name="endDate"
value={assetData.endDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">Serial Number:</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
placeholder="Enter serial number (optional, must be unique)"
value={assetData.serialNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost:</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
placeholder="Enter renewal cost (optional)"
value={assetData.renewalCost}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">Vendor Name: <span className="text-red-500">*</span></label>
<input
type="text"
id="vendor"
name="vendor"
placeholder="Enter vendor name"
value={assetData.vendor}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactPerson"
name="contactPerson"
placeholder="Enter contact person"
value={assetData.contactPerson}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID: <span className="text-red-500">*</span></label>
<input
type="email"
id="emailId"
name="emailId"
placeholder="Enter email ID"
value={assetData.emailId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactNumber"
name="contactNumber"
placeholder="Enter contact number"
value={assetData.contactNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="flex justify-center">
<button
type="submit"
className="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
<button
type="button"
className="bg-gray-400 text-gray-700 py-2 px-4 ml-2 rounded-md hover:bg-gray-300 focus:outline-none focus:bg-gray-300"
onClick={() => {
window.location.href = '/asset-inventory';
}}
>
Cancel
</button>
</div>
</form>
</div>
);
};
export default AddNewAsset;
2. AddAssetForm.js
3. AssetInventory.js
Summary of Changes
Removed Used and Remaining Fields: These fields were removed from both the add and edit forms.
Added Asterisks for Required Fields: Each required field now has an asterisk (*) next to it to denote that it's important.
Formatted Consistently: Both of the forms are ensured to have a consistent and clear layout.
You should now have an updated implementation that matches your specifications! If you need more modifications or support, feel free to ask.
Show full message · 632 lines import React, { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
const AddNewAsset = () => {
const { id } = useParams(); // Get asset ID from URL for editing
code Copy code
const [assetData, setAssetData] = useState({
assetId: '', // Required
vendor: '', // Required
applicationName: '', // Required
qty: '', // Required
used: '', // Optional
remaining: '', // Optional
renewalCost: '', // Optional
startDate: '', // Required
endDate: '', // Required
contactPerson: '', // Required
emailId: '', // Required
contactNumber: '', // Required
serialNumber: '' // Required but unique
});
// Fetch asset details if an ID is provided (for editing)
useEffect(() => {
if (id) {
const fetchAssetDetails = async () => {
try {
const response = await fetch(`/api/assets/${id}`);
const data = await response.json();
setAssetData(data);
} catch (error) {
console.error('Error fetching asset details:', error);
}
};
fetchAssetDetails();
}
}, [id]);
const handleInputChange = (event) => {
const { name, value } = event.target;
setAssetData({
...assetData,
[name]: value,
});
};
const handleSubmit = async (event) => {
event.preventDefault();
try {
const method = id ? 'PUT' : 'POST'; // Determine method based on whether we are editing or adding
const response = await fetch(`/api/assets${id ? '/' + id : ''}`, {
method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(assetData),
});
if (!response.ok) {
throw new Error('Failed to add/edit asset');
}
// Clear form fields on successful submission (optional)
setAssetData({
assetId: '',
vendor: '',
applicationName: '',
qty: '',
used: '',
remaining: '',
renewalCost: '',
startDate: '',
endDate: '',
contactPerson: '',
emailId: '',
contactNumber: '',
serialNumber: ''
});
// Redirect or show success message
alert(id ? 'Asset edited successfully' : 'Asset added successfully');
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
} catch (error) {
console.error('Error adding/editing asset:', error);
alert('Failed to add/edit asset. Please try again.');
}
};
return (
<div className="container mx-auto p-4 w-1/2 mt-10 p-6 bg-gray-100 rounded-lg">
<div className="text-2xl text-center font-bold mb-4 text-black">
{id ? 'Edit Asset' : 'Add New Asset'}
</div>
<form onSubmit={handleSubmit} className="max-w-md mx-auto" id="assetForm">
<div className="mb-4">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">
Serial Number: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
placeholder="Enter serial number"
value={assetData.serialNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required and must be unique.</small>
</div>
<div className="mb-4">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">
Asset ID: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="assetId"
name="assetId"
placeholder="Enter asset ID"
value={assetData.assetId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
readOnly={id ? true : false} // Make ID field read-only if editing
/>
<small className="text-gray-500">This field is required and must be unique.</small>
</div>
<div className="mb-4">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">
Vendor: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="vendor"
name="vendor"
placeholder="Enter vendor name"
value={assetData.vendor}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">
Application Name: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="applicationName"
name="applicationName"
placeholder="Enter application name"
value={assetData.applicationName}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">
Quantity: <span className="text-red-500">*</span>
</label>
<input
type="number"
id="qty"
name="qty"
placeholder="Enter quantity"
value={assetData.qty}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="used" className="block text-sm font-medium text-gray-700">Used:</label>
<input
type="number"
id="used"
name="used"
placeholder="Enter used quantity"
value={assetData.used}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
<small className="text-gray-500">This field is optional.</small>
</div>
<div className="mb-4">
<label htmlFor="remaining" className="block text-sm font-medium text-gray-700">Remaining:</label>
<input
type="number"
id="remaining"
name="remaining"
placeholder="Enter remaining quantity"
value={assetData.remaining}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
<small className="text-gray-500">This field is optional.</small>
</div>
<div className="mb-4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost:</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
placeholder="Enter renewal cost (optional)"
value={assetData.renewalCost}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
<small className="text-gray-500">This field is optional.</small>
</div>
<div className="mb-4">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date:<span className="text-red-500">*</span></label>
<input
type="date"
id="startDate"
name="startDate"
value={assetData.startDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date:<span className="text-red-500">*</span></label>
<input
type="date"
id="endDate"
name="endDate"
value={assetData.endDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person:<span className="text-red-500">*</span></label>
<input
type="text"
id="contactPerson"
name="contactPerson"
placeholder="Enter contact person"
value={assetData.contactPerson}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID:<span className="text-red-500">*</span></label>
<input
type="email"
id="emailId"
name="emailId"
placeholder="Enter email ID"
value={assetData.emailId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number:<span className="text-red-500">*</span></label>
<input
type="text"
id="contactNumber"
name="contactNumber"
placeholder="Enter contact number"
value={assetData.contactNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="flex justify-center">
<button
type="submit"
className="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
<button
type="button"
className="bg-gray-400 text-gray-700 py-2 px-4 ml-2 rounded-md hover:bg-gray-300 focus:outline-none focus:bg-gray-300"
onClick={() => {
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
}}
>
Cancel
</button>
</div>
</form>
</div>
);
};
export default AddNewAsset; make those chnges in this edit asse page
Show full message · 308 lines Certainly! Below is the updated component for AddNewAsset, where I've made the following changes based on your previous messages:
Removed the used and remaining fields , as per your instructions.
Added asterisks to required fields to indicate which fields are necessary.
Formatted the UX to ensure clarity for required fields.
Modified the redirection to /asset-inventory on cancellation or submission .
Here is the updated AddNewAsset component:
Updated AddNewAsset.js
javascript Copy code
import React, { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
const AddNewAsset = () => {
const { id } = useParams(); // Get asset ID from URL for editing
const [assetData, setAssetData] = useState({
assetId: '', // Required
vendor: '', // Required
applicationName: '', // Required
qty: '', // Required
renewalCost: '', // Optional
startDate: '', // Required
endDate: '', // Required
contactPerson: '', // Required
emailId: '', // Required
contactNumber: '', // Required
serialNumber: '' // Required but unique
});
// Fetch asset details if an ID is provided (for editing)
useEffect(() => {
if (id) {
const fetchAssetDetails = async () => {
try {
const response = await fetch(`/api/assets/${id}`);
const data = await response.json();
setAssetData(data);
} catch (error) {
console.error('Error fetching asset details:', error);
}
};
fetchAssetDetails();
}
}, [id]);
const handleInputChange = (event) => {
const { name, value } = event.target;
setAssetData({
...assetData,
[name]: value,
});
};
const handleSubmit = async (event) => {
event.preventDefault();
try {
const method = id ? 'PUT' : 'POST'; // Determine method based on whether we are editing or adding
const response = await fetch(`/api/assets${id ? '/' + id : ''}`, {
method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(assetData),
});
if (!response.ok) {
throw new Error('Failed to add/edit asset');
}
// Clear form fields on successful submission (optional)
setAssetData({
assetId: '',
vendor: '',
applicationName: '',
qty: '',
renewalCost: '',
startDate: '',
endDate: '',
contactPerson: '',
emailId: '',
contactNumber: '',
serialNumber: ''
});
// Redirect or show success message
alert(id ? 'Asset edited successfully' : 'Asset added successfully');
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
} catch (error) {
console.error('Error adding/editing asset:', error);
alert('Failed to add/edit asset. Please try again.');
}
};
return (
<div className="container mx-auto p-4 w-1/2 mt-10 p-6 bg-gray-100 rounded-lg">
<div className="text-2xl text-center font-bold mb-4 text-black">
{id ? 'Edit Asset' : 'Add New Asset'}
</div>
<form onSubmit={handleSubmit} className="max-w-md mx-auto" id="assetForm">
<div className="mb-4">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">
Serial Number: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
placeholder="Enter serial number"
value={assetData.serialNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required and must be unique.</small>
</div>
<div className="mb-4">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">
Asset ID: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="assetId"
name="assetId"
placeholder="Enter asset ID"
value={assetData.assetId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
readOnly={id ? true : false} // Make ID field read-only if editing
/>
<small className="text-gray-500">This field is required and must be unique.</small>
</div>
<div className="mb-4">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">
Vendor: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="vendor"
name="vendor"
placeholder="Enter vendor name"
value={assetData.vendor}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">
Application Name: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="applicationName"
name="applicationName"
placeholder="Enter application name"
value={assetData.applicationName}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">
Quantity: <span className="text-red-500">*</span>
</label>
<input
type="number"
id="qty"
name="qty"
placeholder="Enter quantity"
value={assetData.qty}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost:</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
placeholder="Enter renewal cost (optional)"
value={assetData.renewalCost}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
<small className="text-gray-500">This field is optional.</small>
</div>
<div className="mb-4">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="startDate"
name="startDate"
value={assetData.startDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="endDate"
name="endDate"
value={assetData.endDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactPerson"
name="contactPerson"
placeholder="Enter contact person"
value={assetData.contactPerson}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID: <span className="text-red-500">*</span></label>
<input
type="email"
id="emailId"
name="emailId"
placeholder="Enter email ID"
value={assetData.emailId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactNumber"
name="contactNumber"
placeholder="Enter contact number"
value={assetData.contactNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="flex justify-center">
<button
type="submit"
className="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
<button
type="button"
className="bg-gray-400 text-gray-700 py-2 px-4 ml-2 rounded-md hover:bg-gray-300 focus:outline-none focus:bg-gray-300"
onClick={() => {
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
}}
>
Cancel
</button>
</div>
</form>
</div>
);
};
export default AddNewAsset;
Summary of Changes Made
Removed used and remaining Fields : The fields for the quantity used and remaining have been removed from both the form and state.
Asterisks Added : Asterisks (*) were added next to required fields, making it clear which fields are mandatory.
Form Structure : Maintained a user-friendly and structured approach to the form layout.
Proper State Management : Ensured that the state management for form inputs remains functional.
You can now use this updated AddNewAsset component in your application to seamlessly add or edit assets without the removed fields. If you need any further modifications or assistance, feel free to ask!
Show full message · 300 lines import React, { useState } from 'react';
const AddNewAsset = () => {
const [assetData, setAssetData] = useState({
serialNumber: '', // Optional but unique
assetId: '', // Required
vendor: '', // Required
applicationName: '', // Required
qty: '', // Required
renewalCost: '', // Optional
startDate: '', // Required
endDate: '', // Required
contactPerson: '', // Required
emailId: '', // Required
contactNumber: '' // Required
});
code Copy code
const handleInputChange = (event) => {
const { name, value } = event.target;
setAssetData({
...assetData,
[name]: value,
});
};
const handleSubmit = async (event) => {
event.preventDefault();
try {
const response = await fetch('/api/assets', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(assetData),
});
if (!response.ok) {
throw new Error('Failed to add asset');
}
// Clear form fields on successful submission (optional)
setAssetData({
serialNumber: '',
assetId: '',
vendor: '',
applicationName: '',
qty: '',
renewalCost: '',
startDate: '',
endDate: '',
contactPerson: '',
emailId: '',
contactNumber: '',
});
// Redirect or show success message
alert('Asset added successfully');
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
} catch (error) {
console.error('Error adding asset:', error);
alert('Failed to add asset. Please try again.');
}
};
return (
<div className="container mx-auto p-4 w-1/2 mt-10 p-6 bg-gray-100 rounded-lg">
<div className="text-2xl text-center font-bold mb-4 text-black">Add New License</div>
<form onSubmit={handleSubmit} className="max-w-md mx-auto" id="assetForm">
<div className="mb-4">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">Line Item: <span className="text-red-500">*</span></label>
<input
type="text"
id="assetId"
name="assetId"
placeholder="Enter asset ID"
value={assetData.assetId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">Application Name: <span className="text-red-500">*</span></label>
<input
type="text"
id="applicationName"
name="applicationName"
placeholder="Enter application name"
value={assetData.applicationName}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">Quantity: <span className="text-red-500">*</span></label>
<input
type="number"
id="qty"
name="qty"
placeholder="Enter quantity"
value={assetData.qty}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="startDate"
name="startDate"
value={assetData.startDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="endDate"
name="endDate"
value={assetData.endDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">Serial Number:</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
placeholder="Enter serial number (optional, must be unique)"
value={assetData.serialNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost:</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
placeholder="Enter renewal cost (optional)"
value={assetData.renewalCost}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">Vendor Name: <span className="text-red-500">*</span></label>
<input
type="text"
id="vendor"
name="vendor"
placeholder="Enter vendor name"
value={assetData.vendor}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactPerson"
name="contactPerson"
placeholder="Enter contact person"
value={assetData.contactPerson}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID: <span className="text-red-500">*</span></label>
<input
type="email"
id="emailId"
name="emailId"
placeholder="Enter email ID"
value={assetData.emailId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactNumber"
name="contactNumber"
placeholder="Enter contact number"
value={assetData.contactNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="flex justify-center">
<button
type="submit"
className="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
<button
type="button"
className="bg-gray-400 text-gray-700 py-2 px-4 ml-2 rounded-md hover:bg-gray-300 focus:outline-none focus:bg-gray-300"
onClick={() => {
window.location.href = '/asset-inventory';
}}
>
Cancel
</button>
</div>
</form>
</div>
);
};
export default AddNewAsset; in this is this possible that the line item first should be 001 automatic for first for second 002 and for three 003 ..so on .....like it is autofilled when the user enetr
Show full message · 232 lines Yes, it is definitely possible to automatically generate a sequential line item number (assetId) like 001, 002, 003, etc., based on the number of assets already present in your inventory. However, you would need to ensure that this logic aligns with your backend system and how it handles asset IDs.
Option 1: Fetch Existing Assets
If you have an endpoint that fetches the existing assets, you can retrieve them initially, find the highest assetId, and increment it for the new asset.
Example Code Implementation
Assuming your backend returns a list of existing assets, here's how to implement this:
Fetch existing assets on component mount.
Determine the next sequential assetId.
Here's how you can modify your AddNewAsset component:
javascript Copy code
import React, { useEffect, useState } from 'react';
const AddNewAsset = () => {
const [assetData, setAssetData] = useState({
serialNumber: '', // Optional but unique
assetId: '', // Required
vendor: '', // Required
applicationName: '', // Required
qty: '', // Required
renewalCost: '', // Optional
startDate: '', // Required
endDate: '', // Required
contactPerson: '', // Required
emailId: '', // Required
contactNumber: '' // Required
});
// Fetch existing assets to determine the next assetId
useEffect(() => {
const fetchExistingAssets = async () => {
try {
const response = await fetch('/api/assets');
const existingAssets = await response.json();
// Find the highest assetId and increment it
const highestAssetId = existingAssets.reduce((maxId, asset) => {
const assetNumber = parseInt(asset.assetId, 10);
return assetNumber > maxId ? assetNumber : maxId;
}, 0);
// Set the next assetId (001, 002, etc.)
const nextAssetId = String(highestAssetId + 1).padStart(3, '0'); // Ensuring it is in the format 001, 002, etc.
setAssetData((prevData) => ({ ...prevData, assetId: nextAssetId }));
} catch (error) {
console.error('Error fetching existing assets:', error);
}
};
fetchExistingAssets();
}, []);
const handleInputChange = (event) => {
const { name, value } = event.target;
setAssetData({
...assetData,
[name]: value,
});
};
const handleSubmit = async (event) => {
event.preventDefault();
try {
const response = await fetch('/api/assets', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(assetData),
});
if (!response.ok) {
throw new Error('Failed to add asset');
}
// Clear form fields on successful submission (optional)
setAssetData({
serialNumber: '',
assetId: '',
vendor: '',
applicationName: '',
qty: '',
renewalCost: '',
startDate: '',
endDate: '',
contactPerson: '',
emailId: '',
contactNumber: '',
});
// Redirect or show success message
alert('Asset added successfully');
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
} catch (error) {
console.error('Error adding asset:', error);
alert('Failed to add asset. Please try again.');
}
};
return (
<div className="container mx-auto p-4 w-1/2 mt-10 p-6 bg-gray-100 rounded-lg">
<div className="text-2xl text-center font-bold mb-4 text-black">Add New License</div>
<form onSubmit={handleSubmit} className="max-w-md mx-auto" id="assetForm">
<div className="mb-4">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">Line Item: <span className="text-red-500">*</span></label>
<input
type="text"
id="assetId"
name="assetId"
placeholder="Enter asset ID"
value={assetData.assetId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
readOnly // Make it read-only since it's auto-generated
/>
</div>
<div className="mb-4">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">Application Name: <span className="text-red-500">*</span></label>
<input
type="text"
id="applicationName"
name="applicationName"
placeholder="Enter application name"
value={assetData.applicationName}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">Quantity: <span className="text-red-500">*</span></label>
<input
type="number"
id="qty"
name="qty"
placeholder="Enter quantity"
value={assetData.qty}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="startDate"
name="startDate"
value={assetData.startDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="endDate"
name="endDate"
value={assetData.endDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">Serial Number:</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
placeholder="Enter serial number (optional, must be unique)"
value={assetData.serialNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost:</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
placeholder="Enter renewal cost (optional)"
value={assetData.renewalCost}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">Vendor Name: <span className="text-red-500">*</span></label>
<input
type="text"
id="vendor"
name="vendor"
placeholder="Enter vendor name"
value={assetData.vendor}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactPerson"
name="contactPerson"
placeholder="Enter contact person"
value={assetData.contactPerson}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID: <span className="text-red-500">*</span></label>
<input
type="email"
id="emailId"
name="emailId"
placeholder="Enter email ID"
value={assetData.emailId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactNumber"
name="contactNumber"
placeholder="Enter contact number"
value={assetData.contactNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="flex justify-center">
<button
type="submit"
className="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
<button
type="button"
className="bg-gray-400 text-gray-700 py-2 px-4 ml-2 rounded-md hover:bg-gray-300 focus:outline-none focus:bg-gray-300"
onClick={() => {
window.location.href = '/asset-inventory';
}}
>
Cancel
</button>
</div>
</form>
</div>
);
};
export default AddNewAsset;
Key Points
The assetId field is auto-generated. The code fetches existing assets from the API, determines the highest assetId, and increments it by 1, formatting the number to ensure it has three digits.
The assetId input field is made read-only to prevent user modifications since it is managed automatically.
This modification allows new assets to have a sequentially assigned assetId while maintaining the integrity of the required fields.
Remember to adjust your backend to ensure it can handle this logic correctly, especially for scenarios where concurrent accesses might generate duplicate IDs. If you're using a database, consider managing the IDs through auto-increment fields or sequences to prevent collisions.
Show full message · 282 lines import React, { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
const AddNewAsset = () => {
const { id } = useParams(); // Get asset ID from URL for editing
code Copy code
const [assetData, setAssetData] = useState({
assetId: '', // Required
vendor: '', // Required
applicationName: '', // Required
qty: '', // Required
used: '', // Optional
remaining: '', // Optional
renewalCost: '', // Optional
startDate: '', // Required
endDate: '', // Required
contactPerson: '', // Required
emailId: '', // Required
contactNumber: '', // Required
serialNumber: '' // Required but unique
});
// Fetch asset details if an ID is provided (for editing)
useEffect(() => {
if (id) {
const fetchAssetDetails = async () => {
try {
const response = await fetch(`/api/assets/${id}`);
const data = await response.json();
setAssetData(data);
} catch (error) {
console.error('Error fetching asset details:', error);
}
};
fetchAssetDetails();
}
}, [id]);
const handleInputChange = (event) => {
const { name, value } = event.target;
setAssetData({
...assetData,
[name]: value,
});
};
const handleSubmit = async (event) => {
event.preventDefault();
try {
const method = id ? 'PUT' : 'POST'; // Determine method based on whether we are editing or adding
const response = await fetch(`/api/assets${id ? '/' + id : ''}`, {
method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(assetData),
});
if (!response.ok) {
throw new Error('Failed to add/edit asset');
}
// Clear form fields on successful submission (optional)
setAssetData({
assetId: '',
vendor: '',
applicationName: '',
qty: '',
used: '',
remaining: '',
renewalCost: '',
startDate: '',
endDate: '',
contactPerson: '',
emailId: '',
contactNumber: '',
serialNumber: ''
});
// Redirect or show success message
alert(id ? 'Asset edited successfully' : 'Asset added successfully');
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
} catch (error) {
console.error('Error adding/editing asset:', error);
alert('Failed to add/edit asset. Please try again.');
}
};
return (
<div className="container mx-auto p-4 w-1/2 mt-10 p-6 bg-gray-100 rounded-lg">
<div className="text-2xl text-center font-bold mb-4 text-black">
{id ? 'Edit Asset' : 'Add New Asset'}
</div>
<form onSubmit={handleSubmit} className="max-w-md mx-auto" id="assetForm">
<div className="mb-4">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">
Serial Number: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
placeholder="Enter serial number"
value={assetData.serialNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required and must be unique.</small>
</div>
<div className="mb-4">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">
Asset ID: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="assetId"
name="assetId"
placeholder="Enter asset ID"
value={assetData.assetId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
readOnly={id ? true : false} // Make ID field read-only if editing
/>
<small className="text-gray-500">This field is required and must be unique.</small>
</div>
<div className="mb-4">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">
Vendor: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="vendor"
name="vendor"
placeholder="Enter vendor name"
value={assetData.vendor}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">
Application Name: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="applicationName"
name="applicationName"
placeholder="Enter application name"
value={assetData.applicationName}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">
Quantity: <span className="text-red-500">*</span>
</label>
<input
type="number"
id="qty"
name="qty"
placeholder="Enter quantity"
value={assetData.qty}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="used" className="block text-sm font-medium text-gray-700">Used:</label>
<input
type="number"
id="used"
name="used"
placeholder="Enter used quantity"
value={assetData.used}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
<small className="text-gray-500">This field is optional.</small>
</div>
<div className="mb-4">
<label htmlFor="remaining" className="block text-sm font-medium text-gray-700">Remaining:</label>
<input
type="number"
id="remaining"
name="remaining"
placeholder="Enter remaining quantity"
value={assetData.remaining}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
<small className="text-gray-500">This field is optional.</small>
</div>
<div className="mb-4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost:</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
placeholder="Enter renewal cost (optional)"
value={assetData.renewalCost}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
<small className="text-gray-500">This field is optional.</small>
</div>
<div className="mb-4">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date:<span className="text-red-500">*</span></label>
<input
type="date"
id="startDate"
name="startDate"
value={assetData.startDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date:<span className="text-red-500">*</span></label>
<input
type="date"
id="endDate"
name="endDate"
value={assetData.endDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person:<span className="text-red-500">*</span></label>
<input
type="text"
id="contactPerson"
name="contactPerson"
placeholder="Enter contact person"
value={assetData.contactPerson}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID:<span className="text-red-500">*</span></label>
<input
type="email"
id="emailId"
name="emailId"
placeholder="Enter email ID"
value={assetData.emailId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number:<span className="text-red-500">*</span></label>
<input
type="text"
id="contactNumber"
name="contactNumber"
placeholder="Enter contact number"
value={assetData.contactNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="flex justify-center">
<button
type="submit"
className="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
<button
type="button"
className="bg-gray-400 text-gray-700 py-2 px-4 ml-2 rounded-md hover:bg-gray-300 focus:outline-none focus:bg-gray-300"
onClick={() => {
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
}}
>
Cancel
</button>
</div>
</form>
</div>
);
};
export default AddNewAsset; in this also keep the syntax or format like import React, { useEffect, useState } from 'react';
const AddNewAsset = () => {
const [assetData, setAssetData] = useState({
serialNumber: '', // Optional but unique
assetId: '', // Required
vendor: '', // Required
applicationName: '', // Required
qty: '', // Required
renewalCost: '', // Optional
startDate: '', // Required
endDate: '', // Required
contactPerson: '', // Required
emailId: '', // Required
contactNumber: '' // Required
});
};
export default AddNewAsset;
Show full message · 564 lines Here's the complete AddNewAsset component, utilizing the desired syntax and format. The following code integrates the auto-increment logic for the assetId, removes unused fields (used and remaining), and maintains consistency in structure.
Updated AddNewAsset.js
javascript Copy code
import React, { useEffect, useState } from 'react';
const AddNewAsset = () => {
const [assetData, setAssetData] = useState({
assetId: '', // Required
vendor: '', // Required
applicationName: '', // Required
qty: '', // Required
renewalCost: '', // Optional
startDate: '', // Required
endDate: '', // Required
contactPerson: '', // Required
emailId: '', // Required
contactNumber: '', // Required
serialNumber: '' // Required but unique
});
// Fetch existing assets to determine the next assetId
useEffect(() => {
const fetchExistingAssets = async () => {
try {
const response = await fetch('/api/assets');
const existingAssets = await response.json();
// Find the highest assetId and increment it
const highestAssetId = existingAssets.reduce((maxId, asset) => {
const assetNumber = parseInt(asset.assetId, 10);
return assetNumber > maxId ? assetNumber : maxId;
}, 0);
// Set the next assetId (001, 002, etc.)
const nextAssetId = String(highestAssetId + 1).padStart(3, '0'); // Ensuring it is in the format 001, 002, etc.
setAssetData((prevData) => ({ ...prevData, assetId: nextAssetId }));
} catch (error) {
console.error('Error fetching existing assets:', error);
}
};
fetchExistingAssets();
}, []);
const handleInputChange = (event) => {
const { name, value } = event.target;
setAssetData({
...assetData,
[name]: value,
});
};
const handleSubmit = async (event) => {
event.preventDefault();
try {
const response = await fetch('/api/assets', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(assetData),
});
if (!response.ok) {
throw new Error('Failed to add asset');
}
// Clear form fields on successful submission
setAssetData({
assetId: '',
vendor: '',
applicationName: '',
qty: '',
renewalCost: '',
startDate: '',
endDate: '',
contactPerson: '',
emailId: '',
contactNumber: '',
serialNumber: ''
});
// Redirect or show success message
alert('Asset added successfully');
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
} catch (error) {
console.error('Error adding asset:', error);
alert('Failed to add asset. Please try again.');
}
};
return (
<div className="container mx-auto p-4 w-1/2 mt-10 p-6 bg-gray-100 rounded-lg">
<div className="text-2xl text-center font-bold mb-4 text-black">Add New License</div>
<form onSubmit={handleSubmit} className="max-w-md mx-auto" id="assetForm">
<div className="mb-4">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">Line Item: <span className="text-red-500">*</span></label>
<input
type="text"
id="assetId"
name="assetId"
placeholder="Enter asset ID"
value={assetData.assetId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
readOnly // Make it read-only since it's auto-generated
/>
<small className="text-gray-500">This field is required and must be unique.</small>
</div>
<div className="mb-4">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">Application Name: <span className="text-red-500">*</span></label>
<input
type="text"
id="applicationName"
name="applicationName"
placeholder="Enter application name"
value={assetData.applicationName}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">Quantity: <span className="text-red-500">*</span></label>
<input
type="number"
id="qty"
name="qty"
placeholder="Enter quantity"
value={assetData.qty}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="startDate"
name="startDate"
value={assetData.startDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="endDate"
name="endDate"
value={assetData.endDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">Serial Number:</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
placeholder="Enter serial number (optional, must be unique)"
value={assetData.serialNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
<small className="text-gray-500">This field is required and must be unique.</small>
</div>
<div className="mb-4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost:</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
placeholder="Enter renewal cost (optional)"
value={assetData.renewalCost}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
<small className="text-gray-500">This field is optional.</small>
</div>
<div className="mb-4">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">Vendor Name: <span className="text-red-500">*</span></label>
<input
type="text"
id="vendor"
name="vendor"
placeholder="Enter vendor name"
value={assetData.vendor}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactPerson"
name="contactPerson"
placeholder="Enter contact person"
value={assetData.contactPerson}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID: <span className="text-red-500">*</span></label>
<input
type="email"
id="emailId"
name="emailId"
placeholder="Enter email ID"
value={assetData.emailId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="mb-4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactNumber"
name="contactNumber"
placeholder="Enter contact number"
value={assetData.contactNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
<small className="text-gray-500">This field is required.</small>
</div>
<div className="flex justify-center">
<button
type="submit"
className="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
<button
type="button"
className="bg-gray-400 text-gray-700 py-2 px-4 ml-2 rounded-md hover:bg-gray-300 focus:outline-none focus:bg-gray-300"
onClick={() => {
window.location.href = '/asset-inventory';
}}
>
Cancel
</button>
</div>
</form>
</div>
);
};
export default AddNewAsset;
Key Changes and Additions
Auto-increment assetId: The logic to calculate the next asset ID based on existing assets has been added.
Removed used and remaining fields: These fields were removed as requested.
Consistent formatting: The coding style and structure effectively match your requirements, including comments and code organization.
This setup allows the assetId to be dynamically generated based on existing assets while retaining a clear and user-friendly form for adding new assets. If further customization or additional features are needed, feel free to ask!
Show full message · 281 lines Copy first response
Not yet included in a published release.
// Initial form data state
const [formData, setFormData] = useState({
assetId: assetId || '',
vendor: '',
applicationName: '',
qty: '',
used: '',
remaining: '',
startDate: '',
endDate: '',
renewalCost: '',
contactPerson: '',
emailId: '',
contactNumber: '',
serialNumber: ''
});
useEffect(() => {
if (assetId) {
// Fetch existing asset data for editing
const fetchAssetData = async () => {
try {
const response = await axios.get(`/api/assets/${assetId}`);
setFormData(response.data);
} catch (error) {
console.error('Error fetching asset data:', error);
}
};
fetchAssetData();
}
}, [assetId]);
const handleChange = (e) => {
const { name, value } = e.target;
setFormData({
...formData,
[name]: value,
});
};
const handleSubmit = async (e) => {
e.preventDefault();
try {
// Determine whether to add a new asset or update an existing one
if (assetId) {
await axios.put(`/api/assets/${assetId}`, formData);
} else {
await axios.post('/api/assets', formData);
}
// Navigate back to the dashboard
navigate('/technician-dashboard');
} catch (error) {
console.error('Error saving asset:', error);
}
};
return (
<div className="bg-gray-100 min-h-screen">
{/* Navbar */}
<nav className="flex justify-between items-center p-4 bg-white shadow-md">
<div className="space-x-4">
<a href="/technician-dashboard" className="text-blue-600 hover:text-gray-900">Home</a>
</div>
<div className="space-x-4">
<a href="/" className="text-blue-600 hover:text-gray-900">Logout</a>
</div>
</nav>
{/* Form Content */}
<div className="container mx-auto p-6">
<h1 className="text-2xl font-bold text-gray-800 mb-6">Add License Details</h1>
<form onSubmit={handleSubmit} className="space-y-4">
{/* Asset ID Field */}
<div className="flex space-x-4">
<div className="w-1/2">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">Asset ID</label>
<input
type="text"
id="assetId"
name="assetId"
value={formData.assetId}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
readOnly={!!assetId} // Make read-only if editing
/>
</div>
</div>
{/* Vendor and Application Name */}
<div className="flex space-x-4">
<div className="w-1/2">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">Vendor</label>
<input
type="text"
id="vendor"
name="vendor"
value={formData.vendor}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="w-1/2">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">Application Name</label>
<input
type="text"
id="applicationName"
name="applicationName"
value={formData.applicationName}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
</div>
{/* Serial Number and Quantity */}
<div className="flex space-x-4">
<div className="w-1/2">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">Serial Number</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
value={formData.serialNumber}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
/>
</div>
<div className="w-1/2">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">Quantity</label>
<input
type="number"
id="qty"
name="qty"
value={formData.qty}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
</div>
{/* Used, Remaining, Start Date and End Date */}
<div className="flex space-x-4">
<div className="w-1/2">
<label htmlFor="used" className="block text-sm font-medium text-gray-700">Used</label>
<input
type="number"
id="used"
name="used"
value={formData.used}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
/>
</div>
<div className="w-1/2">
<label htmlFor="remaining" className="block text-sm font-medium text-gray-700">Remaining</label>
<input
type="number"
id="remaining"
name="remaining"
value={formData.remaining}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
/>
</div>
</div>
<div className="flex space-x-4">
<div className="w-1/2">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date</label>
<input
type="date"
id="startDate"
name="startDate"
value={formData.startDate}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="w-1/2">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date</label>
<input
type="date"
id="endDate"
name="endDate"
value={formData.endDate}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
</div>
{/* Renewal Cost, Contact Person, Email ID, and Contact Number */}
<div className="flex space-x-4">
<div className="w-1/4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
value={formData.renewalCost}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
/>
</div>
<div className="w-1/4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person</label>
<input
type="text"
id="contactPerson"
name="contactPerson"
value={formData.contactPerson}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="w-1/4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID</label>
<input
type="email"
id="emailId"
name="emailId"
value={formData.emailId}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="w-1/4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number</label>
<input
type="text"
id="contactNumber"
name="contactNumber"
value={formData.contactNumber}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
</div>
{/* Submit Button */}
<div className="flex justify-end mt-4">
<button
type="submit"
className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
</div>
</form>
</div>
</div>
);
useEffect(() => {
fetchData();
}, []);
useEffect(() => {
// Filter assets whenever searchQuery changes
const filtered = assets.filter(asset => {
return (
(asset.assetId && asset.assetId.toString().toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.vendor && asset.vendor.toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.applicationName && asset.applicationName.toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.serialNumber && asset.serialNumber.toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.contactPerson && asset.contactPerson.toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.location && asset.location.toLowerCase().includes(searchQuery.toLowerCase()))
);
});
setFilteredAssets(filtered);
}, [searchQuery, assets]);
const fetchData = async () => {
try {
const response = await axios.get('/api/assets');
setAssets(response.data);
setFilteredAssets(response.data); // Set initial filtered assets
} catch (error) {
console.error('Error fetching assets:', error);
}
};
const handleEditAsset = (assetId) => {
window.location.href = `/edit-asset/${assetId}`;
};
const handleDeleteAsset = async (assetId) => {
if (window.confirm('Are you sure you want to delete this asset?')) {
try {
await axios.delete(`/api/assets/${assetId}`);
setAssets(assets.filter(asset => asset.assetId !== assetId));
setFilteredAssets(filteredAssets.filter(asset => asset.assetId !== assetId));
alert('Asset deleted successfully!');
} catch (error) {
console.error('Error deleting asset:', error);
}
}
};
return (
<div className="bg-gray-100">
<nav className="flex justify-between items-center p-4 bg-white shadow-md">
<div className="space-x-4">
<a href="/admin-dashboard" className="text-blue-600 hover:text-gray-900">Home</a>
</div>
<div className="space-x-4">
<a href="/" className="text-blue-600 hover:text-gray-900">Logout</a>
</div>
</nav>
<div className="container max-w-6xl mt-30 p-6 bg-gray-100 rounded-lg ml-40">
<div className="text-2xl font-bold text-center mb-6 text-black">License Inventory Management</div>
<div className="text-right mb-6">
<Link to="/add-newAsset">
<button className="bg-blue-400 text-white rounded px-4 py-2 hover:bg-blue-600">Add New License</button>
</Link>
<input
type="text"
placeholder="Search..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="bg-gray-200 text-gray-800 rounded px-4 py-2 ml-2"
/>
</div>
<div className="overflow-x-auto">
<table className="min-w-full bg-white rounded-lg overflow-hidden shadow-md">
<thead>
<tr className="bg-gray-100 text-left">
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Line Item</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Application Name</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Quantity</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Start Date</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">End Date</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Serial Number</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Renewal Cost</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Vendor</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Contact Person</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Email</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Contact Number</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Actions</th>
</tr>
</thead>
<tbody>
{filteredAssets.map(asset => (
<tr key={asset.assetId} className="border-b border-gray-200">
<td className="px-6 py-4">{asset.assetId}</td>
<td className="px-6 py-4">{asset.vendor}</td>
<td className="px-6 py-4">{asset.applicationName}</td>
<td className="px-6 py-4">{asset.qty}</td>
<td className="px-6 py-4">{asset.used}</td>
<td className="px-6 py-4">{asset.remaining}</td>
<td className="px-6 py-4">{new Date(asset.startDate).toLocaleDateString()}</td>
<td className="px-6 py-4">{new Date(asset.endDate).toLocaleDateString()}</td>
<td className="px-6 py-4">{asset.contactPerson}</td>
<td className="px-6 py-4">{asset.serialNumber}</td> {/* Display the serial number */}
<td className="px-6 py-4">
<div className='flex'>
<button
className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
onClick={() => handleEditAsset(asset.assetId)}>Edit</button>
<button
className="bg-red-500 text-white px-4 py-2 rounded-md hover:bg-red-600 focus:outline-none focus:bg-red-600 ml-2"
onClick={() => handleDeleteAsset(asset.assetId)}>Delete</button>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
import React, { useState, useEffect } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import axios from 'axios';
const AddAssetForm = () => {
const { assetId } = useParams();
const navigate = useNavigate();
// Initial form data state
const [formData, setFormData] = useState({
assetId: assetId || '', // Required
vendor: '', // Required
applicationName: '', // Required
qty: '', // Required
startDate: '', // Required
endDate: '', // Required
renewalCost: '', // Optional
contactPerson: '', // Required
emailId: '', // Required
contactNumber: '', // Required
serialNumber: '' // Optional
});
useEffect(() => {
if (assetId) {
// Fetch existing asset data for editing
const fetchAssetData = async () => {
try {
const response = await axios.get(`/api/assets/${assetId}`);
setFormData(response.data);
} catch (error) {
console.error('Error fetching asset data:', error);
}
};
fetchAssetData();
}
}, [assetId]);
const handleChange = (e) => {
const { name, value } = e.target;
setFormData({
...formData,
[name]: value,
});
};
const handleSubmit = async (e) => {
e.preventDefault();
try {
// Determine whether to add a new asset or update an existing one
if (assetId) {
await axios.put(`/api/assets/${assetId}`, formData);
} else {
await axios.post('/api/assets', formData);
}
// Navigate back to the dashboard
navigate('/asset-inventory');
} catch (error) {
console.error('Error saving asset:', error);
}
};
return (
<div className="bg-gray-100 min-h-screen">
{/* Navbar */}
<nav className="flex justify-between items-center p-4 bg-white shadow-md">
<div className="space-x-4">
<a href="/technician-dashboard" className="text-blue-600 hover:text-gray-900">Home</a>
</div>
<div className="space-x-4">
<a href="/" className="text-blue-600 hover:text-gray-900">Logout</a>
</div>
</nav>
{/* Form Content */}
<div className="container mx-auto p-6">
<h1 className="text-2xl font-bold text-gray-800 mb-6">Add License Details</h1>
<form onSubmit={handleSubmit} className="space-y-4">
{/* Asset ID Field */}
<div className="flex space-x-4">
<div className="w-1/2">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">Asset ID <span className="text-red-500">*</span></label>
<input
type="text"
id="assetId"
name="assetId"
value={formData.assetId}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
readOnly={!!assetId} // Make read-only if editing
/>
</div>
</div>
{/* Vendor and Application Name */}
<div className="flex space-x-4">
<div className="w-1/2">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">Vendor <span className="text-red-500">*</span></label>
<input
type="text"
id="vendor"
name="vendor"
value={formData.vendor}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="w-1/2">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">Application Name <span className="text-red-500">*</span></label>
<input
type="text"
id="applicationName"
name="applicationName"
value={formData.applicationName}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
</div>
{/* Serial Number and Quantity */}
<div className="flex space-x-4">
<div className="w-1/2">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">Serial Number:</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
value={formData.serialNumber}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
/>
</div>
<div className="w-1/2">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">Quantity <span className="text-red-500">*</span></label>
<input
type="number"
id="qty"
name="qty"
value={formData.qty}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
</div>
<div className="flex space-x-4">
<div className="w-1/2">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date <span className="text-red-500">*</span></label>
<input
type="date"
id="startDate"
name="startDate"
value={formData.startDate}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="w-1/2">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date <span className="text-red-500">*</span></label>
<input
type="date"
id="endDate"
name="endDate"
value={formData.endDate}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
</div>
{/* Renewal Cost, Contact Person, Email ID, and Contact Number */}
<div className="flex space-x-4">
<div className="w-1/4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost:</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
value={formData.renewalCost}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
/>
</div>
<div className="w-1/4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person <span className="text-red-500">*</span></label>
<input
type="text"
id="contactPerson"
name="contactPerson"
value={formData.contactPerson}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="w-1/4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID <span className="text-red-500">*</span></label>
<input
type="email"
id="emailId"
name="emailId"
value={formData.emailId}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="w-1/4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number <span className="text-red-500">*</span></label>
<input
type="text"
id="contactNumber"
name="contactNumber"
value={formData.contactNumber}
onChange={handleChange}
className="border border-gray-300 rounded-md px-3 py-2 w-full focus:outline-none focus:border-blue-500"
required
/>
</div>
</div>
{/* Submit Button */}
<div className="flex justify-end mt-4">
<button
type="submit"
className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
</div>
</form>
</div>
</div>
);
};
export default AddAssetForm;
import React, { useEffect, useState } from 'react';
import axios from 'axios';
import { Link } from 'react-router-dom';
const AssetInventory = () => {
const [assets, setAssets] = useState([]);
const [searchQuery, setSearchQuery] = useState('');
const [filteredAssets, setFilteredAssets] = useState([]);
useEffect(() => {
fetchData();
}, []);
useEffect(() => {
// Filter assets whenever searchQuery changes
const filtered = assets.filter(asset => {
return (
(asset.assetId && asset.assetId.toString().toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.vendor && asset.vendor.toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.applicationName && asset.applicationName.toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.serialNumber && asset.serialNumber.toLowerCase().includes(searchQuery.toLowerCase())) ||
(asset.contactPerson && asset.contactPerson.toLowerCase().includes(searchQuery.toLowerCase()))
);
});
setFilteredAssets(filtered);
}, [searchQuery, assets]);
const fetchData = async () => {
try {
const response = await axios.get('/api/assets');
setAssets(response.data);
setFilteredAssets(response.data); // Set initial filtered assets
} catch (error) {
console.error('Error fetching assets:', error);
}
};
const handleEditAsset = (assetId) => {
window.location.href = `/edit-asset/${assetId}`;
};
const handleDeleteAsset = async (assetId) => {
if (window.confirm('Are you sure you want to delete this asset?')) {
try {
await axios.delete(`/api/assets/${assetId}`);
setAssets(assets.filter(asset => asset.assetId !== assetId));
setFilteredAssets(filteredAssets.filter(asset => asset.assetId !== assetId));
alert('Asset deleted successfully!');
} catch (error) {
console.error('Error deleting asset:', error);
}
}
};
return (
<div className="bg-gray-100">
<nav className="flex justify-between items-center p-4 bg-white shadow-md">
<div className="space-x-4">
<a href="/admin-dashboard" className="text-blue-600 hover:text-gray-900">Home</a>
</div>
<div className="space-x-4">
<a href="/" className="text-blue-600 hover:text-gray-900">Logout</a>
</div>
</nav>
<div className="container max-w-6xl mt-30 p-6 bg-gray-100 rounded-lg ml-40">
<div className="text-2xl font-bold text-center mb-6 text-black">License Inventory Management</div>
<div className="text-right mb-6">
<Link to="/add-newAsset">
<button className="bg-blue-400 text-white rounded px-4 py-2 hover:bg-blue-600">Add New License</button>
</Link>
<input
type="text"
placeholder="Search..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="bg-gray-200 text-gray-800 rounded px-4 py-2 ml-2"
/>
</div>
<div className="overflow-x-auto">
<table className="min-w-full bg-white rounded-lg overflow-hidden shadow-md">
<thead>
<tr className="bg-gray-100 text-left">
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Line Item</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Application Name</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Quantity</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Start Date</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">End Date</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Serial Number</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Renewal Cost</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Vendor</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Contact Person</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Email</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Contact Number</th>
<th className="px-6 py-3 text-xs font-medium text-black uppercase tracking-wider">Actions</th>
</tr>
</thead>
<tbody>
{filteredAssets.map(asset => (
<tr key={asset.assetId} className="border-b border-gray-200">
<td className="px-6 py-4">{asset.assetId}</td>
<td className="px-6 py-4">{asset.applicationName}</td>
<td className="px-6 py-4">{asset.qty}</td>
<td className="px-6 py-4">{new Date(asset.startDate).toLocaleDateString()}</td>
<td className="px-6 py-4">{new Date(asset.endDate).toLocaleDateString()}</td>
<td className="px-6 py-4">{asset.serialNumber}</td>
<td className="px-6 py-4">{asset.renewalCost}</td>
<td className="px-6 py-4">{asset.vendor}</td>
<td className="px-6 py-4">{asset.contactPerson}</td>
<td className="px-6 py-4">{asset.emailId}</td>
<td className="px-6 py-4">{asset.contactNumber}</td>
<td className="px-6 py-4">
<div className='flex'>
<button
className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600 focus:outline-none"
onClick={() => handleEditAsset(asset.assetId)}>Edit</button>
<button
className="bg-red-500 text-white px-4 py-2 rounded-md hover:bg-red-600 focus:outline-none ml-2"
onClick={() => handleDeleteAsset(asset.assetId)}>Delete</button>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
};
export default AssetInventory;
// Fetch existing assets to determine the next assetId
useEffect(() => {
const fetchExistingAssets = async () => {
try {
const response = await fetch('/api/assets');
const existingAssets = await response.json();
// Find the highest assetId and increment it
const highestAssetId = existingAssets.reduce((maxId, asset) => {
const assetNumber = parseInt(asset.assetId, 10);
return assetNumber > maxId ? assetNumber : maxId;
}, 0);
// Set the next assetId (001, 002, etc.)
const nextAssetId = String(highestAssetId + 1).padStart(3, '0'); // Ensuring it is in the format 001, 002, etc.
setAssetData((prevData) => ({ ...prevData, assetId: nextAssetId }));
} catch (error) {
console.error('Error fetching existing assets:', error);
}
};
fetchExistingAssets();
}, []);
const handleInputChange = (event) => {
const { name, value } = event.target;
setAssetData({
...assetData,
[name]: value,
});
};
const handleSubmit = async (event) => {
event.preventDefault();
try {
const response = await fetch('/api/assets', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(assetData),
});
if (!response.ok) {
throw new Error('Failed to add asset');
}
// Clear form fields on successful submission (optional)
setAssetData({
serialNumber: '',
assetId: '',
vendor: '',
applicationName: '',
qty: '',
renewalCost: '',
startDate: '',
endDate: '',
contactPerson: '',
emailId: '',
contactNumber: '',
});
// Redirect or show success message
alert('Asset added successfully');
window.location.href = '/asset-inventory'; // Redirect to asset inventory page
} catch (error) {
console.error('Error adding asset:', error);
alert('Failed to add asset. Please try again.');
}
};
return (
<div className="container mx-auto p-4 w-1/2 mt-10 p-6 bg-gray-100 rounded-lg">
<div className="text-2xl text-center font-bold mb-4 text-black">Add New License</div>
<form onSubmit={handleSubmit} className="max-w-md mx-auto" id="assetForm">
<div className="mb-4">
<label htmlFor="assetId" className="block text-sm font-medium text-gray-700">Line Item: <span className="text-red-500">*</span></label>
<input
type="text"
id="assetId"
name="assetId"
placeholder="Enter asset ID"
value={assetData.assetId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
readOnly // Make it read-only since it's auto-generated
/>
</div>
<div className="mb-4">
<label htmlFor="applicationName" className="block text-sm font-medium text-gray-700">Application Name: <span className="text-red-500">*</span></label>
<input
type="text"
id="applicationName"
name="applicationName"
placeholder="Enter application name"
value={assetData.applicationName}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="qty" className="block text-sm font-medium text-gray-700">Quantity: <span className="text-red-500">*</span></label>
<input
type="number"
id="qty"
name="qty"
placeholder="Enter quantity"
value={assetData.qty}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="startDate" className="block text-sm font-medium text-gray-700">Start Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="startDate"
name="startDate"
value={assetData.startDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="endDate" className="block text-sm font-medium text-gray-700">End Date: <span className="text-red-500">*</span></label>
<input
type="date"
id="endDate"
name="endDate"
value={assetData.endDate}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="serialNumber" className="block text-sm font-medium text-gray-700">Serial Number:</label>
<input
type="text"
id="serialNumber"
name="serialNumber"
placeholder="Enter serial number (optional, must be unique)"
value={assetData.serialNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="renewalCost" className="block text-sm font-medium text-gray-700">Renewal Cost:</label>
<input
type="number"
id="renewalCost"
name="renewalCost"
placeholder="Enter renewal cost (optional)"
value={assetData.renewalCost}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
/>
</div>
<div className="mb-4">
<label htmlFor="vendor" className="block text-sm font-medium text-gray-700">Vendor Name: <span className="text-red-500">*</span></label>
<input
type="text"
id="vendor"
name="vendor"
placeholder="Enter vendor name"
value={assetData.vendor}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactPerson" className="block text-sm font-medium text-gray-700">Contact Person: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactPerson"
name="contactPerson"
placeholder="Enter contact person"
value={assetData.contactPerson}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="emailId" className="block text-sm font-medium text-gray-700">Email ID: <span className="text-red-500">*</span></label>
<input
type="email"
id="emailId"
name="emailId"
placeholder="Enter email ID"
value={assetData.emailId}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="mb-4">
<label htmlFor="contactNumber" className="block text-sm font-medium text-gray-700">Contact Number: <span className="text-red-500">*</span></label>
<input
type="text"
id="contactNumber"
name="contactNumber"
placeholder="Enter contact number"
value={assetData.contactNumber}
onChange={handleInputChange}
className="mt-1 block w-full border border-gray-400 rounded-md px-3 py-2 focus:outline-none focus:border-blue-500"
required
/>
</div>
<div className="flex justify-center">
<button
type="submit"
className="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600"
>
Save
</button>
<button
type="button"
className="bg-gray-400 text-gray-700 py-2 px-4 ml-2 rounded-md hover:bg-gray-300 focus:outline-none focus:bg-gray-300"
onClick={() => {
window.location.href = '/asset-inventory';
}}
>
Cancel
</button>
</div>
</form>
</div>
);