Converted web app to react + js
This commit is contained in:
49
src/utils/searchUtils.js
Normal file
49
src/utils/searchUtils.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import { inventoryData } from '../data/inventory.js'
|
||||
|
||||
export function formatDate(dateStr) {
|
||||
if (!dateStr) return ''
|
||||
const date = new Date(dateStr + 'T00:00:00')
|
||||
return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })
|
||||
}
|
||||
|
||||
export function getExpiryStatus(expiryDate) {
|
||||
if (!expiryDate) return { status: 'Unknown', color: '#999', text: '' }
|
||||
|
||||
const today = new Date()
|
||||
today.setHours(0, 0, 0, 0)
|
||||
|
||||
const expiry = new Date(expiryDate + 'T00:00:00')
|
||||
const daysUntilExpiry = Math.floor((expiry - today) / (1000 * 60 * 60 * 24))
|
||||
|
||||
if (daysUntilExpiry < 0) {
|
||||
return { status: 'Expired', color: '#f44336', days: daysUntilExpiry, text: `Expired ${Math.abs(daysUntilExpiry)} days ago` }
|
||||
} else if (daysUntilExpiry === 0) {
|
||||
return { status: 'Today', color: '#ff9800', days: 0, text: 'Expires today' }
|
||||
} else if (daysUntilExpiry <= 7) {
|
||||
return { status: 'Soon', color: '#ff9800', days: daysUntilExpiry, text: `Expires in ${daysUntilExpiry} days` }
|
||||
} else {
|
||||
return { status: 'Fresh', color: '#4CAF50', days: daysUntilExpiry, text: `Expires in ${daysUntilExpiry} days` }
|
||||
}
|
||||
}
|
||||
|
||||
export function searchInventory(searchName = '', selectedLocation = 'All', minQuantity = 0, maxQuantity = Infinity, minExpiryDate = '', maxExpiryDate = '') {
|
||||
return inventoryData.filter(item => {
|
||||
const nameMatch = item.name.toLowerCase().includes(searchName.toLowerCase())
|
||||
const locationMatch = selectedLocation === 'All' || item.location === selectedLocation
|
||||
const quantityMatch = item.quantity >= minQuantity && item.quantity <= maxQuantity
|
||||
|
||||
let expiryMatch = true
|
||||
if (minExpiryDate || maxExpiryDate) {
|
||||
const itemExpiry = item.expiryDate
|
||||
if (minExpiryDate && itemExpiry < minExpiryDate) expiryMatch = false
|
||||
if (maxExpiryDate && itemExpiry > maxExpiryDate) expiryMatch = false
|
||||
}
|
||||
|
||||
return nameMatch && locationMatch && quantityMatch && expiryMatch
|
||||
})
|
||||
}
|
||||
|
||||
export function getLocations() {
|
||||
const locations = [...new Set(inventoryData.map(item => item.location))]
|
||||
return ['All', ...locations.sort()]
|
||||
}
|
||||
Reference in New Issue
Block a user