From 4e99d8dd37ad3fee434a6927494d5189a65fb015 Mon Sep 17 00:00:00 2001 From: az1plo Mak <1995Azimut@gmail.com> Date: Mon, 25 Nov 2024 10:22:08 +0000 Subject: [PATCH] #525 List search: added SearchBar, added new filter to inventory --- .../Cellar/CellarInventoryTabPanel.js | 29 ++- .../CowPenContextMenu/CowPenContextMenu.js | 225 +++++++++++------- .../FermentationRecipeList.js | 47 +++- .../FermentationRecipeList.test.js | 60 ++++- src/components/Inventory/Inventory.js | 142 ++++++++--- src/components/Inventory/Inventory.sass | 41 ++++ src/components/Inventory/Inventory.test.js | 75 +++++- src/components/RecipeList/RecipeList.js | 42 +++- src/components/SearchBar/SearchBar.js | 26 ++ src/components/SearchBar/SearchBar.sass | 53 +++++ src/components/SearchBar/SearchBar.test.js | 32 +++ src/components/SearchBar/index.js | 1 + src/components/Shop/Shop.js | 2 + .../WineRecipeList/WineRecipeList.js | 23 +- 14 files changed, 644 insertions(+), 154 deletions(-) create mode 100644 src/components/Inventory/Inventory.sass create mode 100644 src/components/SearchBar/SearchBar.js create mode 100644 src/components/SearchBar/SearchBar.sass create mode 100644 src/components/SearchBar/SearchBar.test.js create mode 100644 src/components/SearchBar/index.js diff --git a/src/components/Cellar/CellarInventoryTabPanel.js b/src/components/Cellar/CellarInventoryTabPanel.js index 999bb050..40149444 100644 --- a/src/components/Cellar/CellarInventoryTabPanel.js +++ b/src/components/Cellar/CellarInventoryTabPanel.js @@ -1,11 +1,12 @@ /** @typedef {import("../../index").farmhand.keg} keg */ -import React, { useContext } from 'react' +import React, { useContext, useState } from 'react' import { number } from 'prop-types' import Divider from '@mui/material/Divider/index.js' import Card from '@mui/material/Card/index.js' import CardContent from '@mui/material/CardContent/index.js' import ReactMarkdown from 'react-markdown' +import SearchBar from '../SearchBar/index.js' import FarmhandContext from '../Farmhand/Farmhand.context.js' import { KEG_INTEREST_RATE, @@ -15,6 +16,8 @@ import { } from '../../constants.js' import { integerString } from '../../utils/index.js' +import { itemsMap } from '../../data/maps.js' +import { FERMENTED_CROP_NAME } from '../../templates.js' import { TabPanel } from './TabPanel/index.js' import { Keg } from './Keg.js' @@ -25,6 +28,8 @@ import { Keg } from './Keg.js' * @param {number} props.currentTab */ export const CellarInventoryTabPanel = ({ index, currentTab }) => { + const [searchQuery, setSearchQuery] = useState('') + /** * @type {{ * gameState: { @@ -37,14 +42,32 @@ export const CellarInventoryTabPanel = ({ index, currentTab }) => { gameState: { cellarInventory, purchasedCellar }, } = useContext(FarmhandContext) + const searchTerms = searchQuery + .toLowerCase() + .split(' ') + .filter(term => term.length > 0) + + const filteredKegs = cellarInventory.filter(keg => { + const item = itemsMap[keg.itemId] + const itemName = item.name.toLowerCase() + const fermentationRecipeName = `${FERMENTED_CROP_NAME}${item.name}`.toLowerCase() + + return searchTerms.every( + term => fermentationRecipeName.includes(term) || itemName.includes(term) + ) + }) + return (

- Capacity: {integerString(cellarInventory.length)} /{' '} + Capacity: {integerString(filteredKegs.length)} /{' '} {integerString(PURCHASEABLE_CELLARS.get(purchasedCellar).space)}

+ {cellarInventory.length > 0 && ( + + )} + )} + + + ) + })()}
+ {cowShopInventory.length > 0 && ( + + )}