Skip to content

Commit

Permalink
feat: create button for importing groups from medlem as options
Browse files Browse the repository at this point in the history
  • Loading branch information
edalholt committed Sep 11, 2024
1 parent b317d64 commit 84ff35b
Show file tree
Hide file tree
Showing 9 changed files with 7,601 additions and 1,802 deletions.
16 changes: 16 additions & 0 deletions backend/controllers/group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Request, Response } from "express";

Check warning on line 1 in backend/controllers/group.ts

View workflow job for this annotation

GitHub Actions / prettier-and-lint (18.x)

'Request' is defined but never used
import { getAllGroups } from "ntnui-tools";
import { RequestWithNtnuiNo } from "../utils/request";

export const getGroups = async (
req: RequestWithNtnuiNo,
res: Response
): Promise<Response> => {
try {
const groups = await getAllGroups(req.body.category);
return res.status(200).json(groups);
} catch (error) {
console.error("Error in getGroups controller:", error);
return res.status(500).json({ message: "Error fetching groups" });
}
};
2 changes: 2 additions & 0 deletions backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import votationRoutes from "./routes/votation";
import { parse } from "url";
import { lobbyWss } from "./wsServers/lobby";
import { organizerWss } from "./wsServers/organizer";
import groupRoutes from "./routes/groups";

dotenv.config();

Expand Down Expand Up @@ -45,6 +46,7 @@ app.use("/user", userRoutes);
app.use("/assembly", assemblyRoutes);
app.use("/qr", qrRoutes);
app.use("/votation", votationRoutes);
app.use("/groups", groupRoutes);

// WebSocket routes
server.on("upgrade", function upgrade(request, socket, head) {
Expand Down
9,274 changes: 7,488 additions & 1,786 deletions backend/package-lock.json

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions backend/routes/groups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Router } from "express";
import { getGroups } from "../controllers/group";

const groupRoutes = Router();

groupRoutes.post("/", getGroups);

export default groupRoutes;
30 changes: 15 additions & 15 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@tabler/icons-react": "^2.10.0",
"@types/js-cookie": "^3.0.2",
"@yudiel/react-qr-scanner": "^1.1.4",
"axios": "^1.4.0",
"axios": "^1.7.7",
"js-cookie": "^3.0.1",
"msw": "^1.2.1",
"protected-route-react": "^1.0.1",
Expand Down
43 changes: 43 additions & 0 deletions frontend/src/components/VotationPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Box,
Flex,
NumberInput,
Menu,
} from "@mantine/core";
import {
activateVotation,
Expand All @@ -22,6 +23,7 @@ import { useStyles } from "../styles/EditAssemblyStyles";
import { VoteType } from "../types/votes";
import { getNumberOfParticipantsInAssembly } from "../services/assembly";
import { showNotification } from "@mantine/notifications";
import { getGroups } from "../services/groups";

export interface CaseType {
caseNumber: number;
Expand Down Expand Up @@ -109,6 +111,20 @@ function VotationPanel({
setIsChanged(!isChanged);
}

const handleImportGroupOptions = async (category: string) => {
const groups = await getGroups(category);
if (Array.isArray(groups)) {
const groupNames = groups.map((group) => group.name);

const newGroupNames = groupNames.filter(
(name) => !options.includes(name)
);
setOptions([...options, ...newGroupNames]);

form.setFieldValue("options", [...form.values.options, ...newGroupNames]);
}
};

async function activateVote(votation: VoteType) {
if (!votation.isFinished) {
await activateVotation(
Expand Down Expand Up @@ -229,6 +245,33 @@ function VotationPanel({
}}
{...form.getInputProps("options")}
/>
<Menu>
<Menu.Target>
<Button
style={{
display: "flex",
justifyContent: "left",
marginTop: "10px",
marginBottom: "10px",
}}
>
<Text>Import options</Text>
</Button>
</Menu.Target>

<Menu.Dropdown>
<Menu.Item
onClick={() => handleImportGroupOptions("committee")}
>
Import committees
</Menu.Item>
<Menu.Item
onClick={() => handleImportGroupOptions("sports_group")}
>
Import sports groups
</Menu.Item>
</Menu.Dropdown>
</Menu>
<NumberInput
data-testid="maxOptionsInput"
withAsterisk
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/services/groups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import axios from "axios";

export const getGroups = async (category?: string): Promise<Group[]> => {
try {
const response = await axios.post(
"/groups",
{ category },
{ withCredentials: true }
);
return response.data;
} catch (error) {
console.error("Error fetching groups:", error);
throw new Error("Failed to fetch groups");
}
};
13 changes: 13 additions & 0 deletions frontend/src/types/group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface Group {

Check warning on line 1 in frontend/src/types/group.ts

View workflow job for this annotation

GitHub Actions / prettier-and-lint (18.x)

'Group' is defined but never used
group_id: number;
name: string;
name_english: string;
slug: string;
gsuite_prefix: string;
subgroups: any[];

Check warning on line 7 in frontend/src/types/group.ts

View workflow job for this annotation

GitHub Actions / prettier-and-lint (18.x)

Unexpected any. Specify a different type
member?: boolean;
access: string;
sent_request?: boolean;
category: string;
website_link: string;
}

0 comments on commit 84ff35b

Please sign in to comment.