/* eslint-disable @next/next/no-img-element */ import Head from "next/head"; import React, { useEffect, useState } from "react"; import { withIronSessionSsr } from "iron-session/next"; import { sessionOptions } from "@/lib/session"; import { shouldRedirectHome } from "@/utils/navigation.disabled"; import { Permission, PermissionType } from "@/interfaces/permissions"; import { getPermissionDoc } from "@/utils/permissions.be"; import { User } from "@/interfaces/user"; import { LayoutContext } from "@/components/High/Layout"; import { getUsers } from "@/utils/users.be"; import { BsTrash } from "react-icons/bs"; import Select from "@/components/Low/Select"; import Button from "@/components/Low/Button"; import axios from "axios"; import { toast, ToastContainer } from "react-toastify"; import { Type as UserType } from "@/interfaces/user"; import { getGroups } from "@/utils/groups.be"; import { requestUser } from "@/utils/api"; import { redirect } from "@/utils"; import { G } from "@react-pdf/renderer"; interface BasicUser { id: string; name: string; type: UserType; } interface PermissionWithBasicUsers { id: string; type: PermissionType; users: BasicUser[]; } export const getServerSideProps = withIronSessionSsr( async ({ req, res, params }) => { const user = await requestUser(req, res); if (!user) return redirect("/login"); if (shouldRedirectHome(user)) return redirect("/"); if (!params?.id) return redirect("/permissions"); // Fetch data from external API const [permission, users, groups] = await Promise.all([ getPermissionDoc(params.id as string), getUsers({}, 0, {}, { _id: 0, id: 1, name: 1, type: 1 }), getGroups(), ]); const userGroups = groups.filter((x) => x.admin === user.id); const userGroupsParticipants = userGroups.flatMap((x) => x.participants); const filteredGroups = user.type === "corporate" ? userGroups : user.type === "mastercorporate" ? groups.filter((x) => userGroupsParticipants.includes(x.admin)) : groups; const filteredGroupsParticipants = filteredGroups.flatMap( (g) => g.participants ); const filteredUsers = ["mastercorporate", "corporate"].includes(user.type) ? users.filter((u) => filteredGroupsParticipants.includes(u.id)) : users; // const res = await fetch("api/permissions"); // const permissions: Permission[] = await res.json(); // Pass data to the page via props const usersData: BasicUser[] = permission.users.reduce( (acc: BasicUser[], userId: any) => { const user = filteredUsers.find((u) => u.id === userId) as BasicUser; if (!!user) acc.push(user); return acc; }, [] ); return { props: { // permissions: permissions.map((p) => ({ id: p.id, type: p.type })), permission: { ...permission, id: params.id, users: usersData, }, user, users: filteredUsers, }, }; }, sessionOptions ); interface Props { permission: PermissionWithBasicUsers; user: User; users: BasicUser[]; } export default function Page(props: Props) { const { permission, user, users } = props; const [selectedUsers, setSelectedUsers] = useState(() => permission.users.map((u) => u.id) ); const onChange = (value: any) => { setSelectedUsers((prev) => { if (value?.value) { return [...prev, value?.value]; } return prev; }); }; const removeUser = (id: string) => { setSelectedUsers((prev) => prev.filter((u) => u !== id)); }; const update = async () => { try { await axios.patch(`/api/permissions/${permission.id}`, { users: selectedUsers, }); toast.success("Permission updated"); } catch (err) { toast.error("Failed to update permission"); } }; const { setClassName } = React.useContext(LayoutContext); useEffect(() => { setClassName("gap-6"); return () => setClassName(""); }, [setClassName]); return ( <> EnCoach <>

Permission: {permission.type as string}