Skip to content

Commit

Permalink
user page
Browse files Browse the repository at this point in the history
  • Loading branch information
wesolowski committed Jun 14, 2024
1 parent 6e5f134 commit 951614c
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 21 deletions.
33 changes: 32 additions & 1 deletion src/components/Flag.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,38 @@ export interface Props {
iso: string;
}
const { iso } = Astro.props as Props;
let { iso } = Astro.props as Props;
const countryMapping = {
ALB: "ALB",
BEL: "BEL",
DEU: "GER",
DNK: "DEN",
ENG: "ENG",
FRA: "FRA",
GEO: "GEO",
ITA: "ITA",
HRV: "CRO",
NLD: "NED",
AUT: "AUT",
POL: "POL",
PRT: "POR",
ROU: "ROU",
SCO: "SCO",
CHE: "SUI",
SRB: "SRB",
SVK: "SVK",
SVN: "SVN",
ESP: "ESP",
CZE: "CZE",
TUR: "TUR",
UKR: "UKR",
HUN: "HUN"
};
if (countryMapping.hasOwnProperty(iso)) {
iso = countryMapping[iso];
}
---

<div class="inline-block w-8 align-bottom ml-4 mr-2">
Expand Down
10 changes: 5 additions & 5 deletions src/components/ShortTable.astro
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const { topThree, userAndNeighbors, langenfeld, mannheim, mainz } = await getRat
{topThree.map((user, index) => (
<tr class={`${index % 2 === 0 ? 'bg-neutral-700' : 'bg-neutral-800'} border-b-2 border-l-2 border-b-black ${user.user_id === userId ? 'border-l-slate-300' : 'border-l-zinc-400'}`}>
<td class="w-1/6 p-4 font-semibold ">{user.position}</td>
<td class={`w-3/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.name}</td>
<td class={`w-3/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}><a href=`/user/${user.user_id}`>{user.name}</a></td>
<td class={`w-1/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.score_sum}</td>
</tr>
))}
Expand All @@ -58,7 +58,7 @@ const { topThree, userAndNeighbors, langenfeld, mannheim, mainz } = await getRat
{userAndNeighbors.map((user, index) => (
<tr class={`bg-neutral-${index % 2 === 0 ? '700' : '800'} border-b-2 border-l-2 border-b-black ${user.user_id === userId ? 'border-l-slate-300' : 'border-l-zinc-400'}`}>
<td class="w-1/6 p-4 font-semibold ">{user.position}</td>
<td class={`w-3/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.name}</td>
<td class={`w-3/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}><a href=`/user/${user.user_id}`>{user.name}</a></td>
<td class={`w-1/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.score_sum}</td>
</tr>
))}
Expand All @@ -79,7 +79,7 @@ const { topThree, userAndNeighbors, langenfeld, mannheim, mainz } = await getRat
{langenfeld.map((user, index) => (
<tr class={`${index % 2 === 0 ? 'bg-neutral-700' : 'bg-neutral-800'} border-b-2 border-l-2 border-b-black ${user.user_id === userId ? 'border-l-slate-300' : 'border-l-zinc-400'}`}>
<td class="w-1/6 p-4 font-semibold ">{user.position}</td>
<td class={`w-3/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.name}</td>
<td class={`w-3/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}><a href=`/user/${user.user_id}`>{user.name}</a></td>
<td class={`w-1/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.score_sum}</td>
</tr>
))}
Expand All @@ -101,7 +101,7 @@ const { topThree, userAndNeighbors, langenfeld, mannheim, mainz } = await getRat
{mannheim.map((user, index) => (
<tr class={`${index % 2 === 0 ? 'bg-neutral-700' : 'bg-neutral-800'} border-b-2 border-l-2 border-b-black ${user.user_id === userId ? 'border-l-slate-300' : 'border-l-zinc-400'}`}>
<td class="w-1/6 p-4 font-semibold ">{user.position}</td>
<td class={`w-3/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.name}</td>
<td class={`w-3/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}><a href=`/user/${user.user_id}`>{user.name}</a></td>
<td class={`w-1/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.score_sum}</td>
</tr>
))}
Expand All @@ -122,7 +122,7 @@ const { topThree, userAndNeighbors, langenfeld, mannheim, mainz } = await getRat
{mainz.map((user, index) => (
<tr class={`${index % 2 === 0 ? 'bg-neutral-700' : 'bg-neutral-800'} border-b-2 border-l-2 border-b-black ${user.user_id === userId ? 'border-l-slate-300' : 'border-l-zinc-400'}`}>
<td class="w-1/6 p-4 font-semibold ">{user.position}</td>
<td class={`w-3/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.name}</td>
<td class={`w-3/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}><a href=`/user/${user.user_id}`>{user.name}</a></td>
<td class={`w-1/6 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.score_sum}</td>
</tr>
))}
Expand Down
6 changes: 5 additions & 1 deletion src/core/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


export default async function fetchApi<T>(endpoint: string): Promise<T> {
export default async function fetchApi<T>(endpoint: string, wrappedByKey?: string): Promise<T> {
if (endpoint.startsWith('/')) {
endpoint = endpoint.slice(1);
}
Expand All @@ -14,5 +14,9 @@ export default async function fetchApi<T>(endpoint: string): Promise<T> {
const res = await fetch(url.toString());
let data = await res.json();

if (wrappedByKey) {
data = data[wrappedByKey];
}

return data as T;
}
27 changes: 23 additions & 4 deletions src/interfaces/table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Tip {
export class UserRating {
name: string;
user_id: number;
department: string;
Expand All @@ -8,15 +8,34 @@ class Tip {
sum_score_diff: number;
sum_team: number;
extra_point: number;
tips: any[];
tips: MatchInfo[];
}

export interface TeamMatch {
name: string;
tla: string;
}

interface MatchInfo {
match_id: string;
user: string;
user_id: number;
score: number;
team1: TeamMatch;
team2: TeamMatch;
tip_home?: number;
tip_away?: number;
score_home?: number;
score_away?: number;
date: number;
}

class Department {
[key: string]: Tip[];
[key: string]: UserRating[];
}

class Table {
global: Tip[];
global: UserRating[];
departments: Department;
}

Expand Down
1 change: 1 addition & 0 deletions src/layouts/LogginLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const currentPath = Astro.url.pathname;
<div class="flex gap-x-12">
<a href="/" class={`text-sm ${currentPath === '/' ? 'pb-1 px-4 border-b-2 border-b-red-500' : 'text-zinc-400'}`}>Dashboard</a>
<a href="/table" class={`text-sm ${currentPath === '/table' ? 'px-4 pb-1 border-b-2 border-b-red-500' : 'text-zinc-400'}`}>Tabelle</a>
<a href=`/user/${user.id}` class={`text-sm ${currentPath === `/user/${user.id}` ? 'px-4 pb-1 border-b-2 border-b-red-500' : 'text-zinc-400'}`}>Mein Konto</a>
<a href="/api/auth/logout" class="text-sm text-zinc-400">Abmelden</a>
</div>
</nav>
Expand Down
12 changes: 6 additions & 6 deletions src/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ export async function getUserById(id: number): Promise<DatabaseUser | undefined>
return db.query.user.findFirst({where: eq(user.id, id)});
}

export async function getAllUser(): Promise<DatabaseUser[] | undefined> {
return db.select().from(user);
}

export async function createUser(newUser: DatabaseUser) {
await db.insert(user).values({
email: newUser.email,
Expand All @@ -28,8 +24,12 @@ export async function createUser(newUser: DatabaseUser) {
});
}

export async function updateUser(newUser: DatabaseUser) {
await db.update(user).set(newUser).where(eq(user.id, newUser.id));
export async function changeWinner(winner, secretWinner, id: number) {
const dbUser = await getUserById(id);
dbUser.winner = winner;
dbUser.secretWinner = secretWinner;

await db.update(user).set(dbUser).where(eq(user.id, id));
}

export async function deleteUser(id: number) {
Expand Down
72 changes: 72 additions & 0 deletions src/pages/api/user/winner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type {APIContext, APIRoute} from "astro";
import {deleteUser, changeWinner} from "../../../lib/user.ts";

export const POST: APIRoute = async (context: APIContext) => {

if(!context.locals.session || !context.locals.session.userId) {
return new Response(
JSON.stringify({
error: "user is not logged in"
}),
{
status: 401,
}
);
}

const deadline = new Date('2024-06-14T21:00:00+02:00');
const now = new Date();

if (now > deadline) {
return new Response(
JSON.stringify({
error: "Deadline has passed"
}),
{
status: 401,
}
);
}
const userId = parseInt(context.locals.session.userId);


const formData = await context.request.formData();

let winner = formData.get("winner")?.toString();
let secretWinner = formData.get("secretWinner")?.toString();

let errors = [];

if (!winner) {
errors.push("Winner is missing");
}
if (!secretWinner) {
errors.push("Secret Winner is missing");
}

if(errors.length > 0) {
return new Response(
JSON.stringify({
error: "Error " + errors.join(", ")
}),
{
status: 401,
});
}



try {
await changeWinner(winner, secretWinner, userId);

} catch (error) {
return new Response(
JSON.stringify({
error: "Error " + error.toString()
}),
{
status: 500,
});
}
return context.redirect("/user/" + userId);
};
12 changes: 12 additions & 0 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,22 @@ const groupedGames = validGames.reduce((acc, game) => {
return acc;
}, {});
const deadline = new Date('2024-06-14T21:00:00+02:00');
---
<LogginLayout title="Dashboard">

<div class="grid grid-cols-1 lg:grid-cols-2 gap-4 pt-5 px-5">
<div>

<p class="text-zinc-400 pb-5">
{new Date() < deadline ? (
<span>Du kannst noch in "Mein Konto" deinen Tipp und Geheimtipp ändern. Das ist nur bis heute um 21:00 Uhr möglich. Sobald das erste Spiel beginnt, kann man das nicht mehr ändern.</span>
<br><br>
) : null}
Du kannst die Ergebnisse der bisherigen Spiele anderer Nutzer sehen, indem du in der Tabelle auf deren Benutzernamen klickst.
</p>

<h1>Tabelle</h1>
<ShortTable />
</div>
Expand All @@ -85,6 +96,7 @@ const groupedGames = validGames.reduce((acc, game) => {
<div>
{Object.entries(groupedGames).map(([date, games]) => (
<>

<div class="info-grid mt-8 pl-4 text-zinc-400 mb-4">
<div class="info-date ">{date}</div>
<div class="info-tip">Dein Tipp</div>
Expand Down
8 changes: 4 additions & 4 deletions src/pages/table.astro
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function abbreviateUsername(username: string) {
{global.map((user, index) => (
<tr class={`${index % 2 === 0 ? 'bg-neutral-700' : 'bg-neutral-800'} border-b-2 border-l-2 border-b-black ${user.user_id === userId ? 'border-l-slate-300' : 'border-l-zinc-400'}`}>
<td class="w-1/12 p-4 font-semibold ">{user.position}</td>
<td class={`w-4/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{abbreviateUsername(user.name)}</td>
<td class={`w-4/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}><a href=`/user/${user.user_id}`>{abbreviateUsername(user.name)}</a></td>
<td class={`w-2/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_win_exact}</td>
<td class={`w-1/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_score_diff}</td>
<td class={`w-1/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_team}</td>
Expand Down Expand Up @@ -129,7 +129,7 @@ function abbreviateUsername(username: string) {
{langenfeld.map((user, index) => (
<tr class={`${index % 2 === 0 ? 'bg-neutral-700' : 'bg-neutral-800'} border-b-2 border-l-2 border-b-black ${user.user_id === userId ? 'border-l-slate-300' : 'border-l-zinc-400'}`}>
<td class="w-1/12 p-4 font-semibold ">{user.position}</td>
<td class={`w-4/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{abbreviateUsername(user.name)}</td>
<td class={`w-4/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}><a href=`/user/${user.user_id}`>{abbreviateUsername(user.name)}</a></td>
<td class={`w-2/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_win_exact}</td>
<td class={`w-1/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_score_diff}</td>
<td class={`w-1/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_team}</td>
Expand Down Expand Up @@ -174,7 +174,7 @@ function abbreviateUsername(username: string) {
{mannheim.map((user, index) => (
<tr class={`${index % 2 === 0 ? 'bg-neutral-700' : 'bg-neutral-800'} border-b-2 border-l-2 border-b-black ${user.user_id === userId ? 'border-l-slate-300' : 'border-l-zinc-400'}`}>
<td class="w-1/12 p-4 font-semibold ">{user.position}</td>
<td class={`w-4/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{abbreviateUsername(user.name)}</td>
<td class={`w-4/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}><a href=`/user/${user.user_id}`>{abbreviateUsername(user.name)}</a></td>
<td class={`w-2/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_win_exact}</td>
<td class={`w-1/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_score_diff}</td>
<td class={`w-1/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_team}</td>
Expand Down Expand Up @@ -218,7 +218,7 @@ function abbreviateUsername(username: string) {
{mainz.map((user, index) => (
<tr class={`${index % 2 === 0 ? 'bg-neutral-700' : 'bg-neutral-800'} border-b-2 border-l-2 border-b-black ${user.user_id === userId ? 'border-l-slate-300' : 'border-l-zinc-400'}`}>
<td class="w-1/12 p-4 font-semibold ">{user.position}</td>
<td class={`w-4/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{abbreviateUsername(user.name)}</td>
<td class={`w-4/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}><a href=`/user/${user.user_id}`>{abbreviateUsername(user.name)}</a></td>
<td class={`w-2/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_win_exact}</td>
<td class={`w-1/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_score_diff}</td>
<td class={`w-1/12 p-4 ${user.user_id === userId && 'text-yellow-100 font-black'}`}>{user.sum_team}</td>
Expand Down
Loading

0 comments on commit 951614c

Please sign in to comment.