cal.pub0.org/pages/[user].tsx

93 lines
2.8 KiB
TypeScript
Raw Normal View History

import { GetServerSideProps } from "next";
import Head from "next/head";
import Link from "next/link";
import prisma, {whereAndSelect} from "@lib/prisma";
import Avatar from "../components/Avatar";
import Theme from "@components/Theme";
2021-03-22 13:48:48 +00:00
export default function User(props): User {
const { isReady } = Theme(props.user.theme);
const eventTypes = props.eventTypes.map((type) => (
2021-07-09 11:34:00 +00:00
<li
key={type.id}
className="dark:bg-gray-800 dark:opacity-90 dark:hover:opacity-100 dark:hover:bg-gray-800 bg-white hover:bg-gray-50 ">
<Link href={`/${props.user.username}/${type.slug}`}>
<a className="block px-6 py-4">
<div
className="inline-block w-3 h-3 rounded-full mr-2"
style={{ backgroundColor: getRandomColorCode() }}></div>
2021-07-09 11:34:00 +00:00
<h2 className="inline-block font-medium dark:text-white">{type.title}</h2>
<p className="inline-block text-gray-400 dark:text-gray-100 ml-2">{type.description}</p>
</a>
</Link>
</li>
));
return isReady && (
<div>
<Head>
<title>{props.user.name || props.user.username} | Calendso</title>
<link rel="icon" href="/favicon.ico" />
</Head>
2021-03-22 13:48:48 +00:00
<main className="max-w-2xl mx-auto my-24">
<div className="mb-8 text-center">
<Avatar user={props.user} className="mx-auto w-24 h-24 rounded-full mb-4" />
2021-07-09 11:34:00 +00:00
<h1 className="text-3xl font-semibold text-gray-800 dark:text-white mb-1">
{props.user.name || props.user.username}
</h1>
2021-07-09 11:34:00 +00:00
<p className="text-gray-600 dark:text-white">{props.user.bio}</p>
2021-03-22 13:48:48 +00:00
</div>
2021-07-09 11:34:00 +00:00
<div className="shadow overflow-hidden rounded-md">
<ul className="divide-y divide-gray-200 dark:divide-gray-900">{eventTypes}</ul>
{eventTypes.length == 0 && (
2021-07-09 11:34:00 +00:00
<div className="p-8 text-center text-gray-400 dark:text-white">
<h2 className="font-semibold text-3xl text-gray-600">Uh oh!</h2>
<p className="max-w-md mx-auto">This user hasn&apos;t set up any event types yet.</p>
</div>
)}
</div>
</main>
</div>
);
2021-03-22 13:48:48 +00:00
}
export const getServerSideProps: GetServerSideProps = async (context) => {
2021-03-22 13:48:48 +00:00
const user = await whereAndSelect(prisma.user.findFirst, {
username: context.query.user.toLowerCase(),
}, [
"id", "username", "email", "name", "bio", "avatar", "eventTypes", "theme"
]
);
if (!user) {
return {
notFound: true,
};
}
const eventTypes = await prisma.eventType.findMany({
where: {
userId: user.id,
hidden: false,
},
});
2021-04-28 09:23:30 +00:00
return {
props: {
user,
eventTypes,
},
};
};
// Auxiliary methods
export function getRandomColorCode(): string {
let color = "#";
for (let idx = 0; idx < 6; idx++) {
color += Math.floor(Math.random() * 10);
}
return color;
}