From 4e81c08adb5b18bfbd5203a524e8c81eea0937f2 Mon Sep 17 00:00:00 2001 From: Joao Correia Date: Wed, 22 Jan 2025 18:07:09 +0000 Subject: [PATCH] fix bug in last commit where all entities would show up on select filter instead of user entities --- src/demo/approval_workflows.json | 2 +- src/pages/approval-workflows/index.tsx | 40 +++++++++++++++----------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/demo/approval_workflows.json b/src/demo/approval_workflows.json index dd4f1fdf..226167e9 100644 --- a/src/demo/approval_workflows.json +++ b/src/demo/approval_workflows.json @@ -71,7 +71,7 @@ { "id": "aaaaaakscbka-asacaca-acawesae", "name": "English Exam 2nd Quarter 2025", - "entityId": "85fc76e6-da50-45f6-a1ed-0fe3802ecf02", + "entityId": "64a92896-fa8c-4908-95f3-23ffe05560c5", "modules": [ "reading", "writing", diff --git a/src/pages/approval-workflows/index.tsx b/src/pages/approval-workflows/index.tsx index be1ec4b4..968f2d05 100644 --- a/src/pages/approval-workflows/index.tsx +++ b/src/pages/approval-workflows/index.tsx @@ -2,21 +2,18 @@ import Layout from "@/components/High/Layout"; import Button from "@/components/Low/Button"; import Select from "@/components/Low/Select"; import useApprovalWorkflows from "@/hooks/useApprovalWorkflows"; -import useUser from "@/hooks/useUser"; import { Module, ModuleTypeLabels } from "@/interfaces"; 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 { sessionOptions } from "@/lib/session"; -import { mapBy, redirect, serialize } from "@/utils"; +import { redirect, serialize } from "@/utils"; import { requestUser } from "@/utils/api"; -import { getEntitiesWithRoles } from "@/utils/entities.be"; +import { getEntities } from "@/utils/entities.be"; import { shouldRedirectHome } from "@/utils/navigation.disabled"; -import { findAllowedEntities } from "@/utils/permissions"; -import { isAdmin } from "@/utils/users"; import { getUsers } from "@/utils/users.be"; import { createColumnHelper, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table"; -import axios, { all } from "axios"; +import axios from "axios"; import clsx from "clsx"; import { withIronSessionSsr } from "iron-session/next"; 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)) return redirect("/") - const entityIDS = mapBy(user.entities, "id"); + /* const entityIDS = mapBy(user.entities, "id"); 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 { props: serialize({ user, teachers: await getUsers({ type: "teacher" }) as TeacherUser[], - allowedEntities, + userEntitiesWithLabel: await getEntities(user.entities.map(entity => entity.id)), }), }; }, sessionOptions); @@ -56,7 +53,7 @@ const StatusClassNames: { [key in ApprovalWorkflowStatus]: string } = { }; type CustomStatus = ApprovalWorkflowStatus | "all"; -type CustomEntity = EntityWithRoles["label"] | "all"; +type CustomEntity = EntityWithRoles | "all"; const STATUS_OPTIONS = [ { @@ -84,22 +81,23 @@ const STATUS_OPTIONS = [ interface Props { user: User, 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(teachers); - console.log(allowedEntities); + console.log(userEntitiesWithLabel); const ENTITY_OPTIONS = [ { label: "All", value: "all", - filter: (x: ApprovalWorkflow) => true, + filter: (x: ApprovalWorkflow) => + userEntitiesWithLabel.some(entity => entity.id === x.entityId), }, - ...allowedEntities + ...userEntitiesWithLabel .map(entity => ({ label: entity.label, value: entity.id, @@ -192,6 +190,14 @@ export default function ApprovalWorkflows({ user, teachers, allowedEntities }: P ), }), + columnHelper.accessor("entityId", { + header: "ENTITY", + cell: (info) => ( + + {userEntitiesWithLabel.find((entity) => entity.id === info.getValue())?.label} + + ), + }), columnHelper.accessor("steps", { id: "currentApprovers", header: "CURRENT APPROVERS", @@ -297,7 +303,7 @@ export default function ApprovalWorkflows({ user, teachers, allowedEntities }: P