Fix restoring party state

Instead of pulling it down from props on mount we're just pulling from state
This commit is contained in:
Justin Edmund 2022-12-01 04:00:39 -08:00
parent c307fcb18f
commit 6d602462fb

View file

@ -1,5 +1,6 @@
import React, { useEffect, useState } from "react" import React, { useEffect, useState } from "react"
import { useRouter } from "next/router" import { useRouter } from "next/router"
import { useSnapshot } from "valtio"
import { appState } from "~utils/appState" import { appState } from "~utils/appState"
import { jobGroups } from "~utils/jobGroups" import { jobGroups } from "~utils/jobGroups"
@ -21,11 +22,19 @@ const JobDropdown = React.forwardRef<HTMLSelectElement, Props>(
const router = useRouter() const router = useRouter()
const locale = router.locale || "en" const locale = router.locale || "en"
// Create snapshot of app state
const { party } = useSnapshot(appState)
// Set up local states for storing jobs // Set up local states for storing jobs
const [currentJob, setCurrentJob] = useState<Job>() const [currentJob, setCurrentJob] = useState<Job>()
const [jobs, setJobs] = useState<Job[]>() const [jobs, setJobs] = useState<Job[]>()
const [sortedJobs, setSortedJobs] = useState<GroupedJob>() const [sortedJobs, setSortedJobs] = useState<GroupedJob>()
// Set current job from state on mount
useEffect(() => {
setCurrentJob(party.job)
}, [])
// Organize jobs into groups on mount // Organize jobs into groups on mount
useEffect(() => { useEffect(() => {
const jobGroups = appState.jobs const jobGroups = appState.jobs
@ -67,11 +76,7 @@ const JobDropdown = React.forwardRef<HTMLSelectElement, Props>(
.sort((a, b) => a.order - b.order) .sort((a, b) => a.order - b.order)
.map((item, i) => { .map((item, i) => {
return ( return (
<option <option key={i} value={item.id}>
key={i}
value={item.id}
selected={item.id === props.currentJob}
>
{item.name[locale]} {item.name[locale]}
</option> </option>
) )
@ -88,8 +93,8 @@ const JobDropdown = React.forwardRef<HTMLSelectElement, Props>(
return ( return (
<select <select
key={currentJob?.id} key={currentJob ? currentJob.id : -1}
value={currentJob?.id} value={currentJob ? currentJob.id : -1}
onBlur={props.onBlur} onBlur={props.onBlur}
onChange={handleChange} onChange={handleChange}
ref={ref} ref={ref}