Skip to content

Commit

Permalink
Show only users in find-the-expert if they selected that role (#137)
Browse files Browse the repository at this point in the history
* Show only users in find-the-expert if they selected that role

* Add fallback binary target
  • Loading branch information
JurreBrandsenInfoSupport authored May 28, 2024
1 parent a116394 commit 99ce864
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 285 deletions.
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-3.0.x"]
binaryTargets = ["native", "debian-openssl-1.1.x", "debian-openssl-3.0.x"]
}

datasource db {
Expand Down
25 changes: 4 additions & 21 deletions src/app/find-the-expert/[role]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import type { Metadata } from "next";
import type { Session } from "next-auth";

import { Suspense } from "react";
import { ShowRolesWrapper } from "~/app/result/[role]/page";
import ButtonSkeleton from "~/components/loading/button-loader";
import { Login } from "~/components/login";
import ShowDataTable from "~/components/show-data-table";
import { getServerAuthSession } from "~/server/auth";
import {
aggregateDataByRole,
createUserAndAnswerMaps,
extractUniqueIds,
fetchUserAnswersForRole,
fetchUsersAndAnswerOptions,
groupDataByRoleAndQuestion,
sortResults,
} from "~/utils/data-manipulation";

export const metadata: Metadata = {
Expand Down Expand Up @@ -63,26 +60,12 @@ const ShowTableWrapper = async () => {
userIds,
answerIds,
);
const { userMap, answerOptionMap } = createUserAndAnswerMaps(
users,
answerOptions,
);
const dataByRoleAndQuestion = groupDataByRoleAndQuestion(
userAnswersForRole,
userMap,
answerOptionMap,
);
const aggregatedDataByRole = aggregateDataByRole(
userAnswersForRole,
userMap,
answerOptionMap,
);
sortResults(aggregatedDataByRole);

return (
<ShowDataTable
dataByRoleAndQuestion={dataByRoleAndQuestion}
aggregatedDataByRole={aggregatedDataByRole}
userAnswersForRole={userAnswersForRole}
users={users}
answerOptions={answerOptions}
/>
);
};
Expand Down
1 change: 0 additions & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { Button } from "~/components/ui/button";
import { ArrowLeftDarkModeFriendly } from "~/components/svg";
import Link from "next/link";
import { headers } from "next/headers";
import { Github } from "lucide-react";
import GithubLink from "~/components/github-link";

const inter = Inter({
Expand Down
75 changes: 53 additions & 22 deletions src/components/show-data-table.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";

import type { $Enums } from "@prisma/client";
import React from "react";
import { slugify } from "~/utils/slugify";
import {
Expand All @@ -10,32 +10,63 @@ import {
import type { ColumnDef } from "@tanstack/react-table";
import { DataTable } from "./data-table";
import { usePathname } from "next/navigation";
import {
aggregateDataByRole,
createUserAndAnswerMaps,
groupDataByRoleAndQuestion,
sortResults,
} from "~/utils/client-data-manipulation";
import type { UserAnswersForRoleArray } from "~/models/types";

type ShowDataTableProps = {
dataByRoleAndQuestion: Record<
string,
Record<string, { name: string; email: string; answer: string }[]>
>;
aggregatedDataByRole: Record<
string,
Record<
string,
{
name: string;
communicationPreferences: string[];
counts: number[];
}
>
>;
};

const ShowDataTable: React.FC<ShowDataTableProps> = ({
dataByRoleAndQuestion,
aggregatedDataByRole,
const ShowDataTable = ({
userAnswersForRole,
users,
answerOptions,
}: {
userAnswersForRole: UserAnswersForRoleArray;
users: {
name: string | null;
id: string;
roles: {
id: string;
role: string;
default: boolean;
}[];
email: string | null;
communicationPreferences: {
id: string;
userId: string;
methods: $Enums.CommunicationMethod[];
}[];
}[];
answerOptions: { id: string; option: number }[];
}) => {
const pathname = usePathname();

const currentRole = pathname.split("/").pop();

const usersForRole: typeof users = users.filter((user) =>
user.roles.some((role) => slugify(role.role) === currentRole),
);

const { userMap, answerOptionMap } = createUserAndAnswerMaps(
usersForRole,
answerOptions,
);

const dataByRoleAndQuestion = groupDataByRoleAndQuestion(
userAnswersForRole,
userMap,
answerOptionMap,
);

const aggregatedDataByRole = aggregateDataByRole(
userAnswersForRole,
userMap,
answerOptionMap,
);
sortResults(aggregatedDataByRole);

return (
<>
{Object.keys(dataByRoleAndQuestion).length === 0 ? (
Expand Down
2 changes: 2 additions & 0 deletions src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export type DataByRoleAndQuestion = Record<
email: string;
communicationPreferences: string[] | undefined;
answer: string;
roles: string[];
}[]
>
>;
Expand All @@ -136,6 +137,7 @@ export type UserMap = Record<
name: string;
email: string;
communicationPreferences: string[];
roles: string[];
}
>;

Expand Down
Loading

0 comments on commit 99ce864

Please sign in to comment.