refactor(cards): allow for changes to the existing card and corrected the display of current stats

This commit is contained in:
2025-04-14 12:43:01 -05:00
parent a0afb305fc
commit d4edeb15d9

View File

@@ -13,19 +13,18 @@ import { Input } from "../ui/input";
// SelectValue, // SelectValue,
// } from "../ui/select"; // } from "../ui/select";
import { Button } from "../ui/button"; import { Button } from "../ui/button";
import { toast } from "sonner";
export default function Cards(card: any) { export default function Cards(card: any) {
const { addCard, removeCard, cards } = useCardStore(); const { addCard, removeCard, cards } = useCardStore();
let existing: any = cards.filter((n: any) => n.name === card.name); let existing: any = cards.filter((n: any) => n.name === card.name);
console.log(existing); //console.log(existing);
const form = useForm({ const form = useForm({
defaultValues: { defaultValues: {
name: existing?.name || card.name, name: existing[0]?.name || card.name,
rowType: existing?.type ?? card.rowType, rowType: existing[0]?.type ?? card.rowType,
age: existing?.age ?? 90, age: existing[0]?.age ?? 90,
active: existing.active ? existing.active : false, active: existing[0]?.active ?? false,
}, },
onSubmit: async ({ value }) => { onSubmit: async ({ value }) => {
console.log(value); console.log(value);
@@ -34,17 +33,19 @@ export default function Cards(card: any) {
); );
if (value.active) { if (value.active) {
if (testCard.length > 0) {
toast.error("Card already exists");
return;
}
// change the name for a type card
const newCard = { const newCard = {
name: `${value.name}`, name: `${value.name}`,
rowType: value.rowType, rowType: value.rowType,
age: value.age ?? 90, age: value.age ?? 90,
active: value.active, active: value.active,
}; };
if (testCard.length > 0) {
removeCard(value.name);
addCard(newCard);
return;
}
// change the name for a type card
addCard(newCard); addCard(newCard);
} else { } else {
removeCard(value.name); removeCard(value.name);
@@ -52,7 +53,7 @@ export default function Cards(card: any) {
}, },
}); });
return ( return (
<div className="border-solid border-2"> <div className="border-solid border-2 m-2">
<p>{card.name}</p> <p>{card.name}</p>
<form <form
onSubmit={(e) => { onSubmit={(e) => {
@@ -112,6 +113,7 @@ export default function Cards(card: any) {
<Input <Input
name={field.name} name={field.name}
onBlur={field.handleBlur} onBlur={field.handleBlur}
value={field.state.value}
type="number" type="number"
onChange={(e) => onChange={(e) =>
field.handleChange( field.handleChange(