fix bug in last commit where all entities would show up on select filter instead of user entities

This commit is contained in:
Joao Correia
2025-01-22 18:07:09 +00:00
parent 4895f00184
commit 4e81c08adb
2 changed files with 24 additions and 18 deletions

View File

@@ -71,7 +71,7 @@
{ {
"id": "aaaaaakscbka-asacaca-acawesae", "id": "aaaaaakscbka-asacaca-acawesae",
"name": "English Exam 2nd Quarter 2025", "name": "English Exam 2nd Quarter 2025",
"entityId": "85fc76e6-da50-45f6-a1ed-0fe3802ecf02", "entityId": "64a92896-fa8c-4908-95f3-23ffe05560c5",
"modules": [ "modules": [
"reading", "reading",
"writing", "writing",

View File

@@ -2,21 +2,18 @@ import Layout from "@/components/High/Layout";
import Button from "@/components/Low/Button"; import Button from "@/components/Low/Button";
import Select from "@/components/Low/Select"; import Select from "@/components/Low/Select";
import useApprovalWorkflows from "@/hooks/useApprovalWorkflows"; import useApprovalWorkflows from "@/hooks/useApprovalWorkflows";
import useUser from "@/hooks/useUser";
import { Module, ModuleTypeLabels } from "@/interfaces"; import { Module, ModuleTypeLabels } from "@/interfaces";
import { ApprovalWorkflow, ApprovalWorkflowStatus, ApprovalWorkflowStatusLabel, StepTypeLabel } from "@/interfaces/approval.workflow"; import { ApprovalWorkflow, ApprovalWorkflowStatus, ApprovalWorkflowStatusLabel, StepTypeLabel } from "@/interfaces/approval.workflow";
import { EntityWithRoles } from "@/interfaces/entity"; import { Entity, EntityWithRoles } from "@/interfaces/entity";
import { TeacherUser, User } from "@/interfaces/user"; import { TeacherUser, User } from "@/interfaces/user";
import { sessionOptions } from "@/lib/session"; import { sessionOptions } from "@/lib/session";
import { mapBy, redirect, serialize } from "@/utils"; import { redirect, serialize } from "@/utils";
import { requestUser } from "@/utils/api"; import { requestUser } from "@/utils/api";
import { getEntitiesWithRoles } from "@/utils/entities.be"; import { getEntities } from "@/utils/entities.be";
import { shouldRedirectHome } from "@/utils/navigation.disabled"; import { shouldRedirectHome } from "@/utils/navigation.disabled";
import { findAllowedEntities } from "@/utils/permissions";
import { isAdmin } from "@/utils/users";
import { getUsers } from "@/utils/users.be"; import { getUsers } from "@/utils/users.be";
import { createColumnHelper, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table"; import { createColumnHelper, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table";
import axios, { all } from "axios"; import axios from "axios";
import clsx from "clsx"; import clsx from "clsx";
import { withIronSessionSsr } from "iron-session/next"; import { withIronSessionSsr } from "iron-session/next";
import Head from "next/head"; import Head from "next/head";
@@ -36,15 +33,15 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
if (shouldRedirectHome(user) || !["admin", "developer", "teacher", "corporate", "mastercorporate"].includes(user.type)) if (shouldRedirectHome(user) || !["admin", "developer", "teacher", "corporate", "mastercorporate"].includes(user.type))
return redirect("/") return redirect("/")
const entityIDS = mapBy(user.entities, "id"); /* const entityIDS = mapBy(user.entities, "id");
const entities = await getEntitiesWithRoles(isAdmin(user) ? undefined : entityIDS); const entities = await getEntitiesWithRoles(isAdmin(user) ? undefined : entityIDS);
const allowedEntities = findAllowedEntities(user, entities, "view_approval_workflows"); const allowedEntities = findAllowedEntities(user, entities, "view_approval_workflows"); */
return { return {
props: serialize({ props: serialize({
user, user,
teachers: await getUsers({ type: "teacher" }) as TeacherUser[], teachers: await getUsers({ type: "teacher" }) as TeacherUser[],
allowedEntities, userEntitiesWithLabel: await getEntities(user.entities.map(entity => entity.id)),
}), }),
}; };
}, sessionOptions); }, sessionOptions);
@@ -56,7 +53,7 @@ const StatusClassNames: { [key in ApprovalWorkflowStatus]: string } = {
}; };
type CustomStatus = ApprovalWorkflowStatus | "all"; type CustomStatus = ApprovalWorkflowStatus | "all";
type CustomEntity = EntityWithRoles["label"] | "all"; type CustomEntity = EntityWithRoles | "all";
const STATUS_OPTIONS = [ const STATUS_OPTIONS = [
{ {
@@ -84,22 +81,23 @@ const STATUS_OPTIONS = [
interface Props { interface Props {
user: User, user: User,
teachers: TeacherUser[], teachers: TeacherUser[],
allowedEntities: EntityWithRoles[], userEntitiesWithLabel: Entity[],
} }
export default function ApprovalWorkflows({ user, teachers, allowedEntities }: Props) { export default function ApprovalWorkflows({ user, teachers, userEntitiesWithLabel }: Props) {
console.log(user); console.log(user);
console.log(teachers); console.log(teachers);
console.log(allowedEntities); console.log(userEntitiesWithLabel);
const ENTITY_OPTIONS = [ const ENTITY_OPTIONS = [
{ {
label: "All", label: "All",
value: "all", value: "all",
filter: (x: ApprovalWorkflow) => true, filter: (x: ApprovalWorkflow) =>
userEntitiesWithLabel.some(entity => entity.id === x.entityId),
}, },
...allowedEntities ...userEntitiesWithLabel
.map(entity => ({ .map(entity => ({
label: entity.label, label: entity.label,
value: entity.id, value: entity.id,
@@ -192,6 +190,14 @@ export default function ApprovalWorkflows({ user, teachers, allowedEntities }: P
</span> </span>
), ),
}), }),
columnHelper.accessor("entityId", {
header: "ENTITY",
cell: (info) => (
<span className="font-medium">
{userEntitiesWithLabel.find((entity) => entity.id === info.getValue())?.label}
</span>
),
}),
columnHelper.accessor("steps", { columnHelper.accessor("steps", {
id: "currentApprovers", id: "currentApprovers",
header: "CURRENT APPROVERS", header: "CURRENT APPROVERS",
@@ -297,7 +303,7 @@ export default function ApprovalWorkflows({ user, teachers, allowedEntities }: P
<Select <Select
options={ENTITY_OPTIONS} options={ENTITY_OPTIONS}
value={ENTITY_OPTIONS.find((x) => x.value === entityFilter)} value={ENTITY_OPTIONS.find((x) => x.value === entityFilter)}
onChange={(value) => setEntityFilter((value?.value as string) ?? undefined)} onChange={(value) => setEntityFilter((value?.value as CustomEntity) ?? undefined)}
isClearable isClearable
placeholder="Entity..." placeholder="Entity..."
/> />