Skip to content

Commit

Permalink
refactor(types): fix CowCard Subheader type errors
Browse files Browse the repository at this point in the history
refactor(type): use record syntax
  • Loading branch information
jeremyckahn committed Aug 26, 2023
1 parent cbbbddd commit 09a0b6e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
46 changes: 34 additions & 12 deletions src/components/CowCard/Subheader/Subheader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @typedef {import('../../../components/Farmhand/Farmhand').farmhand.state} farmhand.state */
/** @typedef {import('../../../index').farmhand.cow} farmhand.cow */
/** @typedef {import('../CowCard').CowCardProps} CowCardProps */
import React from 'react'
import { array, bool, func, object, string } from 'prop-types'
import Checkbox from '@material-ui/core/Checkbox'
Expand Down Expand Up @@ -28,23 +29,44 @@ import './Subheader.sass'

// The extra 0.5 is for rounding up to the next full heart. This allows a fully
// happy cow to have full hearts on the beginning of a new day.
/**
* @param {number} heartIndex
* @param {number} numberOfFullHearts
*/
const isHeartFull = (heartIndex, numberOfFullHearts) =>
heartIndex + 0.5 < numberOfFullHearts

const getCowMapById = memoize(cowInventory =>
cowInventory.reduce((acc, cow) => {
acc[cow.id] = cow
return acc
}, {})
const getCowMapById = memoize(
/**
* @param {farmhand.state['cowInventory']} cowInventory
*/
cowInventory =>
cowInventory.reduce((acc, cow) => {
acc[cow.id] = cow
return acc
}, {})
)

/**
* @typedef {Pick<
* CowCardProps,
* 'cow' |
* 'cowBreedingPen' |
* 'cowIdOfferedForTrade' |
* 'cowInventory' |
* 'handleCowAutomaticHugChange' |
* 'handleCowBreedChange' |
* 'huggingMachinesRemain' |
* 'id'
* > & {
* canCowBeTradedFor: boolean,
* cowValue: number,
* isCowPurchased: boolean,
* }} SubheaderProps
*/

const Subheader = (
/**
* @type {{
* cow: farmhand.cow,
* id: farmhand.state['id']
* }}
*/
/** @type {SubheaderProps} */
{
canCowBeTradedFor,
cow,
Expand All @@ -66,7 +88,7 @@ const Subheader = (
const isThisCowOfferedForTrade = cowIdOfferedForTrade === cow.id

const mateId = cowBreedingPen.cowId1 ?? cowBreedingPen.cowId2
const mate = getCowMapById(cowInventory)[mateId]
const mate = getCowMapById(cowInventory)[mateId ?? '']
const isEligibleToBreed = cow.gender !== mate?.gender

const canBeMovedToBreedingPen =
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/ui-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ export default {
},

/**
* @param {external:React.SyntheticEvent} e
* @param {React.SyntheticEvent} e
* @param {farmhand.cow} cow
*/
handleCowAutomaticHugChange({ target: { checked } }, cow) {
this.changeCowAutomaticHugState(cow, checked)
},

/**
* @param {external:React.SyntheticEvent} e
* @param {React.SyntheticEvent} e
* @param {farmhand.cow} cow
*/
handleCowBreedChange({ target: { checked } }, cow) {
Expand Down Expand Up @@ -148,7 +148,7 @@ export default {
},

/**
* @param {external:React.SyntheticEvent} e
* @param {React.SyntheticEvent} e
* @param {farmhand.cow} cow
*/
handleCowNameInputChange({ target: { value } }, cow) {
Expand All @@ -164,7 +164,7 @@ export default {
},

/**
* @param {external:React.SyntheticEvent} e
* @param {React.SyntheticEvent} e
*/
handleViewChange({ target: { value } }) {
this.setState({ stageFocus: value })
Expand Down

0 comments on commit 09a0b6e

Please sign in to comment.