ENCOA-18: Improve the loading of the company names on the Group and Users lists
This commit is contained in:
@@ -14,20 +14,30 @@ import {toast} from "react-toastify";
|
||||
import readXlsxFile from "read-excel-file";
|
||||
import {useFilePicker} from "use-file-picker";
|
||||
import {getUserCorporate} from "@/utils/groups";
|
||||
import { isAgentUser, isCorporateUser } from "@/resources/user";
|
||||
|
||||
const columnHelper = createColumnHelper<Group>();
|
||||
const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/);
|
||||
|
||||
const LinkedCorporate = ({userId}: {userId: string}) => {
|
||||
const LinkedCorporate = ({userId, users, groups}: {userId: string, users: User[], groups: Group[]}) => {
|
||||
const [companyName, setCompanyName] = useState("");
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setIsLoading(true);
|
||||
getUserCorporate(userId)
|
||||
.then((result) => setCompanyName(result?.corporateInformation?.companyInformation?.name || ""))
|
||||
.finally(() => setIsLoading(false));
|
||||
}, [userId]);
|
||||
useEffect(() => {
|
||||
const user = users.find((u) => u.id === userId)
|
||||
if (!user) return setCompanyName("")
|
||||
|
||||
if (isCorporateUser(user)) return setCompanyName(user.corporateInformation?.companyInformation?.name || user.name)
|
||||
if (isAgentUser(user)) return setCompanyName(user.agentInformation?.companyName || user.name)
|
||||
|
||||
const belongingGroups = groups.filter((x) => x.participants.includes(userId))
|
||||
const belongingGroupsAdmins = belongingGroups.map((x) => users.find((u) => u.id === x.admin)).filter((x) => !!x && isCorporateUser(x))
|
||||
|
||||
if (belongingGroupsAdmins.length === 0) return setCompanyName("")
|
||||
|
||||
const admin = (belongingGroupsAdmins[0] as CorporateUser)
|
||||
setCompanyName(admin.corporateInformation?.companyInformation.name || admin.name)
|
||||
}, [userId, users, groups]);
|
||||
|
||||
return isLoading ? <span className="animate-pulse">Loading...</span> : <>{companyName}</>;
|
||||
};
|
||||
@@ -224,7 +234,7 @@ export default function GroupList({user}: {user: User}) {
|
||||
}),
|
||||
columnHelper.accessor("admin", {
|
||||
header: "Linked Corporate",
|
||||
cell: (info) => <LinkedCorporate userId={info.getValue()} />,
|
||||
cell: (info) => <LinkedCorporate userId={info.getValue()} users={users} groups={groups} />,
|
||||
}),
|
||||
columnHelper.accessor("participants", {
|
||||
header: "Participants",
|
||||
|
||||
@@ -2,7 +2,7 @@ import Button from "@/components/Low/Button";
|
||||
import {PERMISSIONS} from "@/constants/userPermissions";
|
||||
import useGroups from "@/hooks/useGroups";
|
||||
import useUsers from "@/hooks/useUsers";
|
||||
import {Type, User, userTypes, CorporateUser} from "@/interfaces/user";
|
||||
import {Type, User, userTypes, CorporateUser, Group} from "@/interfaces/user";
|
||||
import {Popover, Transition} from "@headlessui/react";
|
||||
import {createColumnHelper, flexRender, getCoreRowModel, useReactTable} from "@tanstack/react-table";
|
||||
import axios from "axios";
|
||||
@@ -44,16 +44,22 @@ const getCompanyName = async (user: User) => {
|
||||
return "";
|
||||
};
|
||||
|
||||
const CompanyNameCell = (user: User) => {
|
||||
const CompanyNameCell = ({users, user, groups}: {user: User, users: User[], groups: Group[]}) => {
|
||||
const [companyName, setCompanyName] = useState("");
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setIsLoading(true);
|
||||
getCompanyName(user)
|
||||
.then((result) => setCompanyName(result))
|
||||
.finally(() => setIsLoading(false));
|
||||
}, [user]);
|
||||
useEffect(() => {
|
||||
if (isCorporateUser(user)) return setCompanyName(user.corporateInformation?.companyInformation?.name || user.name)
|
||||
if (isAgentUser(user)) return setCompanyName(user.agentInformation?.companyName || user.name)
|
||||
|
||||
const belongingGroups = groups.filter((x) => x.participants.includes(user.id))
|
||||
const belongingGroupsAdmins = belongingGroups.map((x) => users.find((u) => u.id === x.admin)).filter((x) => !!x && isCorporateUser(x))
|
||||
|
||||
if (belongingGroupsAdmins.length === 0) return setCompanyName("")
|
||||
|
||||
const admin = (belongingGroupsAdmins[0] as CorporateUser)
|
||||
setCompanyName(admin.corporateInformation?.companyInformation.name || admin.name)
|
||||
}, [user, users, groups]);
|
||||
|
||||
return isLoading ? <span className="animate-pulse">Loading...</span> : <>{companyName}</>;
|
||||
};
|
||||
@@ -372,7 +378,7 @@ export default function UserList({user, filters = []}: {user: User; filters?: ((
|
||||
<SorterArrow name="companyName" />
|
||||
</button>
|
||||
) as any,
|
||||
cell: (info) => <CompanyNameCell {...info.row.original} />,
|
||||
cell: (info) => <CompanyNameCell user={info.row.original} users={users} groups={groups} />,
|
||||
}),
|
||||
columnHelper.accessor("subscriptionExpirationDate", {
|
||||
header: (
|
||||
|
||||
Reference in New Issue
Block a user