nozzles
This commit is contained in:
515
frontend/src/pages/Nozzles/Nozzles.js
Normal file
515
frontend/src/pages/Nozzles/Nozzles.js
Normal file
@@ -0,0 +1,515 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import {
|
||||
PlusIcon,
|
||||
MagnifyingGlassIcon,
|
||||
TrashIcon,
|
||||
PencilIcon,
|
||||
BeakerIcon
|
||||
} from '@heroicons/react/24/outline';
|
||||
import { nozzlesAPI } from '../../services/api';
|
||||
import LoadingSpinner from '../../components/UI/LoadingSpinner';
|
||||
import toast from 'react-hot-toast';
|
||||
|
||||
const Nozzles = () => {
|
||||
const [userNozzles, setUserNozzles] = useState([]);
|
||||
const [nozzleTypes, setNozzleTypes] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [showCreateForm, setShowCreateForm] = useState(false);
|
||||
const [showEditForm, setShowEditForm] = useState(false);
|
||||
const [editingNozzle, setEditingNozzle] = useState(null);
|
||||
const [searchTerm, setSearchTerm] = useState('');
|
||||
const [selectedManufacturer, setSelectedManufacturer] = useState('');
|
||||
const [selectedDropletSize, setSelectedDropletSize] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, []);
|
||||
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const [userNozzlesResponse, nozzleTypesResponse] = await Promise.all([
|
||||
nozzlesAPI.getUserNozzles(),
|
||||
nozzlesAPI.getNozzleTypes()
|
||||
]);
|
||||
|
||||
setUserNozzles(userNozzlesResponse.data.data.userNozzles || []);
|
||||
setNozzleTypes(nozzleTypesResponse.data.data.nozzleTypes || []);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch nozzles:', error);
|
||||
toast.error('Failed to load nozzles');
|
||||
setUserNozzles([]);
|
||||
setNozzleTypes([]);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleCreateNozzle = async (nozzleData) => {
|
||||
try {
|
||||
await nozzlesAPI.create(nozzleData);
|
||||
toast.success('Nozzle added successfully!');
|
||||
setShowCreateForm(false);
|
||||
fetchData();
|
||||
} catch (error) {
|
||||
console.error('Failed to create nozzle:', error);
|
||||
toast.error('Failed to add nozzle');
|
||||
}
|
||||
};
|
||||
|
||||
const handleEditNozzle = (nozzle) => {
|
||||
setEditingNozzle(nozzle);
|
||||
setShowEditForm(true);
|
||||
};
|
||||
|
||||
const handleUpdateNozzle = async (nozzleData) => {
|
||||
try {
|
||||
await nozzlesAPI.update(editingNozzle.id, nozzleData);
|
||||
toast.success('Nozzle updated successfully!');
|
||||
setShowEditForm(false);
|
||||
setEditingNozzle(null);
|
||||
fetchData();
|
||||
} catch (error) {
|
||||
console.error('Failed to update nozzle:', error);
|
||||
toast.error('Failed to update nozzle');
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteNozzle = async (nozzleId) => {
|
||||
if (window.confirm('Are you sure you want to delete this nozzle?')) {
|
||||
try {
|
||||
await nozzlesAPI.delete(nozzleId);
|
||||
toast.success('Nozzle deleted successfully');
|
||||
fetchData();
|
||||
} catch (error) {
|
||||
console.error('Failed to delete nozzle:', error);
|
||||
toast.error('Failed to delete nozzle');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Filter nozzles based on search and filters
|
||||
const filteredNozzles = userNozzles.filter(nozzle => {
|
||||
const matchesSearch = searchTerm === '' ||
|
||||
nozzle.nozzleType?.name?.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
||||
nozzle.customName?.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
||||
nozzle.nozzleType?.manufacturer?.toLowerCase().includes(searchTerm.toLowerCase());
|
||||
|
||||
const matchesManufacturer = selectedManufacturer === '' ||
|
||||
nozzle.nozzleType?.manufacturer === selectedManufacturer;
|
||||
|
||||
const matchesDropletSize = selectedDropletSize === '' ||
|
||||
nozzle.nozzleType?.dropletSize === selectedDropletSize;
|
||||
|
||||
return matchesSearch && matchesManufacturer && matchesDropletSize;
|
||||
});
|
||||
|
||||
// Get unique manufacturers and droplet sizes for filters
|
||||
const manufacturers = [...new Set(nozzleTypes.map(type => type.manufacturer).filter(Boolean))];
|
||||
const dropletSizes = [...new Set(nozzleTypes.map(type => type.dropletSize).filter(Boolean))];
|
||||
|
||||
const NozzleCard = ({ nozzle }) => (
|
||||
<div className="card">
|
||||
<div className="flex justify-between items-start mb-3">
|
||||
<div className="flex items-start gap-3">
|
||||
<div className="p-2 bg-green-100 rounded-lg">
|
||||
<BeakerIcon className="h-5 w-5 text-green-600" />
|
||||
</div>
|
||||
<div>
|
||||
<h3 className="font-semibold text-gray-900">
|
||||
{nozzle.customName || nozzle.nozzleType?.name}
|
||||
</h3>
|
||||
{nozzle.nozzleType?.manufacturer && (
|
||||
<p className="text-sm text-gray-600">{nozzle.nozzleType.manufacturer} {nozzle.nozzleType.model}</p>
|
||||
)}
|
||||
<div className="flex flex-wrap gap-1 mt-1">
|
||||
<span className="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
|
||||
Qty: {nozzle.quantity}
|
||||
</span>
|
||||
<span className={`inline-flex items-center px-2 py-1 rounded-full text-xs font-medium ${getConditionColor(nozzle.condition)}`}>
|
||||
{nozzle.condition}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-1">
|
||||
<button
|
||||
onClick={() => handleEditNozzle(nozzle)}
|
||||
className="p-1 text-gray-400 hover:text-blue-600"
|
||||
title="Edit nozzle"
|
||||
>
|
||||
<PencilIcon className="h-4 w-4" />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleDeleteNozzle(nozzle.id)}
|
||||
className="p-1 text-gray-400 hover:text-red-600"
|
||||
title="Delete nozzle"
|
||||
>
|
||||
<TrashIcon className="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Nozzle Specifications */}
|
||||
<div className="grid grid-cols-2 gap-3 text-sm text-gray-600">
|
||||
{nozzle.nozzleType?.orificeSize && (
|
||||
<div><strong>Orifice:</strong> {nozzle.nozzleType.orificeSize}</div>
|
||||
)}
|
||||
{nozzle.nozzleType?.sprayAngle && (
|
||||
<div><strong>Spray Angle:</strong> {nozzle.nozzleType.sprayAngle}°</div>
|
||||
)}
|
||||
{nozzle.nozzleType?.dropletSize && (
|
||||
<div><strong>Droplet Size:</strong> {nozzle.nozzleType.dropletSize}</div>
|
||||
)}
|
||||
{nozzle.nozzleType?.sprayPattern && (
|
||||
<div><strong>Pattern:</strong> {nozzle.nozzleType.sprayPattern.replace('_', ' ')}</div>
|
||||
)}
|
||||
{nozzle.nozzleType?.flowRateGpm && (
|
||||
<div><strong>Flow Rate:</strong> {nozzle.nozzleType.flowRateGpm} GPM @ rated PSI</div>
|
||||
)}
|
||||
{nozzle.nozzleType?.pressureRangePsi && (
|
||||
<div><strong>Pressure Range:</strong> {nozzle.nozzleType.pressureRangePsi} PSI</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{nozzle.notes && (
|
||||
<p className="text-sm text-gray-600 mt-3">
|
||||
<strong>Notes:</strong> {nozzle.notes}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{nozzle.purchaseDate && (
|
||||
<p className="text-xs text-gray-500 mt-2">
|
||||
Purchased: {new Date(nozzle.purchaseDate).toLocaleDateString()}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
const getConditionColor = (condition) => {
|
||||
const colors = {
|
||||
'excellent': 'bg-green-100 text-green-800',
|
||||
'good': 'bg-blue-100 text-blue-800',
|
||||
'fair': 'bg-yellow-100 text-yellow-800',
|
||||
'poor': 'bg-orange-100 text-orange-800',
|
||||
'needs_replacement': 'bg-red-100 text-red-800',
|
||||
};
|
||||
return colors[condition] || 'bg-gray-100 text-gray-800';
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="flex justify-center items-center h-64">
|
||||
<LoadingSpinner size="lg" />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold text-gray-900">Nozzles</h1>
|
||||
<p className="text-gray-600">Manage your spray nozzle inventory with specifications</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setShowCreateForm(true)}
|
||||
className="btn-primary flex items-center gap-2"
|
||||
>
|
||||
<PlusIcon className="h-5 w-5" />
|
||||
Add Nozzle
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Search and Filters */}
|
||||
<div className="card mb-6">
|
||||
<div className="flex flex-wrap gap-4">
|
||||
{/* Search */}
|
||||
<div className="flex-1 min-w-64">
|
||||
<div className="relative">
|
||||
<MagnifyingGlassIcon className="h-5 w-5 absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400" />
|
||||
<input
|
||||
type="text"
|
||||
className="input pl-10"
|
||||
placeholder="Search nozzles..."
|
||||
value={searchTerm}
|
||||
onChange={(e) => setSearchTerm(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Manufacturer Filter */}
|
||||
<div className="min-w-48">
|
||||
<select
|
||||
className="input"
|
||||
value={selectedManufacturer}
|
||||
onChange={(e) => setSelectedManufacturer(e.target.value)}
|
||||
>
|
||||
<option value="">All Manufacturers</option>
|
||||
{manufacturers.map((manufacturer) => (
|
||||
<option key={manufacturer} value={manufacturer}>
|
||||
{manufacturer}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{/* Droplet Size Filter */}
|
||||
<div className="min-w-48">
|
||||
<select
|
||||
className="input"
|
||||
value={selectedDropletSize}
|
||||
onChange={(e) => setSelectedDropletSize(e.target.value)}
|
||||
>
|
||||
<option value="">All Droplet Sizes</option>
|
||||
{dropletSizes.map((size) => (
|
||||
<option key={size} value={size}>
|
||||
{size}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Nozzles Grid */}
|
||||
{filteredNozzles.length === 0 ? (
|
||||
<div className="card text-center py-12">
|
||||
<BeakerIcon className="h-16 w-16 text-gray-300 mx-auto mb-4" />
|
||||
<h3 className="text-lg font-medium text-gray-900 mb-2">No Nozzles Found</h3>
|
||||
<p className="text-gray-600 mb-6">
|
||||
{searchTerm || selectedManufacturer || selectedDropletSize
|
||||
? 'Try adjusting your search or filters'
|
||||
: 'Start building your nozzle inventory'
|
||||
}
|
||||
</p>
|
||||
{!searchTerm && !selectedManufacturer && !selectedDropletSize && (
|
||||
<button
|
||||
onClick={() => setShowCreateForm(true)}
|
||||
className="btn-primary"
|
||||
>
|
||||
Add Your First Nozzle
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{filteredNozzles.map((nozzle) => (
|
||||
<NozzleCard key={nozzle.id} nozzle={nozzle} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Create Nozzle Form Modal */}
|
||||
{showCreateForm && (
|
||||
<NozzleFormModal
|
||||
isEdit={false}
|
||||
nozzle={null}
|
||||
nozzleTypes={nozzleTypes}
|
||||
onSubmit={handleCreateNozzle}
|
||||
onCancel={() => setShowCreateForm(false)}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Edit Nozzle Form Modal */}
|
||||
{showEditForm && editingNozzle && (
|
||||
<NozzleFormModal
|
||||
isEdit={true}
|
||||
nozzle={editingNozzle}
|
||||
nozzleTypes={nozzleTypes}
|
||||
onSubmit={handleUpdateNozzle}
|
||||
onCancel={() => {
|
||||
setShowEditForm(false);
|
||||
setEditingNozzle(null);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// Nozzle Form Modal Component
|
||||
const NozzleFormModal = ({ isEdit, nozzle, nozzleTypes, onSubmit, onCancel }) => {
|
||||
const [formData, setFormData] = useState({
|
||||
nozzleTypeId: nozzle?.nozzleTypeId || '',
|
||||
customName: nozzle?.customName || '',
|
||||
quantity: nozzle?.quantity || 1,
|
||||
condition: nozzle?.condition || 'good',
|
||||
purchaseDate: nozzle?.purchaseDate || '',
|
||||
notes: nozzle?.notes || ''
|
||||
});
|
||||
|
||||
const [selectedNozzleType, setSelectedNozzleType] = useState(
|
||||
nozzle?.nozzleType || null
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (formData.nozzleTypeId) {
|
||||
const nozzleType = nozzleTypes.find(type => type.id === parseInt(formData.nozzleTypeId));
|
||||
setSelectedNozzleType(nozzleType);
|
||||
}
|
||||
}, [formData.nozzleTypeId, nozzleTypes]);
|
||||
|
||||
const handleSubmit = (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (!formData.nozzleTypeId) {
|
||||
toast.error('Please select a nozzle type');
|
||||
return;
|
||||
}
|
||||
|
||||
const submitData = {
|
||||
nozzleTypeId: parseInt(formData.nozzleTypeId),
|
||||
customName: formData.customName || null,
|
||||
quantity: parseInt(formData.quantity),
|
||||
condition: formData.condition,
|
||||
purchaseDate: formData.purchaseDate || null,
|
||||
notes: formData.notes || null
|
||||
};
|
||||
|
||||
onSubmit(submitData);
|
||||
};
|
||||
|
||||
// Group nozzle types by manufacturer and droplet size for better organization
|
||||
const groupedNozzleTypes = nozzleTypes.reduce((groups, type) => {
|
||||
const key = `${type.manufacturer || 'Other'} - ${type.dropletSize || 'Unknown'}`;
|
||||
if (!groups[key]) {
|
||||
groups[key] = [];
|
||||
}
|
||||
groups[key].push(type);
|
||||
return groups;
|
||||
}, {});
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
|
||||
<div className="bg-white rounded-lg p-6 w-full max-w-2xl max-h-[90vh] overflow-y-auto">
|
||||
<h3 className="text-lg font-semibold mb-4">
|
||||
{isEdit ? 'Edit Nozzle' : 'Add New Nozzle'}
|
||||
</h3>
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
{/* Nozzle Type Selection */}
|
||||
<div>
|
||||
<label className="label">Nozzle Type *</label>
|
||||
<select
|
||||
className="input"
|
||||
value={formData.nozzleTypeId}
|
||||
onChange={(e) => setFormData({ ...formData, nozzleTypeId: e.target.value })}
|
||||
required
|
||||
>
|
||||
<option value="">Select nozzle type...</option>
|
||||
{Object.entries(groupedNozzleTypes).map(([group, types]) => (
|
||||
<optgroup key={group} label={group}>
|
||||
{types.map((type) => (
|
||||
<option key={type.id} value={type.id}>
|
||||
{type.name} - {type.orificeSize} ({type.flowRateGpm} GPM)
|
||||
</option>
|
||||
))}
|
||||
</optgroup>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{/* Nozzle Specifications Preview */}
|
||||
{selectedNozzleType && (
|
||||
<div className="p-4 bg-gray-50 rounded-lg">
|
||||
<h4 className="font-medium mb-2">Nozzle Specifications</h4>
|
||||
<div className="grid grid-cols-2 gap-3 text-sm">
|
||||
<div><strong>Manufacturer:</strong> {selectedNozzleType.manufacturer}</div>
|
||||
<div><strong>Model:</strong> {selectedNozzleType.model}</div>
|
||||
<div><strong>Orifice Size:</strong> {selectedNozzleType.orificeSize}</div>
|
||||
<div><strong>Spray Angle:</strong> {selectedNozzleType.sprayAngle}°</div>
|
||||
<div><strong>Flow Rate:</strong> {selectedNozzleType.flowRateGpm} GPM</div>
|
||||
<div><strong>Droplet Size:</strong> {selectedNozzleType.dropletSize}</div>
|
||||
<div><strong>Spray Pattern:</strong> {selectedNozzleType.sprayPattern?.replace('_', ' ')}</div>
|
||||
<div><strong>Pressure Range:</strong> {selectedNozzleType.pressureRangePsi} PSI</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Custom Name */}
|
||||
<div>
|
||||
<label className="label">Custom Name (Optional)</label>
|
||||
<input
|
||||
type="text"
|
||||
className="input"
|
||||
value={formData.customName}
|
||||
onChange={(e) => setFormData({ ...formData, customName: e.target.value })}
|
||||
placeholder="e.g., Backyard Sprayer Nozzles"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Quantity and Condition */}
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="label">Quantity *</label>
|
||||
<input
|
||||
type="number"
|
||||
min="1"
|
||||
className="input"
|
||||
value={formData.quantity}
|
||||
onChange={(e) => setFormData({ ...formData, quantity: e.target.value })}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="label">Condition</label>
|
||||
<select
|
||||
className="input"
|
||||
value={formData.condition}
|
||||
onChange={(e) => setFormData({ ...formData, condition: e.target.value })}
|
||||
>
|
||||
<option value="excellent">Excellent</option>
|
||||
<option value="good">Good</option>
|
||||
<option value="fair">Fair</option>
|
||||
<option value="poor">Poor</option>
|
||||
<option value="needs_replacement">Needs Replacement</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Purchase Date */}
|
||||
<div>
|
||||
<label className="label">Purchase Date</label>
|
||||
<input
|
||||
type="date"
|
||||
className="input"
|
||||
value={formData.purchaseDate}
|
||||
onChange={(e) => setFormData({ ...formData, purchaseDate: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Notes */}
|
||||
<div>
|
||||
<label className="label">Notes</label>
|
||||
<textarea
|
||||
className="input"
|
||||
rows="3"
|
||||
value={formData.notes}
|
||||
onChange={(e) => setFormData({ ...formData, notes: e.target.value })}
|
||||
placeholder="Usage notes, compatibility info, etc."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-3 pt-4">
|
||||
<button type="submit" className="btn-primary flex-1">
|
||||
{isEdit ? 'Update Nozzle' : 'Add Nozzle'}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onCancel}
|
||||
className="btn-secondary flex-1"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Nozzles;
|
||||
Reference in New Issue
Block a user