feat(route): display profile page instead of "carnet" in member page

This commit is contained in:
Julien Oculi 2024-07-16 16:15:54 +02:00
parent 6fb46b2411
commit f24143964f
4 changed files with 56 additions and 4 deletions

27
routes/carnet/[id].tsx Normal file
View file

@ -0,0 +1,27 @@
import { PageProps } from '$fresh/server.ts'
import { Markdown } from ':components/Markdown.tsx'
import { db } from ':src/db/mod.ts'
import { fetchCarnet } from ':src/members/mod.ts'
export default async function Member(_: Request, { params }: PageProps) {
const uuid = params.id as ReturnType<Crypto['randomUUID']>
const user = await db.resource.user.get({ uuid }).catch(() => undefined)
if (!user) {
return <h3>Membre inconnu, peut être serez vous le prochain</h3>
}
const carnet = await fetchCarnet(user.login)
return (
<Markdown
options={{
baseUrl:
`https://git.cohabit.fr/${user.login}/.carnet/raw/branch/main/index.md`,
allowMath: true,
}}
>
{carnet}
</Markdown>
)
}

View file

@ -2,7 +2,7 @@ import { PageProps } from '$fresh/server.ts'
import { Markdown } from ':components/Markdown.tsx' import { Markdown } from ':components/Markdown.tsx'
import { MemberCard } from ':components/MemberCard.tsx' import { MemberCard } from ':components/MemberCard.tsx'
import { db } from ':src/db/mod.ts' import { db } from ':src/db/mod.ts'
import { fetchCarnet, userToMemberCardProps } from ':src/members/mod.ts' import { fetchProfile, userToMemberCardProps } from ':src/members/mod.ts'
export default async function Member(_: Request, { params, url }: PageProps) { export default async function Member(_: Request, { params, url }: PageProps) {
const uuid = params.id as ReturnType<Crypto['randomUUID']> const uuid = params.id as ReturnType<Crypto['randomUUID']>
@ -13,7 +13,7 @@ export default async function Member(_: Request, { params, url }: PageProps) {
} }
const memberCardProps = await userToMemberCardProps(user) const memberCardProps = await userToMemberCardProps(user)
const carnet = await fetchCarnet(user.login) const carnet = await fetchProfile(user.login)
return ( return (
<div <div
@ -37,6 +37,13 @@ export default async function Member(_: Request, { params, url }: PageProps) {
> >
Portfolio Portfolio
</a> </a>
<br />
<a
href={`/carnet/${uuid}`}
class='cta'
>
Carnet de bord
</a>
</div> </div>
<Markdown <Markdown
options={{ options={{

View file

@ -16,7 +16,9 @@ export default function Profil({ state }: SessionPageProps) {
</section> </section>
<div> <div>
<PassKeyRegister /> <PassKeyRegister />
<Button label='Disconnect' variant='primary'>Disconnect</Button> <Button label='Disconnect' variant='primary'>
Disconnect
</Button>
</div> </div>
</> </>
) )

View file

@ -3,6 +3,22 @@ import { db } from ':src/db/mod.ts'
import { Db } from '@cohabit/resources-manager/db' import { Db } from '@cohabit/resources-manager/db'
import { Ref, User } from '@cohabit/resources-manager/models' import { Ref, User } from '@cohabit/resources-manager/models'
export async function fetchProfile(login: string): Promise<string> {
try {
const response = await fetch(
`https://git.cohabit.fr/${login}/.profile/raw/branch/main/README.md`,
)
if (!response.ok) {
throw new Error(`[${response.status}] "${response.statusText}"`)
}
return response.text()
} catch (error) {
return `# Profil introuvable\n\`\`\`js\n${String(error)}\n\`\`\``
}
}
export async function fetchCarnet(login: string): Promise<string> { export async function fetchCarnet(login: string): Promise<string> {
try { try {
const response = await fetch( const response = await fetch(
@ -38,7 +54,7 @@ export async function userToMemberCardProps(user: User) {
export function dbToMemberCardProps( export function dbToMemberCardProps(
db: Db, db: Db,
): AsyncIterableIterator<MemberCardProps> { ): AsyncIterableIterator<MemberCardProps> {
const memberList = db.ressource.user const memberList = db.resource.user
.list() .list()
.map(userToMemberCardProps) .map(userToMemberCardProps)