diff --git a/client/hooks.tsx b/client/hooks.tsx index f5068d0..255fbf2 100644 --- a/client/hooks.tsx +++ b/client/hooks.tsx @@ -39,10 +39,11 @@ type HeuristicStateUpdater = StateUpdater type ResourceHookFunction = ( url: string | (() => string), - initialValue: T + initialValue: T, + onError?: () => void ) => [T, RefreshFunction, HeuristicStateUpdater] -export const useResource: ResourceHookFunction = (url, initialValue) => { +export const useResource: ResourceHookFunction = (url, initialValue, onError) => { const [value, setValue] = useState(initialValue) function refresh() { @@ -54,6 +55,7 @@ export const useResource: ResourceHookFunction = (url, initialValue) => { if (res.ok) { return res.json() } else { + onError?.() return initialValue } }) diff --git a/client/pages/Problem.tsx b/client/pages/Problem.tsx index 41d4634..bb1c3a0 100644 --- a/client/pages/Problem.tsx +++ b/client/pages/Problem.tsx @@ -1,6 +1,7 @@ +import { route } from 'preact-router' import { useContext, useState } from 'preact/hooks' -import { isAdministrator, Problem as ProblemModel, Solution as SolutionModel } from '../../shared/model' -import { server } from '../api' +import { isAdministrator, Solution as SolutionModel } from '../../shared/model' +import { prependBaseUrl, server } from '../api' import { Header } from '../components/Header' import { MarkdownEditor } from '../components/MarkdownEditor' import { Problem } from '../components/Problem' @@ -20,9 +21,15 @@ export const ProblemPage = ({ id }: RouteProps) => { const [source, setSource] = useState('') - const [{ content }] = useResource<{ content: string }>(`/api/problem/${id}`, { - content: '', - }) + const [{ content }] = useResource<{ content?: string }>( + `/api/problem/${id}`, + { + content: '', + }, + () => { + route(prependBaseUrl('/')) + } + ) const [solutions, refreshSolutions, setSolutionHeuristic] = useListResource( `/api/solutions?problem=${id}`