fix: ehm in realtà le pagine utente non funzionavano per niente
This commit is contained in:
@@ -3,10 +3,10 @@ import { Link } from 'preact-router/match'
|
|||||||
import { isAdministrator } from '../../shared/model'
|
import { isAdministrator } from '../../shared/model'
|
||||||
import { prependBaseUrl } from '../../shared/utils'
|
import { prependBaseUrl } from '../../shared/utils'
|
||||||
|
|
||||||
import { useCurrentUser } from '../hooks/useCurrentUser'
|
import { useLoggedInUser } from '../hooks/useCurrentUser'
|
||||||
|
|
||||||
export const Header = ({}) => {
|
export const Header = ({}) => {
|
||||||
const [user] = useCurrentUser()
|
const [user] = useLoggedInUser()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<header>
|
<header>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { JSX } from 'preact/jsx-runtime'
|
|||||||
import { ProblemId, Solution as SolutionModel, SolutionId, SolutionStatus, UserId } from '../../shared/model'
|
import { ProblemId, Solution as SolutionModel, SolutionId, SolutionStatus, UserId } from '../../shared/model'
|
||||||
import { prependBaseUrl } from '../../shared/utils'
|
import { prependBaseUrl } from '../../shared/utils'
|
||||||
import { server } from '../api'
|
import { server } from '../api'
|
||||||
import { useCurrentUser } from '../hooks/useCurrentUser'
|
import { useLoggedInUser } from '../hooks/useCurrentUser'
|
||||||
import { Markdown } from './Markdown'
|
import { Markdown } from './Markdown'
|
||||||
|
|
||||||
const STATUS_SELECT_OPTIONS: Record<SolutionStatus, JSX.Element> = {
|
const STATUS_SELECT_OPTIONS: Record<SolutionStatus, JSX.Element> = {
|
||||||
@@ -37,7 +37,7 @@ export const Solution = ({
|
|||||||
setSolution,
|
setSolution,
|
||||||
refreshSolution,
|
refreshSolution,
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
const [user] = useCurrentUser()
|
const [user] = useLoggedInUser()
|
||||||
|
|
||||||
const markAsCorrect = async () => {
|
const markAsCorrect = async () => {
|
||||||
setSolution?.(prevSolution => ({ ...prevSolution, status: 'correct' }))
|
setSolution?.(prevSolution => ({ ...prevSolution, status: 'correct' }))
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ const UserContext = createContext<UserContextValue | null>(null)
|
|||||||
|
|
||||||
type CurrentUserHook = () => [User | null, boolean]
|
type CurrentUserHook = () => [User | null, boolean]
|
||||||
|
|
||||||
export const useCurrentUser: CurrentUserHook = () => {
|
export const useLoggedInUser: CurrentUserHook = () => {
|
||||||
const userContext = useContext(UserContext)
|
const userContext = useContext(UserContext)
|
||||||
if (!userContext) {
|
if (!userContext) {
|
||||||
return [null, false]
|
return [null, false]
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { MarkdownEditor } from '../components/MarkdownEditor'
|
|||||||
import { Select } from '../components/Select'
|
import { Select } from '../components/Select'
|
||||||
import { Solution } from '../components/Solution'
|
import { Solution } from '../components/Solution'
|
||||||
import { useListResource } from '../hooks'
|
import { useListResource } from '../hooks'
|
||||||
import { useCurrentUser } from '../hooks/useCurrentUser'
|
import { useLoggedInUser } from '../hooks/useCurrentUser'
|
||||||
|
|
||||||
const CreateProblem = ({}) => {
|
const CreateProblem = ({}) => {
|
||||||
const [source, setSource] = useState('')
|
const [source, setSource] = useState('')
|
||||||
@@ -55,7 +55,7 @@ const CreateProblem = ({}) => {
|
|||||||
type SortOrder = 'latest' | 'oldest'
|
type SortOrder = 'latest' | 'oldest'
|
||||||
|
|
||||||
export const AdminPage = ({}) => {
|
export const AdminPage = ({}) => {
|
||||||
const [user, ready] = useCurrentUser()
|
const [user, ready] = useLoggedInUser()
|
||||||
if (!ready) {
|
if (!ready) {
|
||||||
return <></>
|
return <></>
|
||||||
}
|
}
|
||||||
@@ -134,7 +134,7 @@ export const AdminPage = ({}) => {
|
|||||||
<>
|
<>
|
||||||
<hr />
|
<hr />
|
||||||
<div class="subtitle">Controlli speciali solo per Admin</div>
|
<div class="subtitle">Controlli speciali solo per Admin</div>
|
||||||
|
|
||||||
{/* TODO: make this work */}
|
{/* TODO: make this work */}
|
||||||
<div class="scrollable">
|
<div class="scrollable">
|
||||||
<div class="users-table" style={{ '--cols': 4 }}>
|
<div class="users-table" style={{ '--cols': 4 }}>
|
||||||
@@ -145,7 +145,9 @@ export const AdminPage = ({}) => {
|
|||||||
|
|
||||||
{users.map(user => (
|
{users.map(user => (
|
||||||
<>
|
<>
|
||||||
<div class="cell mono">{user.id}</div>
|
<div class="cell">
|
||||||
|
<a href={prependBaseUrl(`/u/${user.id}`)}>@{user.id}</a>
|
||||||
|
</div>
|
||||||
<div class="cell">{user.fullName}</div>
|
<div class="cell">{user.fullName}</div>
|
||||||
<div class="cell">{user.role}</div>
|
<div class="cell">{user.role}</div>
|
||||||
<div class="cell last-col">
|
<div class="cell last-col">
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { MarkdownEditor } from '../components/MarkdownEditor'
|
|||||||
import { Problem } from '../components/Problem'
|
import { Problem } from '../components/Problem'
|
||||||
import { Solution } from '../components/Solution'
|
import { Solution } from '../components/Solution'
|
||||||
import { MetadataContext, useListResource, useResource, ServerContext, DatabaseContext, useServerAsyncCallback } from '../hooks'
|
import { MetadataContext, useListResource, useResource, ServerContext, DatabaseContext, useServerAsyncCallback } from '../hooks'
|
||||||
import { useCurrentUser } from '../hooks/useCurrentUser'
|
import { useLoggedInUser } from '../hooks/useCurrentUser'
|
||||||
|
|
||||||
type RouteProps = {
|
type RouteProps = {
|
||||||
id: string
|
id: string
|
||||||
@@ -27,7 +27,7 @@ export const ProblemPage = ({ id }: RouteProps) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const [user] = useCurrentUser()
|
const [user] = useLoggedInUser()
|
||||||
|
|
||||||
const [source, setSource] = useState('')
|
const [source, setSource] = useState('')
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ import { prependBaseUrl } from '../../shared/utils'
|
|||||||
import { Header } from '../components/Header'
|
import { Header } from '../components/Header'
|
||||||
import { Solution } from '../components/Solution'
|
import { Solution } from '../components/Solution'
|
||||||
import { useResource } from '../hooks'
|
import { useResource } from '../hooks'
|
||||||
import { useCurrentUser, useUserFunctions } from '../hooks/useCurrentUser'
|
import { useLoggedInUser, useUserFunctions } from '../hooks/useCurrentUser'
|
||||||
|
|
||||||
export const ProfilePage = ({}) => {
|
export const ProfilePage = ({}) => {
|
||||||
const [user, ready] = useCurrentUser()
|
const [user, ready] = useLoggedInUser()
|
||||||
if (!ready) {
|
if (!ready) {
|
||||||
return <></>
|
return <></>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 { sortByStringKey } from '../../shared/utils'
|
||||||
import { prependBaseUrl } from '../../shared/utils'
|
import { prependBaseUrl } from '../../shared/utils'
|
||||||
import { Header } from '../components/Header'
|
import { Header } from '../components/Header'
|
||||||
import { Solution } from '../components/Solution'
|
import { Solution } from '../components/Solution'
|
||||||
import { useResource } from '../hooks'
|
import { useResource } from '../hooks'
|
||||||
import { useCurrentUser } from '../hooks/useCurrentUser'
|
import { useLoggedInUser } from '../hooks/useCurrentUser'
|
||||||
|
|
||||||
type RouteProps = {
|
type RouteProps = {
|
||||||
uid: string
|
uid: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const UserPage = ({ uid }: RouteProps) => {
|
export const UserPage = ({ uid }: RouteProps) => {
|
||||||
const [user, ready] = useCurrentUser()
|
// ottengo l'utente loggato, se è un admin le soluzioni devono mostrare i controlli speciali per admin
|
||||||
if (!ready) {
|
const [currentUser] = useLoggedInUser()
|
||||||
return <></>
|
|
||||||
}
|
|
||||||
|
|
||||||
const [stats] = useResource<null | Record<string, SolutionStat | undefined>>(`/api/stats`, null)
|
// 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// ottengo gli stat per l'utente di questa pagina
|
||||||
|
const [stats] = useResource<null | SolutionStatMap>(`/api/stats`, null)
|
||||||
if (!stats) {
|
if (!stats) {
|
||||||
return <></>
|
return <></>
|
||||||
}
|
}
|
||||||
|
|
||||||
const userStats = stats[uid]
|
const userStats = stats[uid]
|
||||||
|
|
||||||
|
// ottengo le soluzioni pubbliche dell'utente di questa pagina
|
||||||
const [solutions, refreshSolutions] = useResource<SolutionModel[]>(`/api/solutions?user=${uid}&public`, [])
|
const [solutions, refreshSolutions] = useResource<SolutionModel[]>(`/api/solutions?user=${uid}&public`, [])
|
||||||
const sortedSolutions = sortByStringKey(solutions, s => s.createdAt, false)
|
const sortedSolutions = sortByStringKey(solutions, s => s.createdAt, false)
|
||||||
|
|
||||||
@@ -48,7 +56,7 @@ export const UserPage = ({ uid }: RouteProps) => {
|
|||||||
<Solution
|
<Solution
|
||||||
refreshSolution={refreshSolutions}
|
refreshSolution={refreshSolutions}
|
||||||
{...solution}
|
{...solution}
|
||||||
adminControls={user !== null && isAdministrator(user.role)}
|
adminControls={currentUser !== null && isAdministrator(currentUser.role)}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+14
-14
@@ -405,29 +405,29 @@ export async function createApiRouter(): Promise<[Router, DatabaseConnection]> {
|
|||||||
})
|
})
|
||||||
|
|
||||||
r.get('/api/user/:id', async (req, res) => {
|
r.get('/api/user/:id', async (req, res) => {
|
||||||
const user = await getRequestUser(req)
|
// const user = await getRequestUser(req)
|
||||||
|
|
||||||
// intanto l'utente deve essere loggato
|
// // intanto l'utente deve essere loggato
|
||||||
if (!user) {
|
// if (!user) {
|
||||||
res.sendStatus(StatusCodes.UNAUTHORIZED)
|
// res.sendStatus(StatusCodes.UNAUTHORIZED)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
// solo gli amministratori possono usare questa route
|
// // solo gli amministratori possono usare questa route
|
||||||
if (!isAdministrator(user.role)) {
|
// if (!isAdministrator(user.role)) {
|
||||||
res.sendStatus(StatusCodes.UNAUTHORIZED)
|
// res.sendStatus(StatusCodes.UNAUTHORIZED)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
const requestedUser = await getUser(db, req.params.id)
|
const user = await getUser(db, req.params.id)
|
||||||
|
|
||||||
// l'utente richiesto magari deve esistere
|
// l'utente richiesto magari deve esistere
|
||||||
if (!requestedUser) {
|
if (!user) {
|
||||||
res.sendStatus(404)
|
res.sendStatus(404)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json(requestedUser)
|
res.json(user)
|
||||||
})
|
})
|
||||||
|
|
||||||
return [r, createDatabaseWrapper(db)]
|
return [r, createDatabaseWrapper(db)]
|
||||||
|
|||||||
@@ -57,6 +57,8 @@ export type SolutionStatus = 'pending' | 'correct' | 'wrong'
|
|||||||
|
|
||||||
export type SolutionId = Id<Solution>
|
export type SolutionId = Id<Solution>
|
||||||
|
|
||||||
|
export type SolutionStatMap = Record<string, SolutionStat | undefined>
|
||||||
|
|
||||||
export type SolutionStat = {
|
export type SolutionStat = {
|
||||||
sentSolutionsCount: number
|
sentSolutionsCount: number
|
||||||
correctSolutionsCount: number
|
correctSolutionsCount: number
|
||||||
|
|||||||
Reference in New Issue
Block a user