Skip to content

Commit 0d38162

Browse files
committed
feat: add coalitions dropdown to header
1 parent 3372925 commit 0d38162

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/handlers/middleware.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import express from 'express';
2-
import expressSession from 'express-session';
32
import bodyParser from 'body-parser';
3+
import NodeCache from 'node-cache';
44
import { Request, Response, NextFunction } from "express";
55
import { CustomSessionData } from "./session";
66
import { ExpressIntraUser } from '../sync/oauth';
77
import { isStaff } from '../utils';
8+
import { prisma } from '../main';
89

910

1011
const checkIfAuthenticated = function(req: Request, res: Response, next: NextFunction) {
@@ -44,6 +45,33 @@ const includeUser = function(req: Request, res: Response, next: NextFunction) {
4445
next();
4546
};
4647

48+
const coalitionCache = new NodeCache({ stdTTL: 3000, checkperiod: 300 });
49+
const includeCoalitions = async function(req: Request, res: Response, next: NextFunction) {
50+
if (coalitionCache.has('coalitions')) {
51+
res.locals.coalitions = coalitionCache.get('coalitions');
52+
return next();
53+
}
54+
const coalitions = await prisma.codamCoalition.findMany({
55+
select: {
56+
id: true,
57+
description: true,
58+
tagline: true,
59+
intra_coalition: {
60+
select: {
61+
id: true,
62+
name: true,
63+
color: true,
64+
image_url: true,
65+
cover_url: true,
66+
},
67+
},
68+
},
69+
});
70+
coalitionCache.set('coalitions', coalitions);
71+
res.locals.coalitions = coalitions;
72+
next();
73+
};
74+
4775
const staffMiddleware = async function(req: Request, res: Response, next: NextFunction) {
4876
const user = req.user as ExpressIntraUser;
4977
if (await isStaff(user)) {
@@ -58,6 +86,7 @@ export const setupExpressMiddleware = function(app: any) {
5886
app.use(bodyParser.urlencoded({ extended: true }));
5987
app.use(checkIfAuthenticated);
6088
app.use(includeUser);
89+
app.use(includeCoalitions);
6190
app.all('/admin*', staffMiddleware); // require staff accounts to access admin routes
6291
app.use(expressErrorHandler); // should remain last
6392
// More middleware for session management and authentication are defined in usePassport in authentication.ts

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import helmet from "helmet";
1010

1111
// Imports for the database connection
1212
import { PrismaClient } from "@prisma/client";
13-
const prisma = new PrismaClient();
13+
export const prisma = new PrismaClient();
1414

1515
// Imports for the Intra API
1616
import Fast42 from '@codam/fast42';

templates/base.njk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@
3838
<li class="nav-item">
3939
<a class="nav-link" aria-current="page" href="/">Home</a>
4040
</li>
41+
<li class="nav-item dropdown">
42+
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
43+
Coalitions
44+
</a>
45+
<ul class="dropdown-menu">
46+
{% for coalition in coalitions %}
47+
<li><a class="dropdown-item" href="/coalitions/{{ coalition.intra_coalition.id }}"><img src="{{ coalition.intra_coalition.image_url }}" alt="" height="28" style="filter: invert(1);"> {{ coalition.intra_coalition.name | striptags(true) | escape }}</a></li>
48+
{% endfor %}
49+
</ul>
50+
</li>
4151
<li class="nav-item">
4252
<a class="nav-link" href="/profile/me">Profile</a>
4353
</li>

0 commit comments

Comments
 (0)