Skip to content

Commit

Permalink
use getter and setter for state
Browse files Browse the repository at this point in the history
  • Loading branch information
craigkai committed Sep 10, 2024
1 parent 0393fcf commit a16ca3e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions src/components/EditRef.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@
// Select the first match in the round as the reference match
let match = $state(matchesPerRound ? matchesPerRound[0] : null);
let selectedRef: number | null = $state(match?.ref ?? null);
let selectedRef = $state({
get ref() {
return match?.ref ?? null;
},
set ref(ref: number) {
match.ref = ref;
}
});
let open = $state(false);
// Ref is recalculated whenever match.ref or teams changes
let ref = $derived(teams.teams.find((t: Team) => t.id === match?.ref));
// Save the referee for both team1 and team2 in the match
async function saveRef() {
if (match && selectedRef !== null) {
if (match && selectedRef.ref !== null) {
try {
for (let m of matchesPerRound) {
m.ref = selectedRef;
m.ref = selectedRef.ref;
const updatedMatch = await updateMatch(m);
if (!updatedMatch) {
Expand Down Expand Up @@ -83,10 +90,10 @@
<Label class="mb-4 text-lg font-semibold">Select Referee:</Label>
<Select.Root
selected={{
value: selectedRef,
label: teams.teams.find((t: Team) => t.id === selectedRef)?.name
value: selectedRef.ref,
label: teams.teams.find((t: Team) => t.id === selectedRef.ref)?.name
}}
onSelectedChange={(event) => (selectedRef = event?.value as number)}
onSelectedChange={(event) => (selectedRef.ref = event?.value as number)}
>
<Select.Trigger class="mx-auto w-[180px]">
<!-- Add mx-auto for horizontal centering -->
Expand Down

0 comments on commit a16ca3e

Please sign in to comment.