import { route } from 'preact-router' import { useState } from 'preact/hooks' import { isAdministrator, Solution as SolutionModel, SolutionId, User as UserModel } from '../../shared/model' import { server } from '../api' import { prependBaseUrl, sortByStringKey } from '../../shared/utils' import { Header } from '../components/Header' import { MarkdownEditor } from '../components/MarkdownEditor' import { Select } from '../components/Select' import { Solution } from '../components/Solution' import { useListResource } from '../hooks' import { useLoggedInUser } from '../hooks/useCurrentUser' const CreateProblem = ({}) => { const [source, setSource] = useState('') const [title, setTitle] = useState('') type ProblemFields = { title?: string content: string } const handleCreateProblem = async () => { if (source.trim().length === 0) { alert('Il campo di testo è vuoto!') return } const id = await server.post('/api/problem', { content: source, title, }) route(prependBaseUrl(`/problem/${id}`)) } return (
setTitle(e.target instanceof HTMLInputElement ? e.target.value : '')} placeholder="Problema..." />
) } type SortOrder = 'latest' | 'oldest' export const AdminPage = ({}) => { const [user, ready] = useLoggedInUser() if (!ready) { return <> } if (user === null) { route(prependBaseUrl('/login'), true) return <> } if (!isAdministrator(user.role)) { route(prependBaseUrl('/'), true) return <> } const [solutions, refreshSolutions, setSolutionHeuristic] = useListResource(`/api/solutions`) const [sortOrder, setSortOrder] = useState('oldest') const sortedSolutions = sortByStringKey(solutions, s => s.createdAt, sortOrder === 'oldest') const [trackInteracted, setTrackedInteracted] = useState>(new Set()) const hasUntrackedPending = sortedSolutions.filter(s => s.status === 'pending' || trackInteracted.has(s.id)).length > 0 const [users] = useListResource(`/api/users`) const [adminSuUsername, setAdminSuUsername] = useState('') const handleAdminSu = async () => { await server.post(`/api/admin/su`, { username: adminSuUsername, }) route(prependBaseUrl('/u/' + adminSuUsername)) location.reload() } return ( <>
Nuovo problema

Soluzioni da correggere
{hasUntrackedPending ? (
setAdminSuUsername(e.target instanceof HTMLInputElement ? e.target.value : '')} />
)}
) }