62 lines
3.8 KiB
JavaScript
62 lines
3.8 KiB
JavaScript
// search.js - Search module with inventory data and filtering logic
|
|
|
|
export const inventoryData = [
|
|
// Pantry items
|
|
{ id: 1, name: 'Pasta', location: 'Pantry', quantity: 5, unit: 'boxes', img: 'https://picsum.photos/seed/pasta/200/200' },
|
|
{ id: 2, name: 'Rice', location: 'Pantry', quantity: 3, unit: 'bags', img: 'https://picsum.photos/seed/rice/200/200' },
|
|
{ id: 3, name: 'Cereal', location: 'Pantry', quantity: 2, unit: 'boxes', img: 'https://picsum.photos/seed/cereal/200/200' },
|
|
{ id: 4, name: 'Flour', location: 'Pantry', quantity: 1, unit: 'bag', img: 'https://picsum.photos/seed/flour/200/200' },
|
|
{ id: 5, name: 'Sugar', location: 'Pantry', quantity: 2, unit: 'bags', img: 'https://picsum.photos/seed/sugar/200/200' },
|
|
{ id: 6, name: 'Salt', location: 'Pantry', quantity: 1, unit: 'box', img: 'https://picsum.photos/seed/salt/200/200' },
|
|
{ id: 7, name: 'Olive Oil', location: 'Pantry', quantity: 2, unit: 'bottles', img: 'https://picsum.photos/seed/oil/200/200' },
|
|
{ id: 8, name: 'Canned Beans', location: 'Pantry', quantity: 12, unit: 'cans', img: 'https://picsum.photos/seed/beans/200/200' },
|
|
|
|
// Fridge items
|
|
{ id: 9, name: 'Milk', location: 'Fridge', quantity: 1, unit: 'carton', img: 'https://picsum.photos/seed/milk/200/200' },
|
|
{ id: 10, name: 'Cheese', location: 'Fridge', quantity: 2, unit: 'blocks', img: 'https://picsum.photos/seed/cheese/200/200' },
|
|
{ id: 11, name: 'Greek Yogurt', location: 'Fridge', quantity: 3, unit: 'containers', img: 'https://picsum.photos/seed/yogurt/200/200' },
|
|
{ id: 12, name: 'Eggs', location: 'Fridge', quantity: 24, unit: 'eggs', img: 'https://picsum.photos/seed/eggs/200/200' },
|
|
{ id: 13, name: 'Butter', location: 'Fridge', quantity: 1, unit: 'pack', img: 'https://picsum.photos/seed/butter/200/200' },
|
|
{ id: 14, name: 'Chicken Salad', location: 'Fridge', quantity: 2, unit: 'containers', img: 'https://picsum.photos/seed/salad/200/200' },
|
|
|
|
// Freezer items
|
|
{ id: 15, name: 'Ice Cream', location: 'Freezer', quantity: 1, unit: 'tub', img: 'https://picsum.photos/seed/icecream/200/200' },
|
|
{ id: 16, name: 'Frozen Vegetables', location: 'Freezer', quantity: 5, unit: 'bags', img: 'https://picsum.photos/seed/veggies/200/200' },
|
|
{ id: 17, name: 'Chicken Breast', location: 'Freezer', quantity: 4, unit: 'packages', img: 'https://picsum.photos/seed/chicken/200/200' },
|
|
{ id: 18, name: 'Ground Beef', location: 'Freezer', quantity: 3, unit: 'packages', img: 'https://picsum.photos/seed/beef/200/200' },
|
|
{ id: 19, name: 'Pizza', location: 'Freezer', quantity: 2, unit: 'boxes', img: 'https://picsum.photos/seed/pizza/200/200' },
|
|
{ id: 20, name: 'Ice', location: 'Freezer', quantity: 1, unit: 'bag', img: 'https://picsum.photos/seed/ice/200/200' },
|
|
];
|
|
|
|
/**
|
|
* Search and filter inventory items
|
|
* @param {string} searchName - Search term for item name (case insensitive)
|
|
* @param {string} selectedLocation - Filter by location ('All', 'Pantry', 'Fridge', 'Freezer')
|
|
* @param {number} minQuantity - Minimum quantity filter
|
|
* @param {number} maxQuantity - Maximum quantity filter
|
|
* @returns {Array} Filtered inventory items
|
|
*/
|
|
export function searchInventory(searchName = '', selectedLocation = 'All', minQuantity = 0, maxQuantity = Infinity) {
|
|
return inventoryData.filter(item => {
|
|
// Filter by name
|
|
const nameMatch = item.name.toLowerCase().includes(searchName.toLowerCase());
|
|
|
|
// Filter by location
|
|
const locationMatch = selectedLocation === 'All' || item.location === selectedLocation;
|
|
|
|
// Filter by quantity range
|
|
const quantityMatch = item.quantity >= minQuantity && item.quantity <= maxQuantity;
|
|
|
|
return nameMatch && locationMatch && quantityMatch;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get all unique locations
|
|
* @returns {Array} List of unique locations
|
|
*/
|
|
export function getLocations() {
|
|
const locations = [...new Set(inventoryData.map(item => item.location))];
|
|
return ['All', ...locations.sort()];
|
|
}
|