Skip to content

Commit

Permalink
feat(#491): show number of required grapes in inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Apr 23, 2024
1 parent c42d2aa commit e1d28ad
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
24 changes: 12 additions & 12 deletions src/components/Farmhand/Farmhand.context.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import { createContext } from 'react'
/**
* @type {import('react').Context<{
* gameState: farmhand.state & {
* blockInput: boolean,
* features: Record<string, boolean>,
* fieldToolInventory: farmhand.item[],
* isChatAvailable: boolean,
* levelEntitlements: farmhand.levelEntitlements,
* plantableCropInventory: farmhand.item[],
* playerInventory: farmhand.item[],
* playerInventoryQuantities: Record<string, number>,
* shopInventory: farmhand.item[],
* viewList: string[],
* viewTitle: string,
* blockInput: boolean,
* features: Record<string, boolean>,
* fieldToolInventory: farmhand.item[],
* isChatAvailable: boolean,
* levelEntitlements: farmhand.levelEntitlements,
* plantableCropInventory: farmhand.item[],
* playerInventory: farmhand.item[],
* playerInventoryQuantities: Record<string, number>,
* shopInventory: farmhand.item[],
* viewList: string[],
* viewTitle: string,
* }
* handlers: {}
* handlers: Record<string, (...any) => void>
* }>}
*/
// @ts-expect-error
Expand Down
16 changes: 3 additions & 13 deletions src/components/FermentationRecipeList/FermentationRecipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,11 @@ const getRecipesInstancesInCellar = memoize(
* @param {item} props.item
*/
export const FermentationRecipe = ({ item }) => {
/**
* @type {{
* gameState: {
* inventory: Array.<item>,
* cellarInventory: Array.<keg>,
* purchasedCellar: number
* },
* handlers: {
* handleMakeFermentationRecipeClick: function(item, number)
* }
* }}
*/
const {
gameState: { inventory, cellarInventory, purchasedCellar },
handlers: { handleMakeFermentationRecipeClick },
handlers: {
/** @type {function(item, number)} */ handleMakeFermentationRecipeClick,
},
} = useContext(FarmhandContext)

const [quantity, setQuantity] = useState(1)
Expand Down
15 changes: 11 additions & 4 deletions src/components/WineRecipeList/WineRecipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useContext } from 'react'
import { oneOf } from 'prop-types'
import Card from '@mui/material/Card'
import CardHeader from '@mui/material/CardHeader'
Expand All @@ -13,16 +13,23 @@ import { wineService } from '../../services/wine'
import { grapeVariety } from '../../enums'
import { wines } from '../../img'
import { integerString } from '../../utils'
import { getInventoryQuantityMap } from '../../utils/getInventoryQuantityMap'
import FarmhandContext from '../Farmhand/Farmhand.context'

/**
* @param {{
* wineVariety: grapeVariety
* }} props
*/
export const WineRecipe = ({ wineVariety }) => {
const {
gameState: { inventory },
} = useContext(FarmhandContext)
const wineName = grapeVarietyNameMap[wineVariety]
const grape = grapeVarietyVarietyGrapeMap[wineVariety]

const inventoryQuantityMap = getInventoryQuantityMap(inventory)

return (
<Card className="WineRecipe">
<CardHeader
Expand All @@ -42,12 +49,12 @@ export const WineRecipe = ({ wineVariety }) => {
{integerString(wineService.getDaysToMature(wineVariety))}
</p>
<p>
Ingredients:{' '}
Required:{' '}
{integerString(wineService.getGrapesRequiredForWine(wineVariety))}{' '}
x {grape.name}
x {grape.name} (available:{' '}
{integerString(inventoryQuantityMap[grape.id])})
</p>
{
// FIXME: Show number of required grapes in inventory
// FIXME: Show amount in cellar
}
</>
Expand Down
9 changes: 3 additions & 6 deletions src/utils/getInventoryQuantityMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
*/
import { memoize } from './memoize'

/**
* @param {item[]} inventory
* @returns {Object}
*/
export const getInventoryQuantityMap = memoize(
/**
* @param {item[]} inventory
* @returns {Object}
* @param {{ id: item['id'], quantity: number }[]} inventory
* @returns {Record<item['id'], number>}
*/
inventory =>
inventory.reduce((acc, { id, quantity }) => {
acc[id] = quantity

return acc
}, {})
)

0 comments on commit e1d28ad

Please sign in to comment.