From 09a0b6eff63e219485306ea60a079b90c7c723e4 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Sat, 26 Aug 2023 16:11:42 -0500 Subject: [PATCH] refactor(types): fix CowCard Subheader type errors refactor(type): use record syntax --- src/components/CowCard/Subheader/Subheader.js | 46 ++++++++++++++----- src/handlers/ui-events.js | 8 ++-- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/components/CowCard/Subheader/Subheader.js b/src/components/CowCard/Subheader/Subheader.js index abf6059c0..f65443877 100644 --- a/src/components/CowCard/Subheader/Subheader.js +++ b/src/components/CowCard/Subheader/Subheader.js @@ -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' @@ -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, @@ -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 = diff --git a/src/handlers/ui-events.js b/src/handlers/ui-events.js index e2c1bc99e..279692718 100644 --- a/src/handlers/ui-events.js +++ b/src/handlers/ui-events.js @@ -111,7 +111,7 @@ export default { }, /** - * @param {external:React.SyntheticEvent} e + * @param {React.SyntheticEvent} e * @param {farmhand.cow} cow */ handleCowAutomaticHugChange({ target: { checked } }, cow) { @@ -119,7 +119,7 @@ export default { }, /** - * @param {external:React.SyntheticEvent} e + * @param {React.SyntheticEvent} e * @param {farmhand.cow} cow */ handleCowBreedChange({ target: { checked } }, cow) { @@ -148,7 +148,7 @@ export default { }, /** - * @param {external:React.SyntheticEvent} e + * @param {React.SyntheticEvent} e * @param {farmhand.cow} cow */ handleCowNameInputChange({ target: { value } }, cow) { @@ -164,7 +164,7 @@ export default { }, /** - * @param {external:React.SyntheticEvent} e + * @param {React.SyntheticEvent} e */ handleViewChange({ target: { value } }) { this.setState({ stageFocus: value })