You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.1 KiB
TypeScript

import { route } from 'preact-router'
import { useState } from 'preact/hooks'
2 years ago
import { isAdministrator, isStudent, Solution as SolutionModel } from '../../shared/model'
import { server } from '../api'
import { Header } from '../components/Header'
import { MarkdownEditor } from '../components/MarkdownEditor'
2 years ago
import { Solution } from '../components/Solution'
import { useCurrentUser, useListResource, useResource } from '../hooks'
const CreateProblem = ({}) => {
const [source, setSource] = useState('')
const createProblem = async () => {
const id = await server.post('/api/problem', {
content: source,
})
route(`/problem/${id}`)
}
return (
<>
<MarkdownEditor {...{ source, setSource }} />
<button onClick={createProblem}>Aggiungi Problema</button>
</>
)
}
export const AdminPage = ({}) => {
const [user] = useCurrentUser(user => {
if (!user) {
route('/login', true)
} else if (isStudent(user.role)) {
route('/', true)
}
})
2 years ago
const [solutions, refreshSolutions, setSolutionHeuristic] =
useListResource<SolutionModel>(`/api/solutions`)
return (
user && (
<main class="page-admin">
<Header {...{ user }} />
<div class="subtitle">Nuovo problema</div>
<CreateProblem />
<div class="subtitle">Soluzioni ancora da approvare/rifiutare</div>
2 years ago
<div class="solution-list">
{solutions.map(
(s, index) =>
s.status === 'pending' && (
<Solution
adminControls
{...s}
setSolution={solFn => setSolutionHeuristic(index, solFn)}
refreshSolution={refreshSolutions}
/>
)
)}
</div>
</main>
)
)
}