|
|
|
@ -1,28 +1,36 @@
|
|
|
|
|
import { isAdministrator, Solution as SolutionModel, SolutionStat } from '../../shared/model'
|
|
|
|
|
import { route } from 'preact-router'
|
|
|
|
|
import { isAdministrator, Solution as SolutionModel, SolutionStat, SolutionStatMap, User } from '../../shared/model'
|
|
|
|
|
import { sortByStringKey } from '../../shared/utils'
|
|
|
|
|
import { prependBaseUrl } from '../../shared/utils'
|
|
|
|
|
import { Header } from '../components/Header'
|
|
|
|
|
import { Solution } from '../components/Solution'
|
|
|
|
|
import { useResource } from '../hooks'
|
|
|
|
|
import { useCurrentUser } from '../hooks/useCurrentUser'
|
|
|
|
|
import { useLoggedInUser } from '../hooks/useCurrentUser'
|
|
|
|
|
|
|
|
|
|
type RouteProps = {
|
|
|
|
|
uid: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const UserPage = ({ uid }: RouteProps) => {
|
|
|
|
|
const [user, ready] = useCurrentUser()
|
|
|
|
|
if (!ready) {
|
|
|
|
|
return <></>
|
|
|
|
|
}
|
|
|
|
|
// ottengo l'utente loggato, se è un admin le soluzioni devono mostrare i controlli speciali per admin
|
|
|
|
|
const [currentUser] = useLoggedInUser()
|
|
|
|
|
|
|
|
|
|
// ottengo le info sull'utente di questa pagina
|
|
|
|
|
const [user] = useResource<null | User>(`/api/user/${uid}`, null, user => {
|
|
|
|
|
if (!user) {
|
|
|
|
|
route(prependBaseUrl(`/error?message=${encodeURIComponent(`Non c'è ancora nessun utente con username "${uid}"`)}`), true)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const [stats] = useResource<null | Record<string, SolutionStat | undefined>>(`/api/stats`, null)
|
|
|
|
|
// ottengo gli stat per l'utente di questa pagina
|
|
|
|
|
const [stats] = useResource<null | SolutionStatMap>(`/api/stats`, null)
|
|
|
|
|
if (!stats) {
|
|
|
|
|
return <></>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const userStats = stats[uid]
|
|
|
|
|
|
|
|
|
|
// ottengo le soluzioni pubbliche dell'utente di questa pagina
|
|
|
|
|
const [solutions, refreshSolutions] = useResource<SolutionModel[]>(`/api/solutions?user=${uid}&public`, [])
|
|
|
|
|
const sortedSolutions = sortByStringKey(solutions, s => s.createdAt, false)
|
|
|
|
|
|
|
|
|
@ -48,7 +56,7 @@ export const UserPage = ({ uid }: RouteProps) => {
|
|
|
|
|
<Solution
|
|
|
|
|
refreshSolution={refreshSolutions}
|
|
|
|
|
{...solution}
|
|
|
|
|
adminControls={user !== null && isAdministrator(user.role)}
|
|
|
|
|
adminControls={currentUser !== null && isAdministrator(currentUser.role)}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|