/* eslint-disable @next/next/no-img-element */ import CardList from "@/components/High/CardList"; import Layout from "@/components/High/Layout"; import Tooltip from "@/components/Low/Tooltip"; import {useListSearch} from "@/hooks/useListSearch"; import usePagination from "@/hooks/usePagination"; import {Entity, EntityWithRoles, Role} from "@/interfaces/entity"; import {GroupWithUsers, User} from "@/interfaces/user"; import {sessionOptions} from "@/lib/session"; import {USER_TYPE_LABELS} from "@/resources/user"; import {getEntityWithRoles} from "@/utils/entities.be"; import {convertToUsers, getGroup} from "@/utils/groups.be"; import {shouldRedirectHome} from "@/utils/navigation.disabled"; import {checkAccess, getTypesOfUser} from "@/utils/permissions"; import {getUserName} from "@/utils/users"; import {getEntityUsers, getLinkedUsers, getSpecificUsers} from "@/utils/users.be"; import axios from "axios"; import clsx from "clsx"; import {withIronSessionSsr} from "iron-session/next"; import moment from "moment"; import Head from "next/head"; import Link from "next/link"; import {useRouter} from "next/router"; import {Divider} from "primereact/divider"; import {useEffect, useMemo, useState} from "react"; import { BsChevronLeft, BsClockFill, BsEnvelopeFill, BsFillPersonVcardFill, BsPlus, BsSquare, BsStopwatchFill, BsTag, BsTrash, BsX, } from "react-icons/bs"; import {toast, ToastContainer} from "react-toastify"; export const getServerSideProps = withIronSessionSsr(async ({req, params}) => { const user = req.session.user as User; if (!user) { return { redirect: { destination: "/login", permanent: false, }, }; } if (shouldRedirectHome(user)) { return { redirect: { destination: "/", permanent: false, }, }; } const {id} = params as {id: string}; const entityWithRoles = await getEntityWithRoles(id); if (!entityWithRoles || (checkAccess(user, getTypesOfUser(["admin", "developer"])) && !user.entities.map((x) => x.id).includes(id))) { return { redirect: { destination: "/entities", permanent: false, }, }; } const {entity, roles} = entityWithRoles; const linkedUsers = await getLinkedUsers(user.id, user.type); const users = await getEntityUsers(id); return { props: { user, entity: JSON.parse(JSON.stringify(entity)), roles: JSON.parse(JSON.stringify(roles)), users: JSON.parse(JSON.stringify(users)), linkedUsers: JSON.parse(JSON.stringify(linkedUsers.users)), }, }; }, sessionOptions); interface Props { user: User; entity: Entity; roles: Role[]; users: User[]; linkedUsers: User[]; } export default function Home({user, entity, roles, users, linkedUsers}: Props) { const [isEditing, setIsEditing] = useState(false); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); const allowEntityEdit = useMemo(() => checkAccess(user, ["admin", "developer"]), [user]); const renameGroup = () => { if (!allowEntityEdit) return; const name = prompt("Rename this entity:", entity.label); if (!name) return; setIsLoading(true); axios .patch(`/api/entities/${entity.id}`, {name}) .then(() => { toast.success("The entity has been updated successfully!"); router.replace(router.asPath); }) .catch((e) => { console.error(e); toast.error("Something went wrong!"); }) .finally(() => setIsLoading(false)); }; const deleteGroup = () => { if (!allowEntityEdit) return; if (!confirm("Are you sure you want to delete this entity?")) return; setIsLoading(true); axios .delete(`/api/entities/${entity.id}`) .then(() => { toast.success("This entity has been successfully deleted!"); router.replace("/entities"); }) .catch((e) => { console.error(e); toast.error("Something went wrong!"); }) .finally(() => setIsLoading(false)); }; const firstCard = () => ( Create Role ); const renderCard = (role: Role) => { const usersWithRole = users.filter((x) => x.entities.map((x) => x.role).includes(role.id)); return ( ); }; return ( <> {entity.label} | EnCoach {user && (

{entity.label}

{allowEntityEdit && !isEditing && (
)}
Roles
)} ); }