fix: missing problem redirects to homepage
This commit is contained in:
+4
-2
@@ -39,10 +39,11 @@ type HeuristicStateUpdater<S> = StateUpdater<S>
|
|||||||
|
|
||||||
type ResourceHookFunction = <T>(
|
type ResourceHookFunction = <T>(
|
||||||
url: string | (() => string),
|
url: string | (() => string),
|
||||||
initialValue: T
|
initialValue: T,
|
||||||
|
onError?: () => void
|
||||||
) => [T, RefreshFunction, HeuristicStateUpdater<T>]
|
) => [T, RefreshFunction, HeuristicStateUpdater<T>]
|
||||||
|
|
||||||
export const useResource: ResourceHookFunction = (url, initialValue) => {
|
export const useResource: ResourceHookFunction = (url, initialValue, onError) => {
|
||||||
const [value, setValue] = useState(initialValue)
|
const [value, setValue] = useState(initialValue)
|
||||||
|
|
||||||
function refresh() {
|
function refresh() {
|
||||||
@@ -54,6 +55,7 @@ export const useResource: ResourceHookFunction = (url, initialValue) => {
|
|||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
return res.json()
|
return res.json()
|
||||||
} else {
|
} else {
|
||||||
|
onError?.()
|
||||||
return initialValue
|
return initialValue
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
|
import { route } from 'preact-router'
|
||||||
import { useContext, useState } from 'preact/hooks'
|
import { useContext, useState } from 'preact/hooks'
|
||||||
import { isAdministrator, Problem as ProblemModel, Solution as SolutionModel } from '../../shared/model'
|
import { isAdministrator, Solution as SolutionModel } from '../../shared/model'
|
||||||
import { 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'
|
||||||
import { Problem } from '../components/Problem'
|
import { Problem } from '../components/Problem'
|
||||||
@@ -20,9 +21,15 @@ export const ProblemPage = ({ id }: RouteProps) => {
|
|||||||
|
|
||||||
const [source, setSource] = useState('')
|
const [source, setSource] = useState('')
|
||||||
|
|
||||||
const [{ content }] = useResource<{ content: string }>(`/api/problem/${id}`, {
|
const [{ content }] = useResource<{ content?: string }>(
|
||||||
content: '',
|
`/api/problem/${id}`,
|
||||||
})
|
{
|
||||||
|
content: '',
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
route(prependBaseUrl('/'))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const [solutions, refreshSolutions, setSolutionHeuristic] = useListResource<SolutionModel>(
|
const [solutions, refreshSolutions, setSolutionHeuristic] = useListResource<SolutionModel>(
|
||||||
`/api/solutions?problem=${id}`
|
`/api/solutions?problem=${id}`
|
||||||
|
|||||||
Reference in New Issue
Block a user