/* eslint-disable @next/next/no-img-element */
import Head from "next/head";
import {withIronSessionSsr} from "iron-session/next";
import {sessionOptions} from "@/lib/session";
import {ToastContainer} from "react-toastify";
import Layout from "@/components/High/Layout";
import {GroupWithUsers, User} from "@/interfaces/user";
import {shouldRedirectHome} from "@/utils/navigation.disabled";
import {getUserName} from "@/utils/users";
import {convertToUsers, getGroupsForEntities} from "@/utils/groups.be";
import {getSpecificUsers} from "@/utils/users.be";
import Link from "next/link";
import {uniq} from "lodash";
import {BsPlus} from "react-icons/bs";
import CardList from "@/components/High/CardList";
import Separator from "@/components/Low/Separator";
import {mapBy} from "@/utils";
export const getServerSideProps = withIronSessionSsr(async ({req}) => {
const user = req.session.user;
if (!user) {
return {
redirect: {
destination: "/login",
permanent: false,
},
};
}
if (shouldRedirectHome(user)) {
return {
redirect: {
destination: "/",
permanent: false,
},
};
}
const entityIDS = mapBy(user.entities, "id");
const groups = await getGroupsForEntities(entityIDS);
const users = await getSpecificUsers(uniq(groups.flatMap((g) => [...g.participants.slice(0, 5), g.admin])));
const groupsWithUsers: GroupWithUsers[] = groups.map((g) => convertToUsers(g, users));
return {
props: {user, groups: JSON.parse(JSON.stringify(groupsWithUsers))},
};
}, sessionOptions);
const SEARCH_FIELDS = [
["name"],
["admin", "name"],
["admin", "email"],
["admin", "corporateInformation", "companyInformation", "name"],
["participants", "name"],
["participants", "email"],
["participants", "corporateInformation", "companyInformation", "name"],
];
interface Props {
user: User;
groups: GroupWithUsers[];
}
export default function Home({user, groups}: Props) {
const renderCard = (group: GroupWithUsers) => (
Group:
{group.name}
Admin:
{getUserName(group.admin)}
Participants ({group.participants.length}):
{group.participants.slice(0, 5).map(getUserName).join(", ")}
{group.participants.length > 5 ? and {group.participants.length - 5} more : ""}
);
const firstCard = () => (