diff --git a/client/components/Select.tsx b/client/components/Select.tsx index 39e2045..b219464 100644 --- a/client/components/Select.tsx +++ b/client/components/Select.tsx @@ -1,23 +1,31 @@ import { StateUpdater } from 'preact/hooks' import { JSX } from 'preact/jsx-runtime' -type Props = { - options: Record - value: string - setValue?: StateUpdater +type Props = { + options: Record + value: T + setValue?: StateUpdater } -export const Select = ({ options, value, setValue }: Props) => ( -
- - expand_more -
-) +export const Select = ({ options, value, setValue }: Props) => { + const optionValues = new Set(Object.keys(options)) + + return ( +
+ + expand_more +
+ ) +} diff --git a/client/components/Solution.tsx b/client/components/Solution.tsx index 774ffce..e426d88 100644 --- a/client/components/Solution.tsx +++ b/client/components/Solution.tsx @@ -1,3 +1,4 @@ +import { useState } from 'preact/hooks' import { JSX } from 'preact/jsx-runtime' import { MetadataProps, @@ -19,6 +20,7 @@ const STATUS_SELECT_OPTIONS: Record = { type Props = { id: SolutionId + createdAt: string sentBy?: UserId forProblem: ProblemId content: string @@ -31,6 +33,7 @@ type Props = { export const Solution = ({ id, + createdAt, sentBy, forProblem, content, @@ -70,6 +73,14 @@ export const Solution = ({ refreshSolution?.() } + const [viewRaw, setViewRaw] = useState(false) + + const toggleViewRaw = () => { + setViewRaw(prev => !prev) + } + + const d = new Date(createdAt) + return (
@@ -87,35 +98,38 @@ export const Solution = ({ 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 && ( + + {sortedSolutions.map( + (s, index) => + (s.status === 'pending' || trackInteracted.has(s.id)) && ( + { + setTrackedInteracted(prev => new Set([...prev, s.id])) + setSolutionHeuristic(index, solFn) + }} + refreshSolution={refreshSolutions} + /> + ) + )} + + ) : ( + <> + Nessuna soluzione ancora da correggere + + )} ) ) diff --git a/client/pages/Home.tsx b/client/pages/Home.tsx index 5ce87ac..2a5e066 100644 --- a/client/pages/Home.tsx +++ b/client/pages/Home.tsx @@ -1,17 +1,40 @@ import { route } from 'preact-router' import { useEffect, useState } from 'preact/hooks' import { Problem as ProblemModel } from '../../shared/model' +import { sortByNumericKey, sortByStringKey } from '../../shared/utils' import { Header } from '../components/Header' import { Problem } from '../components/Problem' import { Select } from '../components/Select' 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 = { + 'latest': false, + 'oldest': true, + 'top-solved': false, + 'least-solved': true, +} + export const HomePage = () => { const [user] = useCurrentUser() - const [problems] = useResource('/api/problems', []) + const [problems] = useResource<(ProblemModel & { solutionsCount: number })[]>('/api/problems', []) + + const [sortOrder, setSortOrder] = useState('oldest') - console.log(problems) + const sortedProblems = + sortOrder === 'latest' || sortOrder === 'oldest' + ? sortByStringKey(problems, byTime, SORT_ORDER[sortOrder]) + : sortByNumericKey(problems, bySolvedProblems, SORT_ORDER[sortOrder]) return (
@@ -20,7 +43,8 @@ export const HomePage = () => {