import { useState } from 'preact/hooks' import { JSX } from 'preact/jsx-runtime' import { ProblemId, Solution as SolutionModel, SolutionId, SolutionStatus, UserId } from '../../shared/model' import { prependBaseUrl, server } from '../api' import { useCurrentUser } from '../hooks/useCurrentUser' import { Markdown } from './Markdown' const STATUS_SELECT_OPTIONS: Record = { ['pending']:
In attesa di correzione
, ['correct']:
Corretta
, ['wrong']:
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 [user] = useCurrentUser() 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 deleteSolution = async () => { if (confirm('Sei proprio sicuro di voler eliminare questa soluzione?')) { await server.delete(`/api/solution/${id}`) 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 && ( )}
) }