fix: route home for missing problems

pull/1/head
Antonio De Lucreziis 2 years ago
parent 438ddc8dd7
commit 717c2125c1

@ -40,10 +40,10 @@ type HeuristicStateUpdater<S> = StateUpdater<S>
type ResourceHookFunction = <T>( type ResourceHookFunction = <T>(
url: string | (() => string), url: string | (() => string),
initialValue: T, initialValue: T,
onError?: () => void onValue?: (value: T) => void
) => [T, RefreshFunction, HeuristicStateUpdater<T>] ) => [T, RefreshFunction, HeuristicStateUpdater<T>]
export const useResource: ResourceHookFunction = (url, initialValue, onError) => { export const useResource: ResourceHookFunction = (url, initialValue, onValue) => {
const [value, setValue] = useState(initialValue) const [value, setValue] = useState(initialValue)
function refresh() { function refresh() {
@ -55,11 +55,11 @@ export const useResource: ResourceHookFunction = (url, initialValue, onError) =>
if (res.ok) { if (res.ok) {
return res.json() return res.json()
} else { } else {
onError?.()
return initialValue return initialValue
} }
}) })
.then(newValue => { .then(newValue => {
onValue?.(newValue)
setValue(newValue) setValue(newValue)
}) })

@ -1,6 +1,6 @@
import { route } from 'preact-router' import { route } from 'preact-router'
import { useContext, useState } from 'preact/hooks' import { useContext, useState } from 'preact/hooks'
import { isAdministrator, Solution as SolutionModel } from '../../shared/model' import { isAdministrator, Problem as ProblemModel, Solution as SolutionModel } from '../../shared/model'
import { prependBaseUrl, server } from '../api' import { prependBaseUrl, server } from '../api'
import { Header } from '../components/Header' import { Header } from '../components/Header'
import { MarkdownEditor } from '../components/MarkdownEditor' import { MarkdownEditor } from '../components/MarkdownEditor'
@ -21,15 +21,13 @@ export const ProblemPage = ({ id }: RouteProps) => {
const [source, setSource] = useState('') const [source, setSource] = useState('')
const [{ content }] = useResource<{ content?: string }>( const [problem] = useResource<ProblemModel | null>(`/api/problem/${id}`, null, problem => {
`/api/problem/${id}`, if (problem === null) {
{
content: '',
},
() => {
route(prependBaseUrl('/')) route(prependBaseUrl('/'))
} }
) })
const content = problem?.content ?? ''
const [solutions, refreshSolutions, setSolutionHeuristic] = useListResource<SolutionModel>( const [solutions, refreshSolutions, setSolutionHeuristic] = useListResource<SolutionModel>(
`/api/solutions?problem=${id}` `/api/solutions?problem=${id}`

Loading…
Cancel
Save