import { useState } from 'preact/hooks' import { JSX } from 'preact/jsx-runtime' import { MetadataProps, ProblemId, Solution as SolutionModel, SolutionId, SolutionStatus, UserId, } from '../../shared/model' import { prependBaseUrl, server } from '../api' import { Markdown } from './Markdown' import { Select } from './Select' const STATUS_SELECT_OPTIONS: Record = { ['pending']:
Soluzione in attesa di correzione
, ['correct']:
Soluzione corretta
, ['wrong']:
Soluzione sbagliata
, } type Props = { id: SolutionId createdAt: string sentBy?: UserId forProblem: ProblemId content: string status?: SolutionStatus visible?: boolean adminControls: boolean setSolution?: (solutionFn: (prev: SolutionModel) => SolutionModel) => void refreshSolution?: () => void } export const Solution = ({ id, createdAt, sentBy, forProblem, content, status, visible, adminControls, setSolution, refreshSolution, }: Props) => { const markAsCorrect = async () => { setSolution?.(prevSolution => ({ ...prevSolution, status: 'correct' })) await server.patch(`/api/solution/${id}`, { status: 'correct', }) refreshSolution?.() } const markAsWrong = async () => { setSolution?.(prevSolution => ({ ...prevSolution, status: 'wrong' })) await server.patch(`/api/solution/${id}`, { status: 'wrong', }) refreshSolution?.() } const changeVisibility = async () => { setSolution?.(prevSolution => ({ ...prevSolution, visible: !visible })) await server.patch(`/api/solution/${id}`, { visible: !visible, }) refreshSolution?.() } const [viewRaw, setViewRaw] = useState(false) const toggleViewRaw = () => { setViewRaw(prev => !prev) } const d = new Date(createdAt) return (
Soluzione {sentBy && ( <> {' '} di @{sentBy} )} {forProblem && ( <> {' '} per il{' '} Problema {forProblem} )} {!isNaN(d as any) && ( <> {' del '} {d.getFullYear()}/{d.getMonth().toString().padStart(2, '0')}/ {d.getDate().toString().padStart(2, '0')}{' '} {d.getHours().toString().padStart(2, '0')}: {d.getMinutes().toString().padStart(2, '0')} )}
{viewRaw ? (
                        {content}
                    
) : ( )}
{status && ( )}
) }