traxxx-web/pages/users/@username/+onBeforeRender.js

32 lines
846 B
JavaScript

import { render } from 'vike/abort'; /* eslint-disable-line import/extensions */
import { fetchUser } from '#/src/users.js';
import { fetchUserStashes } from '#/src/stashes.js';
import { fetchAlerts } from '#/src/alerts.js';
export async function onBeforeRender(pageContext) {
const [profile, alerts] = await Promise.all([
fetchUser(pageContext.routeParams.username, {}, pageContext.user),
pageContext.routeParams.username === pageContext.user?.username
? fetchAlerts(pageContext.user)
: [],
]);
if (!profile) {
throw render(404, `Cannot find user '${pageContext.routeParams.username}'.`);
}
const stashes = await fetchUserStashes(profile.id, pageContext.user);
return {
pageContext: {
title: profile.username,
pageProps: {
profile, // differentiate from authed 'user'
stashes,
alerts,
},
},
};
}