Skip to content

Commit

Permalink
Refactor StepAbilities component to handle newCharacter prop
Browse files Browse the repository at this point in the history
  • Loading branch information
gvorbeck committed Jan 26, 2024
1 parent 6e93e9e commit 827fff9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
45 changes: 28 additions & 17 deletions src/components/PageNewCharacter/StepAbilities/StepAbilities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ interface StepAbilitiesProps {
setComboClass?: (comboClass: boolean) => void;
setComboClassSwitch?: (comboClassSwitch: boolean) => void;
hideRollAll?: boolean;
newCharacter?: boolean;
}

const StepAbilities: React.FC<
StepAbilitiesProps & React.ComponentPropsWithRef<"div">
> = ({ className, character, setCharacter, hideRollAll }) => {
> = ({ className, character, setCharacter, hideRollAll, newCharacter }) => {
const dataSource = [
{
key: "1",
Expand Down Expand Up @@ -104,22 +105,32 @@ const StepAbilities: React.FC<
scores: Record<string, number>,
modifiers: Record<string, string>,
) => {
setCharacter({
...character,
abilities: {
scores: { ...character.abilities?.scores, ...scores },
modifiers: { ...character.abilities?.modifiers, ...modifiers },
},
class: [],
race: "",
hp: {
dice: "",
points: 0,
max: 0,
desc: "",
},
equipment: [],
});
if (newCharacter) {
setCharacter({
...character,
abilities: {
scores: { ...character.abilities?.scores, ...scores },
modifiers: { ...character.abilities?.modifiers, ...modifiers },
},
class: [],
race: "",
hp: {
dice: "",
points: 0,
max: 0,
desc: "",
},
equipment: [],
});
} else {
setCharacter({
...character,
abilities: {
scores: { ...character.abilities?.scores, ...scores },
modifiers: { ...character.abilities?.modifiers, ...modifiers },
},
});
}
};

const rollAllAbilities = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const StepHitPoints: React.FC<
roll = roll < 1 ? 1 : roll;
setMax(roll);
};
const handleInputChange = (value: number | null) => {
if (value) setMax(value);
};
const onSelectChange = (value: DiceTypes) => {
setDie(value);
};
Expand All @@ -55,7 +58,7 @@ const StepHitPoints: React.FC<
<Select options={options} onChange={onSelectChange} value={die} />
)}
<Space.Compact>
<InputNumber value={max} />
<InputNumber value={max} onChange={handleInputChange} />
<Button onClick={handleButtonClick} disabled={!die}>
Roll&nbsp;{character.level === 1 && "1"}
{die}
Expand Down
6 changes: 5 additions & 1 deletion src/support/pageNewCharacterSupport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export const getStepsItems = (
fulltitle: "Roll for Ability Scores",
description: newCharacterStepDescriptions.abilities,
content: (
<StepAbilities character={character} setCharacter={setCharacter} />
<StepAbilities
character={character}
setCharacter={setCharacter}
newCharacter
/>
),
},
{
Expand Down

0 comments on commit 827fff9

Please sign in to comment.