feature: Problem sorting, solution view raw toggle button

pull/1/head
Antonio De Lucreziis 2 years ago
parent dcaa052f30
commit 28a1a85531

@ -1,23 +1,31 @@
import { StateUpdater } from 'preact/hooks' import { StateUpdater } from 'preact/hooks'
import { JSX } from 'preact/jsx-runtime' import { JSX } from 'preact/jsx-runtime'
type Props = { type Props<K extends string, T extends K> = {
options: Record<string, string> options: Record<K, string | JSX.Element>
value: string value: T
setValue?: StateUpdater<string> setValue?: StateUpdater<K>
} }
export const Select = ({ options, value, setValue }: Props) => ( export const Select = <K extends string, T extends K>({ options, value, setValue }: Props<K, T>) => {
<div class="input-select"> const optionValues = new Set(Object.keys(options))
<select
onInput={e => setValue?.(e.target instanceof HTMLSelectElement ? e.target.value : '')} return (
> <div class="input-select">
{Object.entries(options).map(([k, v]) => ( <select
<option value={k} selected={value === k}> onInput={e => {
{v} if (e.target instanceof HTMLSelectElement && optionValues.has(e.target.value)) {
</option> setValue?.(e.target.value as K)
))} }
</select> }}
<span class="material-symbols-outlined">expand_more</span> >
</div> {Object.entries<string | JSX.Element>(options).map(([k, v]) => (
) <option value={k} selected={value === k}>
{v}
</option>
))}
</select>
<span class="material-symbols-outlined">expand_more</span>
</div>
)
}

@ -1,3 +1,4 @@
import { useState } from 'preact/hooks'
import { JSX } from 'preact/jsx-runtime' import { JSX } from 'preact/jsx-runtime'
import { import {
MetadataProps, MetadataProps,
@ -19,6 +20,7 @@ const STATUS_SELECT_OPTIONS: Record<SolutionStatus, JSX.Element> = {
type Props = { type Props = {
id: SolutionId id: SolutionId
createdAt: string
sentBy?: UserId sentBy?: UserId
forProblem: ProblemId forProblem: ProblemId
content: string content: string
@ -31,6 +33,7 @@ type Props = {
export const Solution = ({ export const Solution = ({
id, id,
createdAt,
sentBy, sentBy,
forProblem, forProblem,
content, content,
@ -70,6 +73,14 @@ export const Solution = ({
refreshSolution?.() refreshSolution?.()
} }
const [viewRaw, setViewRaw] = useState<boolean>(false)
const toggleViewRaw = () => {
setViewRaw(prev => !prev)
}
const d = new Date(createdAt)
return ( return (
<div class={['solution', status].join(' ')}> <div class={['solution', status].join(' ')}>
<div class="solution-header"> <div class="solution-header">
@ -87,35 +98,38 @@ export const Solution = ({
per il <a href={`/problem/${forProblem}`}>Problema {forProblem}</a> per il <a href={`/problem/${forProblem}`}>Problema {forProblem}</a>
</> </>
)} )}
{!isNaN(d as any) && (
<>
{' del '}
<span title={!isNaN(d as any) ? d.toISOString() : undefined} class="dotted">
{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')}
</span>
</>
)}
</div> </div>
</div> </div>
<div class="solution-content"> <div class="solution-content">
<Markdown source={content} /> {viewRaw ? (
<pre>
<code>{content}</code>
</pre>
) : (
<Markdown source={content} />
)}
</div> </div>
{status && ( {status && (
<div class="solution-footer"> <div class="solution-footer">
{/* <div class="row">
<div class="label">Stato</div>
<Select value={status} options={STATUS_SELECT_OPTIONS} />
</div> */}
<div class="row"> <div class="row">
<div class="status-label">{STATUS_SELECT_OPTIONS[status]}</div> <div class="status-label">{STATUS_SELECT_OPTIONS[status]}</div>
{adminControls && ( {adminControls && (
<> <>
<button <button disabled={status === 'correct'} class="icon" onClick={markAsCorrect}>
disabled={status === 'correct'} <span class="material-symbols-outlined correct">check_circle</span>
class="icon"
onClick={markAsCorrect}
>
<span class="material-symbols-outlined correct">
check_circle
</span>
</button> </button>
<button <button disabled={status === 'wrong'} class="icon" onClick={markAsWrong}>
disabled={status === 'wrong'}
class="icon"
onClick={markAsWrong}
>
<span class="material-symbols-outlined wrong">cancel</span> <span class="material-symbols-outlined wrong">cancel</span>
</button> </button>
{status !== 'pending' && ( {status !== 'pending' && (
@ -127,6 +141,15 @@ export const Solution = ({
)} )}
</> </>
)} )}
<button
class="icon"
onClick={toggleViewRaw}
title={!viewRaw ? 'Mostra markdown grezzo' : 'Mostra testo matematicoso'}
>
<span class="material-symbols-outlined">
{!viewRaw ? 'data_object' : 'functions'}
</span>
</button>
</div> </div>
</div> </div>
)} )}

@ -44,8 +44,6 @@ type ResourceHookFunction = <T>(
) => [T, RefreshFunction, HeuristicStateUpdater<T>] ) => [T, RefreshFunction, HeuristicStateUpdater<T>]
export const useResource: ResourceHookFunction = (url, initialValue) => { export const useResource: ResourceHookFunction = (url, initialValue) => {
//
const [value, setValue] = useState(initialValue) const [value, setValue] = useState(initialValue)
function refresh() { function refresh() {

@ -1,9 +1,11 @@
import { route } from 'preact-router' import { route } from 'preact-router'
import { useState } from 'preact/hooks' import { useState } from 'preact/hooks'
import { isAdministrator, isStudent, Solution as SolutionModel } from '../../shared/model' import { isAdministrator, isStudent, Solution as SolutionModel, SolutionId } from '../../shared/model'
import { sortByStringKey } from '../../shared/utils'
import { server } from '../api' import { server } from '../api'
import { Header } from '../components/Header' import { Header } from '../components/Header'
import { MarkdownEditor } from '../components/MarkdownEditor' import { MarkdownEditor } from '../components/MarkdownEditor'
import { Select } from '../components/Select'
import { Solution } from '../components/Solution' import { Solution } from '../components/Solution'
import { useCurrentUser, useListResource, useResource } from '../hooks' import { useCurrentUser, useListResource, useResource } from '../hooks'
@ -25,6 +27,8 @@ const CreateProblem = ({}) => {
) )
} }
type SortOrder = 'latest' | 'oldest'
export const AdminPage = ({}) => { export const AdminPage = ({}) => {
const [user] = useCurrentUser(user => { const [user] = useCurrentUser(user => {
if (!user) { if (!user) {
@ -37,26 +41,56 @@ export const AdminPage = ({}) => {
const [solutions, refreshSolutions, setSolutionHeuristic] = const [solutions, refreshSolutions, setSolutionHeuristic] =
useListResource<SolutionModel>(`/api/solutions`) useListResource<SolutionModel>(`/api/solutions`)
const [sortOrder, setSortOrder] = useState<SortOrder>('oldest')
const sortedSolutions = sortByStringKey(solutions, s => s.createdAt, sortOrder === 'oldest')
const [trackInteracted, setTrackedInteracted] = useState<Set<SolutionId>>(new Set())
const hasUntrackedPending =
sortedSolutions.filter(s => s.status === 'pending' || trackInteracted.has(s.id)).length > 0
return ( return (
user && ( user && (
<main class="page-admin"> <main class="page-admin">
<Header {...{ user }} /> <Header {...{ user }} />
<div class="subtitle">Nuovo problema</div> <div class="subtitle">Nuovo problema</div>
<CreateProblem /> <CreateProblem />
<div class="subtitle">Soluzioni ancora da approvare/rifiutare</div> <div class="subtitle">Soluzioni da correggere</div>
<div class="solution-list"> {hasUntrackedPending ? (
{solutions.map( <div class="solution-list">
(s, index) => <div class="controls">
s.status === 'pending' && ( <div class="sort-order">
<Solution <Select
adminControls value={sortOrder}
{...s} setValue={setSortOrder}
setSolution={solFn => setSolutionHeuristic(index, solFn)} options={{
refreshSolution={refreshSolutions} latest: 'Prima più recenti',
oldest: 'Prima più antichi',
}}
/> />
) </div>
)} </div>
</div> {sortedSolutions.map(
(s, index) =>
(s.status === 'pending' || trackInteracted.has(s.id)) && (
<Solution
adminControls
{...s}
setSolution={solFn => {
setTrackedInteracted(prev => new Set([...prev, s.id]))
setSolutionHeuristic(index, solFn)
}}
refreshSolution={refreshSolutions}
/>
)
)}
</div>
) : (
<>
<em>Nessuna soluzione ancora da correggere</em>
</>
)}
</main> </main>
) )
) )

@ -1,17 +1,40 @@
import { route } from 'preact-router' import { route } from 'preact-router'
import { useEffect, useState } from 'preact/hooks' import { useEffect, useState } from 'preact/hooks'
import { Problem as ProblemModel } from '../../shared/model' import { Problem as ProblemModel } from '../../shared/model'
import { sortByNumericKey, sortByStringKey } from '../../shared/utils'
import { Header } from '../components/Header' import { Header } from '../components/Header'
import { Problem } from '../components/Problem' import { Problem } from '../components/Problem'
import { Select } from '../components/Select' import { Select } from '../components/Select'
import { useResource, useCurrentUser } from '../hooks' import { useResource, useCurrentUser } from '../hooks'
function byTime(p: ProblemModel): string {
return p.createdAt
}
function bySolvedProblems(p: ProblemModel & { solutionsCount: number }): number {
return p.solutionsCount
}
type SortOrder = 'latest' | 'oldest' | 'top-solved' | 'least-solved'
const SORT_ORDER: Record<SortOrder, boolean> = {
'latest': false,
'oldest': true,
'top-solved': false,
'least-solved': true,
}
export const HomePage = () => { export const HomePage = () => {
const [user] = useCurrentUser() const [user] = useCurrentUser()
const [problems] = useResource<ProblemModel[]>('/api/problems', []) const [problems] = useResource<(ProblemModel & { solutionsCount: number })[]>('/api/problems', [])
const [sortOrder, setSortOrder] = useState<SortOrder>('oldest')
console.log(problems) const sortedProblems =
sortOrder === 'latest' || sortOrder === 'oldest'
? sortByStringKey(problems, byTime, SORT_ORDER[sortOrder])
: sortByNumericKey(problems, bySolvedProblems, SORT_ORDER[sortOrder])
return ( return (
<main class="page-home"> <main class="page-home">
@ -20,7 +43,8 @@ export const HomePage = () => {
<div class="fill-row board-controls"> <div class="fill-row board-controls">
<div class="sort-order"> <div class="sort-order">
<Select <Select
value="oldest" value={sortOrder}
setValue={setSortOrder}
options={{ options={{
'latest': 'Prima più recenti', 'latest': 'Prima più recenti',
'oldest': 'Prima più antichi', 'oldest': 'Prima più antichi',
@ -31,7 +55,7 @@ export const HomePage = () => {
</div> </div>
</div> </div>
{problems.map(p => ( {sortedProblems.map(p => (
<Problem {...p} /> <Problem {...p} />
))} ))}
</div> </div>

@ -1,9 +1,5 @@
import { useContext, useState } from 'preact/hooks' import { useContext, useState } from 'preact/hooks'
import { import { isAdministrator, Problem as ProblemModel, Solution as SolutionModel } from '../../shared/model'
isAdministrator,
Problem as ProblemModel,
Solution as SolutionModel,
} from '../../shared/model'
import { server } from '../api' import { server } from '../api'
import { Header } from '../components/Header' import { Header } from '../components/Header'
import { MarkdownEditor } from '../components/MarkdownEditor' import { MarkdownEditor } from '../components/MarkdownEditor'
@ -37,6 +33,8 @@ export const ProblemPage = ({ id }: RouteProps) => {
content: source, content: source,
}) })
setSource('')
refreshSolutions() refreshSolutions()
} }

@ -35,7 +35,7 @@ textarea {
padding: 1rem; padding: 1rem;
box-shadow: -2px 4px 8px 1px #00000010, 0 0 4px 0px #00000010; box-shadow: -2px 4px 6px 1px #00000018, 0 0 4px 0px #00000010;
border-radius: 0.25rem; border-radius: 0.25rem;
background: #ffffff; background: #ffffff;
} }
@ -162,12 +162,12 @@ button {
} }
&.icon { &.icon {
padding: 0.25rem; padding: 0.125rem;
display: grid; display: grid;
place-content: center; place-content: center;
.material-symbols-outlined { .material-symbols-outlined {
font-size: 20px; font-size: 19px;
} }
} }
} }
@ -189,6 +189,10 @@ a:visited {
// Typography // Typography
// //
.dotted {
text-decoration: underline dotted gray;
}
.math-inline { .math-inline {
font-size: 95%; font-size: 95%;
} }
@ -324,12 +328,16 @@ details {
} }
.solution-list { .solution-list {
width: 100%;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
gap: 1rem; gap: 1rem;
.controls {
display: flex;
width: 100%;
gap: 1rem;
}
} }
header { header {
@ -381,7 +389,7 @@ header {
max-width: 80ch; max-width: 80ch;
box-shadow: -2px 4px 8px 1px #00000010, 0 0 4px 0px #00000010; box-shadow: -2px 4px 6px 1px #00000018, 0 0 4px 0px #00000010;
border-radius: 0.5rem; border-radius: 0.5rem;
background: #ffffff; background: #ffffff;
@ -410,12 +418,12 @@ header {
} }
.solution { .solution {
padding: 1rem; padding: 1rem 0.5rem 0.5rem 1rem;
width: 100%; width: 100%;
max-width: 80ch; max-width: 80ch;
box-shadow: -2px 4px 8px 1px #00000010, 0 0 4px 0px #00000010; box-shadow: -2px 4px 6px 1px #00000018, 0 0 4px 0px #00000010;
border-radius: 0.5rem; border-radius: 0.5rem;
background: #ffffff; background: #ffffff;
@ -438,10 +446,30 @@ header {
gap: 0.25rem; gap: 0.25rem;
font-size: 18px; font-size: 18px;
line-height: 1.25;
} }
.solution-content { .solution-content {
@extend .text-body; @extend .text-body;
pre,
code {
margin: 0;
font-family: 'DM Mono', monospace;
font-size: 16px;
background: #f0f0f0;
}
pre {
padding: 0.25rem;
overflow-x: auto;
border-radius: 0.25rem;
}
padding-right: 0.5rem;
} }
.solution-footer { .solution-footer {
@ -550,7 +578,7 @@ header {
padding: 1rem; padding: 1rem;
box-shadow: -2px 4px 8px 1px #00000010, 0 0 4px 0px #00000010; box-shadow: -2px 4px 6px 1px #00000018, 0 0 4px 0px #00000010;
border-radius: 0.25rem; border-radius: 0.25rem;
background: #ffffff; background: #ffffff;
} }
@ -622,6 +650,15 @@ header {
.markdown-editor .preview .preview-content { .markdown-editor .preview .preview-content {
padding: 0.75rem; padding: 0.75rem;
} }
.solution {
.solution-content {
pre {
white-space: pre-line;
word-break: break-all;
}
}
}
} }
@media screen and (max-width: $device-m-width), (pointer: coarse) { @media screen and (max-width: $device-m-width), (pointer: coarse) {

@ -4,3 +4,32 @@ export function validateObjectKeys<K extends string>(obj: any, keys: K[]): boole
// @ts-ignore // @ts-ignore
return Object.keys(obj).every(key => keySet.has(key)) return Object.keys(obj).every(key => keySet.has(key))
} }
function compareStringKeys(a: string, b: string): number {
return a === b ? 0 : a < b ? -1 : 1
}
function compareNumberKeys(a: number, b: number): number {
return a === b ? 0 : a < b ? -1 : 1
}
type KeyFn<T, K> = (item: T) => K
export function sortByStringKey<T>(items: T[], keyFn: KeyFn<T, string>, ascending: boolean = true) {
return sortByKey(compareStringKeys, items, keyFn, ascending)
}
export function sortByNumericKey<T>(items: T[], keyFn: KeyFn<T, number>, ascending: boolean = true) {
return sortByKey(compareNumberKeys, items, keyFn, ascending)
}
function sortByKey<T, K>(
compareFn: (a: K, b: K) => number,
items: T[],
keyFn: (item: T) => K,
ascending: boolean
) {
const sortedList = [...items]
const order = ascending ? 1 : -1
sortedList.sort((a, b) => order * compareFn(keyFn(a), keyFn(b)))
return sortedList
}

@ -1,6 +1,7 @@
{ {
"compilerOptions": { "compilerOptions": {
"composite": true, "composite": true,
"lib": ["ESNext"],
"module": "ESNext", "module": "ESNext",
"moduleResolution": "Node", "moduleResolution": "Node",
"noImplicitAny": true, "noImplicitAny": true,

Loading…
Cancel
Save