Improved the performance of the MasterCorporate
This commit is contained in:
@@ -36,8 +36,7 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
const [selectedUser, setSelectedUser] = useState<User>();
|
const [selectedUser, setSelectedUser] = useState<User>();
|
||||||
const [showModal, setShowModal] = useState(false);
|
const [showModal, setShowModal] = useState(false);
|
||||||
|
|
||||||
const {data: stats} = useFilterRecordsByUser<Stat[]>(user.id);
|
const {users, reload, isLoading} = useUsers();
|
||||||
const {users, reload} = useUsers();
|
|
||||||
const {groups} = useGroups({});
|
const {groups} = useGroups({});
|
||||||
const {pending, done} = usePaymentStatusUsers();
|
const {pending, done} = usePaymentStatusUsers();
|
||||||
|
|
||||||
@@ -280,6 +279,7 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
<section className="w-full grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 place-items-center items-center justify-between">
|
<section className="w-full grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 place-items-center items-center justify-between">
|
||||||
<IconCard
|
<IconCard
|
||||||
Icon={BsPersonFill}
|
Icon={BsPersonFill}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Students"
|
label="Students"
|
||||||
value={users.filter((x) => x.type === "student").length}
|
value={users.filter((x) => x.type === "student").length}
|
||||||
onClick={() => router.push("/#students")}
|
onClick={() => router.push("/#students")}
|
||||||
@@ -287,6 +287,7 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
Icon={BsPencilSquare}
|
Icon={BsPencilSquare}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Teachers"
|
label="Teachers"
|
||||||
value={users.filter((x) => x.type === "teacher").length}
|
value={users.filter((x) => x.type === "teacher").length}
|
||||||
onClick={() => router.push("/#teachers")}
|
onClick={() => router.push("/#teachers")}
|
||||||
@@ -294,6 +295,7 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
Icon={BsBank}
|
Icon={BsBank}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Corporate"
|
label="Corporate"
|
||||||
value={users.filter((x) => x.type === "corporate").length}
|
value={users.filter((x) => x.type === "corporate").length}
|
||||||
onClick={() => router.push("/#corporate")}
|
onClick={() => router.push("/#corporate")}
|
||||||
@@ -301,6 +303,7 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
Icon={BsBriefcaseFill}
|
Icon={BsBriefcaseFill}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Country Managers"
|
label="Country Managers"
|
||||||
value={users.filter((x) => x.type === "agent").length}
|
value={users.filter((x) => x.type === "agent").length}
|
||||||
onClick={() => router.push("/#agents")}
|
onClick={() => router.push("/#agents")}
|
||||||
@@ -308,6 +311,7 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
Icon={BsGlobeCentralSouthAsia}
|
Icon={BsGlobeCentralSouthAsia}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Countries"
|
label="Countries"
|
||||||
value={[...new Set(users.filter((x) => x.demographicInformation).map((x) => x.demographicInformation?.country))].length}
|
value={[...new Set(users.filter((x) => x.demographicInformation).map((x) => x.demographicInformation?.country))].length}
|
||||||
color="purple"
|
color="purple"
|
||||||
@@ -315,6 +319,7 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
<IconCard
|
<IconCard
|
||||||
onClick={() => router.push("/#inactiveStudents")}
|
onClick={() => router.push("/#inactiveStudents")}
|
||||||
Icon={BsPersonFill}
|
Icon={BsPersonFill}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Inactive Students"
|
label="Inactive Students"
|
||||||
value={
|
value={
|
||||||
users.filter((x) => x.type === "student" && (x.status === "disabled" || moment().isAfter(x.subscriptionExpirationDate)))
|
users.filter((x) => x.type === "student" && (x.status === "disabled" || moment().isAfter(x.subscriptionExpirationDate)))
|
||||||
@@ -325,6 +330,7 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
<IconCard
|
<IconCard
|
||||||
onClick={() => router.push("/#inactiveCountryManagers")}
|
onClick={() => router.push("/#inactiveCountryManagers")}
|
||||||
Icon={BsBriefcaseFill}
|
Icon={BsBriefcaseFill}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Inactive Country Managers"
|
label="Inactive Country Managers"
|
||||||
value={users.filter(inactiveCountryManagerFilter).length}
|
value={users.filter(inactiveCountryManagerFilter).length}
|
||||||
color="rose"
|
color="rose"
|
||||||
@@ -332,6 +338,7 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
<IconCard
|
<IconCard
|
||||||
onClick={() => router.push("/#inactiveCorporate")}
|
onClick={() => router.push("/#inactiveCorporate")}
|
||||||
Icon={BsBank}
|
Icon={BsBank}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Inactive Corporate"
|
label="Inactive Corporate"
|
||||||
value={
|
value={
|
||||||
users.filter((x) => x.type === "corporate" && (x.status === "disabled" || moment().isAfter(x.subscriptionExpirationDate)))
|
users.filter((x) => x.type === "corporate" && (x.status === "disabled" || moment().isAfter(x.subscriptionExpirationDate)))
|
||||||
@@ -342,6 +349,7 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
<IconCard
|
<IconCard
|
||||||
onClick={() => router.push("/#paymentdone")}
|
onClick={() => router.push("/#paymentdone")}
|
||||||
Icon={BsCurrencyDollar}
|
Icon={BsCurrencyDollar}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Payment Done"
|
label="Payment Done"
|
||||||
value={done.length}
|
value={done.length}
|
||||||
color="purple"
|
color="purple"
|
||||||
@@ -349,6 +357,7 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
<IconCard
|
<IconCard
|
||||||
onClick={() => router.push("/#paymentpending")}
|
onClick={() => router.push("/#paymentpending")}
|
||||||
Icon={BsCurrencyDollar}
|
Icon={BsCurrencyDollar}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Pending Payment"
|
label="Pending Payment"
|
||||||
value={pending.length}
|
value={pending.length}
|
||||||
color="rose"
|
color="rose"
|
||||||
@@ -356,12 +365,14 @@ export default function AdminDashboard({user}: Props) {
|
|||||||
<IconCard
|
<IconCard
|
||||||
onClick={() => router.push("https://cms.encoach.com/admin")}
|
onClick={() => router.push("https://cms.encoach.com/admin")}
|
||||||
Icon={BsLayoutSidebar}
|
Icon={BsLayoutSidebar}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Content Management System (CMS)"
|
label="Content Management System (CMS)"
|
||||||
color="green"
|
color="green"
|
||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
onClick={() => router.push("/#corporatestudentslevels")}
|
onClick={() => router.push("/#corporatestudentslevels")}
|
||||||
Icon={BsPersonFill}
|
Icon={BsPersonFill}
|
||||||
|
isLoading={isLoading}
|
||||||
label="Corporate Students Levels"
|
label="Corporate Students Levels"
|
||||||
color="purple"
|
color="purple"
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,703 +0,0 @@
|
|||||||
/* eslint-disable @next/next/no-img-element */
|
|
||||||
import Modal from "@/components/Modal";
|
|
||||||
import useFilterRecordsByUser from "@/hooks/useFilterRecordsByUser";
|
|
||||||
import useUsers, { userHashStudent, userHashTeacher, userHashCorporate } from "@/hooks/useUsers";
|
|
||||||
import {CorporateUser, Group, MasterCorporateUser, Stat, User} from "@/interfaces/user";
|
|
||||||
import UserList from "@/pages/(admin)/Lists/UserList";
|
|
||||||
import {dateSorter} from "@/utils";
|
|
||||||
import moment from "moment";
|
|
||||||
import {useEffect, useState, useMemo} from "react";
|
|
||||||
import {
|
|
||||||
BsArrowLeft,
|
|
||||||
BsClipboard2Data,
|
|
||||||
BsClock,
|
|
||||||
BsPaperclip,
|
|
||||||
BsPersonFill,
|
|
||||||
BsPencilSquare,
|
|
||||||
BsPersonCheck,
|
|
||||||
BsPeople,
|
|
||||||
BsBank,
|
|
||||||
BsEnvelopePaper,
|
|
||||||
BsArrowRepeat,
|
|
||||||
BsPlus,
|
|
||||||
BsPersonFillGear,
|
|
||||||
BsFilter,
|
|
||||||
BsDatabase,
|
|
||||||
} from "react-icons/bs";
|
|
||||||
import UserCard from "@/components/UserCard";
|
|
||||||
import useGroups from "@/hooks/useGroups";
|
|
||||||
|
|
||||||
import {averageLevelCalculator, calculateAverageLevel, calculateBandScore} from "@/utils/score";
|
|
||||||
import {MODULE_ARRAY} from "@/utils/moduleUtils";
|
|
||||||
import {Module} from "@/interfaces";
|
|
||||||
import {groupByExam} from "@/utils/stats";
|
|
||||||
import IconCard from "./IconCard";
|
|
||||||
import GroupList from "@/pages/(admin)/Lists/GroupList";
|
|
||||||
import useFilterStore from "@/stores/listFilterStore";
|
|
||||||
import {useRouter} from "next/router";
|
|
||||||
import useCodes from "@/hooks/useCodes";
|
|
||||||
import useAssignments from "@/hooks/useAssignments";
|
|
||||||
import {Assignment} from "@/interfaces/results";
|
|
||||||
import AssignmentView from "./AssignmentView";
|
|
||||||
import AssignmentCreator from "./AssignmentCreator";
|
|
||||||
import clsx from "clsx";
|
|
||||||
import AssignmentCard from "./AssignmentCard";
|
|
||||||
import {createColumn, createColumnHelper} from "@tanstack/react-table";
|
|
||||||
import List from "@/components/List";
|
|
||||||
import {getUserCorporate} from "@/utils/groups";
|
|
||||||
import {getCorporateUser, getUserCompanyName} from "@/resources/user";
|
|
||||||
import Checkbox from "@/components/Low/Checkbox";
|
|
||||||
import {groupBy, uniq, uniqBy} from "lodash";
|
|
||||||
import Select from "@/components/Low/Select";
|
|
||||||
import {Menu, MenuButton, MenuItem, MenuItems} from "@headlessui/react";
|
|
||||||
import {Popover, PopoverContent, PopoverTrigger} from "@/components/ui/popover";
|
|
||||||
import MasterStatistical from "./MasterStatistical";
|
|
||||||
import {futureAssignmentFilter, pastAssignmentFilter, archivedAssignmentFilter, activeAssignmentFilter} from "@/utils/assignments";
|
|
||||||
import useUserBalance from "@/hooks/useUserBalance";
|
|
||||||
import AssignmentsPage from "./views/AssignmentsPage";
|
|
||||||
import { group } from "console";
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
user: MasterCorporateUser;
|
|
||||||
}
|
|
||||||
|
|
||||||
type StudentPerformanceItem = User & {
|
|
||||||
corporate?: CorporateUser;
|
|
||||||
group?: Group;
|
|
||||||
};
|
|
||||||
const StudentPerformanceList = ({items, stats, users, groups}: {items: StudentPerformanceItem[]; stats: Stat[]; users: User[]; groups: Group[]}) => {
|
|
||||||
const [isShowingAmount, setIsShowingAmount] = useState(false);
|
|
||||||
const [availableCorporates] = useState(
|
|
||||||
uniqBy(
|
|
||||||
items.map((x) => x.corporate),
|
|
||||||
"id",
|
|
||||||
),
|
|
||||||
);
|
|
||||||
const [availableGroups] = useState(
|
|
||||||
uniqBy(
|
|
||||||
items.map((x) => x.group),
|
|
||||||
"id",
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
const [selectedCorporate, setSelectedCorporate] = useState<CorporateUser | null | undefined>(null);
|
|
||||||
const [selectedGroup, setSelectedGroup] = useState<Group | null | undefined>(null);
|
|
||||||
|
|
||||||
const columnHelper = createColumnHelper<StudentPerformanceItem>();
|
|
||||||
|
|
||||||
const columns = [
|
|
||||||
columnHelper.accessor("name", {
|
|
||||||
header: "Student Name",
|
|
||||||
cell: (info) => info.getValue(),
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("email", {
|
|
||||||
header: "E-mail",
|
|
||||||
cell: (info) => info.getValue(),
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("demographicInformation.passport_id", {
|
|
||||||
header: "ID",
|
|
||||||
cell: (info) => info.getValue() || "N/A",
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("group", {
|
|
||||||
header: "Group",
|
|
||||||
cell: (info) => info.getValue()?.name || "N/A",
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("corporate", {
|
|
||||||
header: "Corporate",
|
|
||||||
cell: (info) => (!!info.getValue() ? getUserCompanyName(info.getValue() as User, users, groups) : "N/A"),
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("levels.reading", {
|
|
||||||
header: "Reading",
|
|
||||||
cell: (info) =>
|
|
||||||
!isShowingAmount
|
|
||||||
? calculateBandScore(
|
|
||||||
stats
|
|
||||||
.filter((x) => x.module === "reading" && x.user === info.row.original.id)
|
|
||||||
.reduce((acc, curr) => acc + curr.score.correct, 0),
|
|
||||||
stats
|
|
||||||
.filter((x) => x.module === "reading" && x.user === info.row.original.id)
|
|
||||||
.reduce((acc, curr) => acc + curr.score.total, 0),
|
|
||||||
"level",
|
|
||||||
info.row.original.focus || "academic",
|
|
||||||
) || 0
|
|
||||||
: `${Object.keys(groupByExam(stats.filter((x) => x.module === "reading" && x.user === info.row.original.id))).length} exams`,
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("levels.listening", {
|
|
||||||
header: "Listening",
|
|
||||||
cell: (info) =>
|
|
||||||
!isShowingAmount
|
|
||||||
? calculateBandScore(
|
|
||||||
stats
|
|
||||||
.filter((x) => x.module === "listening" && x.user === info.row.original.id)
|
|
||||||
.reduce((acc, curr) => acc + curr.score.correct, 0),
|
|
||||||
stats
|
|
||||||
.filter((x) => x.module === "listening" && x.user === info.row.original.id)
|
|
||||||
.reduce((acc, curr) => acc + curr.score.total, 0),
|
|
||||||
"level",
|
|
||||||
info.row.original.focus || "academic",
|
|
||||||
) || 0
|
|
||||||
: `${Object.keys(groupByExam(stats.filter((x) => x.module === "listening" && x.user === info.row.original.id))).length} exams`,
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("levels.writing", {
|
|
||||||
header: "Writing",
|
|
||||||
cell: (info) =>
|
|
||||||
!isShowingAmount
|
|
||||||
? calculateBandScore(
|
|
||||||
stats
|
|
||||||
.filter((x) => x.module === "writing" && x.user === info.row.original.id)
|
|
||||||
.reduce((acc, curr) => acc + curr.score.correct, 0),
|
|
||||||
stats
|
|
||||||
.filter((x) => x.module === "writing" && x.user === info.row.original.id)
|
|
||||||
.reduce((acc, curr) => acc + curr.score.total, 0),
|
|
||||||
"level",
|
|
||||||
info.row.original.focus || "academic",
|
|
||||||
) || 0
|
|
||||||
: `${Object.keys(groupByExam(stats.filter((x) => x.module === "writing" && x.user === info.row.original.id))).length} exams`,
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("levels.speaking", {
|
|
||||||
header: "Speaking",
|
|
||||||
cell: (info) =>
|
|
||||||
!isShowingAmount
|
|
||||||
? calculateBandScore(
|
|
||||||
stats
|
|
||||||
.filter((x) => x.module === "speaking" && x.user === info.row.original.id)
|
|
||||||
.reduce((acc, curr) => acc + curr.score.correct, 0),
|
|
||||||
stats
|
|
||||||
.filter((x) => x.module === "speaking" && x.user === info.row.original.id)
|
|
||||||
.reduce((acc, curr) => acc + curr.score.total, 0),
|
|
||||||
"level",
|
|
||||||
info.row.original.focus || "academic",
|
|
||||||
) || 0
|
|
||||||
: `${Object.keys(groupByExam(stats.filter((x) => x.module === "speaking" && x.user === info.row.original.id))).length} exams`,
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("levels.level", {
|
|
||||||
header: "Level",
|
|
||||||
cell: (info) =>
|
|
||||||
!isShowingAmount
|
|
||||||
? calculateBandScore(
|
|
||||||
stats
|
|
||||||
.filter((x) => x.module === "level" && x.user === info.row.original.id)
|
|
||||||
.reduce((acc, curr) => acc + curr.score.correct, 0),
|
|
||||||
stats
|
|
||||||
.filter((x) => x.module === "level" && x.user === info.row.original.id)
|
|
||||||
.reduce((acc, curr) => acc + curr.score.total, 0),
|
|
||||||
"level",
|
|
||||||
info.row.original.focus || "academic",
|
|
||||||
) || 0
|
|
||||||
: `${Object.keys(groupByExam(stats.filter((x) => x.module === "level" && x.user === info.row.original.id))).length} exams`,
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("levels", {
|
|
||||||
id: "overall_level",
|
|
||||||
header: "Overall",
|
|
||||||
cell: (info) =>
|
|
||||||
!isShowingAmount
|
|
||||||
? averageLevelCalculator(
|
|
||||||
users,
|
|
||||||
stats.filter((x) => x.user === info.row.original.id),
|
|
||||||
).toFixed(1)
|
|
||||||
: `${Object.keys(groupByExam(stats.filter((x) => x.user === info.row.original.id))).length} exams`,
|
|
||||||
}),
|
|
||||||
];
|
|
||||||
|
|
||||||
const filterUsers = (data: StudentPerformanceItem[]) => {
|
|
||||||
const filterByCorporate = (item: StudentPerformanceItem) => item.corporate?.id === selectedCorporate?.id;
|
|
||||||
const filterByGroup = (item: StudentPerformanceItem) => item.group?.id === selectedGroup?.id;
|
|
||||||
|
|
||||||
const filters: ((item: StudentPerformanceItem) => boolean)[] = [];
|
|
||||||
if (selectedCorporate !== null) filters.push(filterByCorporate);
|
|
||||||
if (selectedGroup !== null) filters.push(filterByGroup);
|
|
||||||
|
|
||||||
return filters.reduce((d, f) => d.filter(f), data);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="flex flex-col gap-4 w-full h-full">
|
|
||||||
<div className="w-full flex gap-4 justify-between items-center">
|
|
||||||
<Checkbox isChecked={isShowingAmount} onChange={setIsShowingAmount}>
|
|
||||||
Show Utilization
|
|
||||||
</Checkbox>
|
|
||||||
<Popover>
|
|
||||||
<PopoverTrigger>
|
|
||||||
<div className="flex items-center justify-center p-2 hover:bg-neutral-300/50 rounded-full transition ease-in-out duration-300">
|
|
||||||
<BsFilter size={20} />
|
|
||||||
</div>
|
|
||||||
</PopoverTrigger>
|
|
||||||
<PopoverContent className="w-96">
|
|
||||||
<div className="flex flex-col gap-4">
|
|
||||||
<span className="font-bold text-lg">Filters</span>
|
|
||||||
<Select
|
|
||||||
options={availableCorporates.map((x) => ({
|
|
||||||
value: x?.id || "N/A",
|
|
||||||
label: x?.corporateInformation?.companyInformation?.name || x?.name || "N/A",
|
|
||||||
}))}
|
|
||||||
isClearable
|
|
||||||
value={
|
|
||||||
selectedCorporate === null
|
|
||||||
? null
|
|
||||||
: {
|
|
||||||
value: selectedCorporate?.id || "N/A",
|
|
||||||
label:
|
|
||||||
selectedCorporate?.corporateInformation?.companyInformation?.name ||
|
|
||||||
selectedCorporate?.name ||
|
|
||||||
"N/A",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
placeholder="Select a Corporate..."
|
|
||||||
onChange={(value) =>
|
|
||||||
!value
|
|
||||||
? setSelectedCorporate(null)
|
|
||||||
: setSelectedCorporate(
|
|
||||||
value.value === "N/A" ? undefined : availableCorporates.find((x) => x?.id === value.value),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<Select
|
|
||||||
options={availableGroups.map((x) => ({
|
|
||||||
value: x?.id || "N/A",
|
|
||||||
label: x?.name || "N/A",
|
|
||||||
}))}
|
|
||||||
isClearable
|
|
||||||
value={
|
|
||||||
selectedGroup === null
|
|
||||||
? null
|
|
||||||
: {
|
|
||||||
value: selectedGroup?.id || "N/A",
|
|
||||||
label: selectedGroup?.name || "N/A",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
placeholder="Select a Group..."
|
|
||||||
onChange={(value) =>
|
|
||||||
!value
|
|
||||||
? setSelectedGroup(null)
|
|
||||||
: setSelectedGroup(value.value === "N/A" ? undefined : availableGroups.find((x) => x?.id === value.value))
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</PopoverContent>
|
|
||||||
</Popover>
|
|
||||||
</div>
|
|
||||||
<List<StudentPerformanceItem>
|
|
||||||
data={filterUsers(
|
|
||||||
items.sort(
|
|
||||||
(a, b) =>
|
|
||||||
averageLevelCalculator(
|
|
||||||
users,
|
|
||||||
stats.filter((x) => x.user === b.id),
|
|
||||||
) -
|
|
||||||
averageLevelCalculator(
|
|
||||||
users,
|
|
||||||
stats.filter((x) => x.user === a.id),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)}
|
|
||||||
columns={columns}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function MasterCorporateDashboard({user}: Props) {
|
|
||||||
const [selectedUser, setSelectedUser] = useState<User>();
|
|
||||||
const [showModal, setShowModal] = useState(false);
|
|
||||||
const [corporateAssignments, setCorporateAssignments] = useState<(Assignment & {corporate?: CorporateUser})[]>([]);
|
|
||||||
|
|
||||||
const {data: stats} = useFilterRecordsByUser<Stat[]>();
|
|
||||||
|
|
||||||
const {users: students, reload: reloadStudents, isLoading: isStudentsLoading} = useUsers(userHashStudent);
|
|
||||||
const {users: teachers, reload: reloadTeachers, isLoading: isTeachersLoading} = useUsers(userHashTeacher);
|
|
||||||
const {users: corporates, reload: reloadCorporates, isLoading: isCorporatesLoading} = useUsers(userHashCorporate);
|
|
||||||
|
|
||||||
const {groups} = useGroups({admin: user.id, userType: user.type});
|
|
||||||
const {balance} = useUserBalance();
|
|
||||||
|
|
||||||
const users = useMemo(() => uniqBy([...students, ...teachers, ...corporates, user], "id"), [corporates, students, teachers, user]);
|
|
||||||
|
|
||||||
const {assignments, isLoading: isAssignmentsLoading, reload: reloadAssignments} = useAssignments({corporate: user.id});
|
|
||||||
|
|
||||||
const assignmentsGroups = useMemo(() => groups.filter((x) => x.admin === user.id || x.participants.includes(user.id)), [groups, user.id]);
|
|
||||||
const assignmentsUsers = useMemo(
|
|
||||||
() =>
|
|
||||||
[...students, ...teachers].filter((x) =>
|
|
||||||
!!selectedUser
|
|
||||||
? groups
|
|
||||||
.filter((g) => g.admin === selectedUser.id)
|
|
||||||
.flatMap((g) => g.participants)
|
|
||||||
.includes(x.id) || false
|
|
||||||
: groups.flatMap((g) => g.participants).includes(x.id),
|
|
||||||
),
|
|
||||||
[groups, selectedUser, teachers, students],
|
|
||||||
);
|
|
||||||
|
|
||||||
const appendUserFilters = useFilterStore((state) => state.appendUserFilter);
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setShowModal(!!selectedUser && router.asPath === "/");
|
|
||||||
}, [selectedUser, router.asPath]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setCorporateAssignments(
|
|
||||||
assignments.filter(activeAssignmentFilter).map((a) => {
|
|
||||||
const assigner = [...teachers, ...corporates].find((x) => x.id === a.assigner);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...a,
|
|
||||||
corporate: assigner ? getCorporateUser(assigner, [...teachers, ...corporates], groups) : undefined,
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}, [assignments, groups, teachers, corporates]);
|
|
||||||
const getStatsByStudent = (user: User) => stats.filter((s) => s.user === user.id);
|
|
||||||
|
|
||||||
const UserDisplay = (displayUser: User) => (
|
|
||||||
<div
|
|
||||||
onClick={() => setSelectedUser(displayUser)}
|
|
||||||
className="flex w-full p-4 gap-4 items-center hover:bg-mti-purple-ultralight cursor-pointer transition ease-in-out duration-300">
|
|
||||||
<img src={displayUser.profilePicture} alt={displayUser.name} className="rounded-full w-10 h-10" />
|
|
||||||
<div className="flex flex-col gap-1 items-start">
|
|
||||||
<span>{displayUser.name}</span>
|
|
||||||
<span className="text-sm opacity-75">{displayUser.email}</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
|
|
||||||
const groupedByNameCorporates = groupBy(corporates, (x: CorporateUser) => x.corporateInformation?.companyInformation?.name || 'N/A');
|
|
||||||
const groupedByNameCorporatesKeys = Object.keys(groupedByNameCorporates);
|
|
||||||
const groupedByNameCorporateIds = groupedByNameCorporatesKeys.reduce((accm, x) => {
|
|
||||||
const corporateUserIds = (groupedByNameCorporates[x] as CorporateUser[]).map((y) => y.id);
|
|
||||||
return { ...accm, [x]: corporateUserIds };
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
console.log("groupedByNameCorporates", groupedByNameCorporates, groupedByNameCorporateIds);
|
|
||||||
const GroupsList = () => {
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<div className="flex flex-col gap-4">
|
|
||||||
<div
|
|
||||||
onClick={() => router.push("/")}
|
|
||||||
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
|
||||||
<BsArrowLeft className="text-xl" />
|
|
||||||
<span>Back</span>
|
|
||||||
</div>
|
|
||||||
<h2 className="text-2xl font-semibold">Groups ({groups.length})</h2>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<GroupList user={user} />
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const StudentPerformancePage = () => {
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<div className="w-full flex justify-between items-center">
|
|
||||||
<div
|
|
||||||
onClick={() => router.push("/")}
|
|
||||||
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
|
||||||
<BsArrowLeft className="text-xl" />
|
|
||||||
<span>Back</span>
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
onClick={reloadAssignments}
|
|
||||||
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
|
||||||
<span>Reload</span>
|
|
||||||
<BsArrowRepeat className={clsx("text-xl", isAssignmentsLoading && "animate-spin")} />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<StudentPerformanceList items={students} stats={stats} users={corporates} groups={groups} />
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const MasterStatisticalPage = () => {
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<div className="flex flex-col gap-4">
|
|
||||||
<div
|
|
||||||
onClick={() => router.push("/")}
|
|
||||||
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
|
||||||
<BsArrowLeft className="text-xl" />
|
|
||||||
<span>Back</span>
|
|
||||||
</div>
|
|
||||||
<h2 className="text-2xl font-semibold">Master Statistical</h2>
|
|
||||||
</div>
|
|
||||||
<MasterStatistical users={users} corporateUsers={groupedByNameCorporateIds} />
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const DefaultDashboard = () => (
|
|
||||||
<>
|
|
||||||
<section className="flex flex-wrap gap-2 items-center -lg:justify-center lg:justify-between text-center">
|
|
||||||
<IconCard
|
|
||||||
onClick={() => router.push("/#students")}
|
|
||||||
Icon={BsPersonFill}
|
|
||||||
isLoading={isStudentsLoading}
|
|
||||||
label="Students"
|
|
||||||
value={students.length}
|
|
||||||
color="purple"
|
|
||||||
/>
|
|
||||||
<IconCard
|
|
||||||
onClick={() => router.push("/#teachers")}
|
|
||||||
Icon={BsPencilSquare}
|
|
||||||
isLoading={isTeachersLoading}
|
|
||||||
label="Teachers"
|
|
||||||
value={teachers.length}
|
|
||||||
color="purple"
|
|
||||||
/>
|
|
||||||
<IconCard
|
|
||||||
Icon={BsClipboard2Data}
|
|
||||||
label="Exams Performed"
|
|
||||||
value={stats.filter((s) => groups.flatMap((g) => g.participants).includes(s.user)).length}
|
|
||||||
color="purple"
|
|
||||||
/>
|
|
||||||
<IconCard
|
|
||||||
Icon={BsPaperclip}
|
|
||||||
label="Average Level"
|
|
||||||
value={averageLevelCalculator(
|
|
||||||
students,
|
|
||||||
stats.filter((s) => groups.flatMap((g) => g.participants).includes(s.user)),
|
|
||||||
).toFixed(1)}
|
|
||||||
color="purple"
|
|
||||||
/>
|
|
||||||
<IconCard onClick={() => router.push("/#groups")} Icon={BsPeople} label="Groups" value={groups.length} color="purple" />
|
|
||||||
<IconCard
|
|
||||||
Icon={BsPersonCheck}
|
|
||||||
label="User Balance"
|
|
||||||
value={`${balance}/${user.corporateInformation?.companyInformation?.userAmount || 0}`}
|
|
||||||
color="purple"
|
|
||||||
/>
|
|
||||||
<IconCard
|
|
||||||
Icon={BsClock}
|
|
||||||
label="Expiration Date"
|
|
||||||
value={user.subscriptionExpirationDate ? moment(user.subscriptionExpirationDate).format("DD/MM/yyyy") : "Unlimited"}
|
|
||||||
color="rose"
|
|
||||||
/>
|
|
||||||
<IconCard
|
|
||||||
Icon={BsBank}
|
|
||||||
label="Corporate Accounts"
|
|
||||||
value={corporates.length}
|
|
||||||
isLoading={isCorporatesLoading}
|
|
||||||
color="purple"
|
|
||||||
onClick={() => router.push("/#corporate")}
|
|
||||||
/>
|
|
||||||
<IconCard
|
|
||||||
Icon={BsBank}
|
|
||||||
label="Corporate"
|
|
||||||
value={groupedByNameCorporatesKeys.length}
|
|
||||||
isLoading={isCorporatesLoading}
|
|
||||||
color="purple"
|
|
||||||
// onClick={() => router.push("/#corporate")}
|
|
||||||
/>
|
|
||||||
<IconCard
|
|
||||||
Icon={BsPersonFillGear}
|
|
||||||
isLoading={isStudentsLoading}
|
|
||||||
label="Student Performance"
|
|
||||||
value={students.length}
|
|
||||||
color="purple"
|
|
||||||
onClick={() => router.push("/#studentsPerformance")}
|
|
||||||
/>
|
|
||||||
<IconCard
|
|
||||||
Icon={BsDatabase}
|
|
||||||
label="Master Statistical"
|
|
||||||
// value={masterCorporateUserGroups.length}
|
|
||||||
color="purple"
|
|
||||||
onClick={() => router.push("/#statistical")}
|
|
||||||
/>
|
|
||||||
<button
|
|
||||||
disabled={isAssignmentsLoading}
|
|
||||||
onClick={() => router.push("/#assignments")}
|
|
||||||
className="bg-white col-span-2 rounded-xl shadow p-4 flex flex-col gap-4 items-center w-96 h-52 justify-center cursor-pointer hover:shadow-xl transition ease-in-out duration-300">
|
|
||||||
<BsEnvelopePaper className="text-6xl text-mti-purple-light" />
|
|
||||||
<span className="flex flex-col gap-1 items-center text-xl">
|
|
||||||
<span className="text-lg">Assignments</span>
|
|
||||||
<span className="font-semibold text-mti-purple-light">
|
|
||||||
{isAssignmentsLoading ? "Loading..." : assignments.filter((a) => !a.archived).length}
|
|
||||||
</span>
|
|
||||||
</span>
|
|
||||||
</button>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section className="grid grid-cols-1 md:grid-cols-2 gap-4 w-full justify-between">
|
|
||||||
<div className="bg-white shadow flex flex-col rounded-xl w-full">
|
|
||||||
<span className="p-4">Latest students</span>
|
|
||||||
<div className="flex flex-col items-start h-96 overflow-scroll scrollbar-hide">
|
|
||||||
{students
|
|
||||||
.sort((a, b) => dateSorter(a, b, "desc", "registrationDate"))
|
|
||||||
.map((x) => (
|
|
||||||
<UserDisplay key={x.id} {...x} />
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="bg-white shadow flex flex-col rounded-xl w-full">
|
|
||||||
<span className="p-4">Latest teachers</span>
|
|
||||||
<div className="flex flex-col items-start h-96 overflow-scroll scrollbar-hide">
|
|
||||||
{teachers
|
|
||||||
.sort((a, b) => dateSorter(a, b, "desc", "registrationDate"))
|
|
||||||
.map((x) => (
|
|
||||||
<UserDisplay key={x.id} {...x} />
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="bg-white shadow flex flex-col rounded-xl w-full">
|
|
||||||
<span className="p-4">Highest level students</span>
|
|
||||||
<div className="flex flex-col items-start h-96 overflow-scroll scrollbar-hide">
|
|
||||||
{students
|
|
||||||
.sort((a, b) => calculateAverageLevel(b.levels) - calculateAverageLevel(a.levels))
|
|
||||||
.map((x) => (
|
|
||||||
<UserDisplay key={x.id} {...x} />
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="bg-white shadow flex flex-col rounded-xl w-full">
|
|
||||||
<span className="p-4">Highest exam count students</span>
|
|
||||||
<div className="flex flex-col items-start h-96 overflow-scroll scrollbar-hide">
|
|
||||||
{students
|
|
||||||
.sort(
|
|
||||||
(a, b) =>
|
|
||||||
Object.keys(groupByExam(getStatsByStudent(b))).length - Object.keys(groupByExam(getStatsByStudent(a))).length,
|
|
||||||
)
|
|
||||||
.map((x) => (
|
|
||||||
<UserDisplay key={x.id} {...x} />
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<Modal isOpen={showModal} onClose={() => setSelectedUser(undefined)}>
|
|
||||||
<>
|
|
||||||
{selectedUser && (
|
|
||||||
<div className="w-full flex flex-col gap-8">
|
|
||||||
<UserCard
|
|
||||||
maxUserAmount={
|
|
||||||
user.type === "mastercorporate"
|
|
||||||
? (user.corporateInformation?.companyInformation?.userAmount || 0) - balance
|
|
||||||
: undefined
|
|
||||||
}
|
|
||||||
loggedInUser={user}
|
|
||||||
onClose={(shouldReload) => {
|
|
||||||
setSelectedUser(undefined);
|
|
||||||
if (shouldReload && selectedUser!.type === "student") reloadStudents();
|
|
||||||
if (shouldReload && selectedUser!.type === "teacher") reloadTeachers();
|
|
||||||
if (shouldReload && selectedUser!.type === "corporate") reloadCorporates();
|
|
||||||
}}
|
|
||||||
onViewStudents={
|
|
||||||
selectedUser.type === "corporate" || selectedUser.type === "teacher"
|
|
||||||
? () => {
|
|
||||||
appendUserFilters({
|
|
||||||
id: "view-students",
|
|
||||||
filter: (x: User) => x.type === "student",
|
|
||||||
});
|
|
||||||
appendUserFilters({
|
|
||||||
id: "belongs-to-admin",
|
|
||||||
filter: (x: User) =>
|
|
||||||
groups
|
|
||||||
.filter((g) => g.admin === selectedUser.id || g.participants.includes(selectedUser.id))
|
|
||||||
.flatMap((g) => g.participants)
|
|
||||||
.includes(x.id),
|
|
||||||
});
|
|
||||||
|
|
||||||
router.push("/list/users");
|
|
||||||
}
|
|
||||||
: undefined
|
|
||||||
}
|
|
||||||
onViewTeachers={
|
|
||||||
selectedUser.type === "corporate" || selectedUser.type === "student"
|
|
||||||
? () => {
|
|
||||||
appendUserFilters({
|
|
||||||
id: "view-teachers",
|
|
||||||
filter: (x: User) => x.type === "teacher",
|
|
||||||
});
|
|
||||||
appendUserFilters({
|
|
||||||
id: "belongs-to-admin",
|
|
||||||
filter: (x: User) =>
|
|
||||||
groups
|
|
||||||
.filter((g) => g.admin === selectedUser.id || g.participants.includes(selectedUser.id))
|
|
||||||
.flatMap((g) => g.participants)
|
|
||||||
.includes(x.id),
|
|
||||||
});
|
|
||||||
|
|
||||||
router.push("/list/users");
|
|
||||||
}
|
|
||||||
: undefined
|
|
||||||
}
|
|
||||||
user={selectedUser}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
</Modal>
|
|
||||||
{router.asPath === "/#students" && (
|
|
||||||
<UserList
|
|
||||||
user={user}
|
|
||||||
type="student"
|
|
||||||
renderHeader={(total) => (
|
|
||||||
<div className="flex flex-col gap-4">
|
|
||||||
<div
|
|
||||||
onClick={() => router.push("/")}
|
|
||||||
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
|
||||||
<BsArrowLeft className="text-xl" />
|
|
||||||
<span>Back</span>
|
|
||||||
</div>
|
|
||||||
<h2 className="text-2xl font-semibold">Students ({total})</h2>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{router.asPath === "/#teachers" && (
|
|
||||||
<UserList
|
|
||||||
user={user}
|
|
||||||
type="teacher"
|
|
||||||
renderHeader={(total) => (
|
|
||||||
<div className="flex flex-col gap-4">
|
|
||||||
<div
|
|
||||||
onClick={() => router.push("/")}
|
|
||||||
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
|
||||||
<BsArrowLeft className="text-xl" />
|
|
||||||
<span>Back</span>
|
|
||||||
</div>
|
|
||||||
<h2 className="text-2xl font-semibold">Teachers ({total})</h2>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{router.asPath === "/#groups" && <GroupsList />}
|
|
||||||
{router.asPath === "/#corporate" && (
|
|
||||||
<UserList
|
|
||||||
user={user}
|
|
||||||
type="corporate"
|
|
||||||
renderHeader={(total) => (
|
|
||||||
<div className="flex flex-col gap-4">
|
|
||||||
<div
|
|
||||||
onClick={() => router.push("/")}
|
|
||||||
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
|
||||||
<BsArrowLeft className="text-xl" />
|
|
||||||
<span>Back</span>
|
|
||||||
</div>
|
|
||||||
<h2 className="text-2xl font-semibold">Corporate ({total})</h2>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{router.asPath === "/#assignments" && (
|
|
||||||
<AssignmentsPage
|
|
||||||
assignments={assignments}
|
|
||||||
corporateAssignments={corporateAssignments}
|
|
||||||
groups={assignmentsGroups}
|
|
||||||
user={user}
|
|
||||||
users={assignmentsUsers}
|
|
||||||
reloadAssignments={reloadAssignments}
|
|
||||||
isLoading={isAssignmentsLoading}
|
|
||||||
onBack={() => router.push("/")}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{router.asPath === "/#studentsPerformance" && <StudentPerformancePage />}
|
|
||||||
{router.asPath === "/#statistical" && <MasterStatisticalPage />}
|
|
||||||
{router.asPath === "/" && <DefaultDashboard />}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
371
src/dashboards/MasterCorporate/MasterStatistical.tsx
Normal file
371
src/dashboards/MasterCorporate/MasterStatistical.tsx
Normal file
@@ -0,0 +1,371 @@
|
|||||||
|
import React from "react";
|
||||||
|
import {CorporateUser, User} from "@/interfaces/user";
|
||||||
|
import {BsFileExcel, BsBank, BsPersonFill} from "react-icons/bs";
|
||||||
|
import IconCard from "../IconCard";
|
||||||
|
|
||||||
|
import useAssignmentsCorporates from "@/hooks/useAssignmentCorporates";
|
||||||
|
import ReactDatePicker from "react-datepicker";
|
||||||
|
|
||||||
|
import moment from "moment";
|
||||||
|
import {AssignmentWithCorporateId} from "@/interfaces/results";
|
||||||
|
import {flexRender, createColumnHelper, getCoreRowModel, useReactTable} from "@tanstack/react-table";
|
||||||
|
import Checkbox from "@/components/Low/Checkbox";
|
||||||
|
import {useListSearch} from "@/hooks/useListSearch";
|
||||||
|
import axios from "axios";
|
||||||
|
import {toast} from "react-toastify";
|
||||||
|
import Button from "@/components/Low/Button";
|
||||||
|
|
||||||
|
interface GroupedCorporateUsers {
|
||||||
|
// list of user Ids
|
||||||
|
[key: string]: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
corporateUsers: GroupedCorporateUsers;
|
||||||
|
users: User[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TableData {
|
||||||
|
user: string;
|
||||||
|
email: string;
|
||||||
|
correct: number;
|
||||||
|
corporate: string;
|
||||||
|
submitted: boolean;
|
||||||
|
date: moment.Moment;
|
||||||
|
assignment: string;
|
||||||
|
corporateId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UserCount {
|
||||||
|
userCount: number;
|
||||||
|
maxUserCount: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const searchFilters = [["email"], ["user"], ["userId"]];
|
||||||
|
|
||||||
|
const MasterStatistical = (props: Props) => {
|
||||||
|
const {users, corporateUsers} = props;
|
||||||
|
|
||||||
|
// const corporateRelevantUsers = React.useMemo(
|
||||||
|
// () => corporateUsers.filter((x) => x.type !== "student") as CorporateUser[],
|
||||||
|
// [corporateUsers]
|
||||||
|
// );
|
||||||
|
|
||||||
|
const corporates = React.useMemo(() => Object.values(corporateUsers).flat(), [corporateUsers]);
|
||||||
|
|
||||||
|
const [selectedCorporates, setSelectedCorporates] = React.useState<string[]>(corporates);
|
||||||
|
const [startDate, setStartDate] = React.useState<Date | null>(moment("01/01/2023").toDate());
|
||||||
|
const [endDate, setEndDate] = React.useState<Date | null>(moment().endOf("year").toDate());
|
||||||
|
|
||||||
|
const {assignments} = useAssignmentsCorporates({
|
||||||
|
// corporates: [...corporates, "tYU0HTiJdjMsS8SB7XJsUdMMP892"],
|
||||||
|
corporates: selectedCorporates,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
});
|
||||||
|
|
||||||
|
const [downloading, setDownloading] = React.useState<boolean>(false);
|
||||||
|
|
||||||
|
const tableResults = React.useMemo(
|
||||||
|
() =>
|
||||||
|
assignments.reduce((accmA: TableData[], a: AssignmentWithCorporateId) => {
|
||||||
|
const userResults = a.assignees.map((assignee) => {
|
||||||
|
const userStats = a.results.find((r) => r.user === assignee)?.stats || [];
|
||||||
|
const userData = users.find((u) => u.id === assignee);
|
||||||
|
const corporate = users.find((u) => u.id === a.assigner)?.name || "";
|
||||||
|
const commonData = {
|
||||||
|
user: userData?.name || "",
|
||||||
|
email: userData?.email || "",
|
||||||
|
userId: assignee,
|
||||||
|
corporateId: a.corporateId,
|
||||||
|
corporate,
|
||||||
|
assignment: a.name,
|
||||||
|
};
|
||||||
|
if (userStats.length === 0) {
|
||||||
|
return {
|
||||||
|
...commonData,
|
||||||
|
correct: 0,
|
||||||
|
submitted: false,
|
||||||
|
// date: moment(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...commonData,
|
||||||
|
correct: userStats.reduce((n, e) => n + e.score.correct, 0),
|
||||||
|
submitted: true,
|
||||||
|
date: moment.max(userStats.map((e) => moment(e.date))),
|
||||||
|
};
|
||||||
|
}) as TableData[];
|
||||||
|
|
||||||
|
return [...accmA, ...userResults];
|
||||||
|
}, []),
|
||||||
|
[assignments, users],
|
||||||
|
);
|
||||||
|
|
||||||
|
const getCorporateScores = (corporateId: string): UserCount => {
|
||||||
|
const corporateAssignmentsUsers = assignments.filter((a) => a.corporateId === corporateId).reduce((acc, a) => acc + a.assignees.length, 0);
|
||||||
|
|
||||||
|
const corporateResults = tableResults.filter((r) => r.corporateId === corporateId).length;
|
||||||
|
|
||||||
|
return {
|
||||||
|
maxUserCount: corporateAssignmentsUsers,
|
||||||
|
userCount: corporateResults,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const getCorporatesScoresHash = (data: string[]) =>
|
||||||
|
data.reduce(
|
||||||
|
(accm, id) => ({
|
||||||
|
...accm,
|
||||||
|
[id]: getCorporateScores(id),
|
||||||
|
}),
|
||||||
|
{},
|
||||||
|
) as Record<string, UserCount>;
|
||||||
|
|
||||||
|
const getConsolidateScore = (data: Record<string, UserCount>) =>
|
||||||
|
Object.values(data).reduce(
|
||||||
|
(acc: UserCount, {userCount, maxUserCount}: UserCount) => ({
|
||||||
|
userCount: acc.userCount + userCount,
|
||||||
|
maxUserCount: acc.maxUserCount + maxUserCount,
|
||||||
|
}),
|
||||||
|
{userCount: 0, maxUserCount: 0},
|
||||||
|
);
|
||||||
|
|
||||||
|
const corporateScores = getCorporatesScoresHash(corporates);
|
||||||
|
const consolidateScore = getConsolidateScore(corporateScores);
|
||||||
|
|
||||||
|
const getConsolidateScoreStr = (data: UserCount) => `${data.userCount}/${data.maxUserCount}`;
|
||||||
|
|
||||||
|
const columnHelper = createColumnHelper<TableData>();
|
||||||
|
|
||||||
|
const defaultColumns = [
|
||||||
|
columnHelper.accessor("user", {
|
||||||
|
header: "User",
|
||||||
|
id: "user",
|
||||||
|
cell: (info) => {
|
||||||
|
return <span>{info.getValue()}</span>;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("email", {
|
||||||
|
header: "Email",
|
||||||
|
id: "email",
|
||||||
|
cell: (info) => {
|
||||||
|
return <span>{info.getValue()}</span>;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("corporate", {
|
||||||
|
header: "Corporate",
|
||||||
|
id: "corporate",
|
||||||
|
cell: (info) => {
|
||||||
|
return <span>{info.getValue()}</span>;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("assignment", {
|
||||||
|
header: "Assignment",
|
||||||
|
id: "assignment",
|
||||||
|
cell: (info) => {
|
||||||
|
return <span>{info.getValue()}</span>;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("submitted", {
|
||||||
|
header: "Submitted",
|
||||||
|
id: "submitted",
|
||||||
|
cell: (info) => {
|
||||||
|
return (
|
||||||
|
<Checkbox isChecked={info.getValue()} disabled onChange={() => {}}>
|
||||||
|
<span></span>
|
||||||
|
</Checkbox>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("correct", {
|
||||||
|
header: "Correct",
|
||||||
|
id: "correct",
|
||||||
|
cell: (info) => {
|
||||||
|
return <span>{info.getValue()}</span>;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("date", {
|
||||||
|
header: "Date",
|
||||||
|
id: "date",
|
||||||
|
cell: (info) => {
|
||||||
|
const date = info.getValue();
|
||||||
|
if (date) {
|
||||||
|
return <span>{date.format("DD/MM/YYYY")}</span>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <span>{""}</span>;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
const {rows: filteredRows, renderSearch, text: searchText} = useListSearch(searchFilters, tableResults);
|
||||||
|
|
||||||
|
const table = useReactTable({
|
||||||
|
data: filteredRows,
|
||||||
|
columns: defaultColumns,
|
||||||
|
getCoreRowModel: getCoreRowModel(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const areAllSelected = selectedCorporates.length === corporates.length;
|
||||||
|
|
||||||
|
const getStudentsConsolidateScore = () => {
|
||||||
|
if (tableResults.length === 0) {
|
||||||
|
return {highest: null, lowest: null};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the student with the highest and lowest score
|
||||||
|
return tableResults.reduce(
|
||||||
|
(acc, curr) => {
|
||||||
|
if (curr.correct > acc.highest.correct) {
|
||||||
|
acc.highest = curr;
|
||||||
|
}
|
||||||
|
if (curr.correct < acc.lowest.correct) {
|
||||||
|
acc.lowest = curr;
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
{highest: tableResults[0], lowest: tableResults[0]},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const triggerDownload = async () => {
|
||||||
|
try {
|
||||||
|
setDownloading(true);
|
||||||
|
const res = await axios.post("/api/assignments/statistical/excel", {
|
||||||
|
ids: selectedCorporates,
|
||||||
|
...(startDate ? {startDate: startDate.toISOString()} : {}),
|
||||||
|
...(endDate ? {endDate: endDate.toISOString()} : {}),
|
||||||
|
searchText,
|
||||||
|
});
|
||||||
|
toast.success("Report ready!");
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = res.data;
|
||||||
|
// download should have worked but there are some CORS issues
|
||||||
|
// https://firebase.google.com/docs/storage/web/download-files#cors_configuration
|
||||||
|
// link.download="report.pdf";
|
||||||
|
link.target = "_blank";
|
||||||
|
link.rel = "noreferrer";
|
||||||
|
link.click();
|
||||||
|
setDownloading(false);
|
||||||
|
} catch (err) {
|
||||||
|
toast.error("Failed to display the report!");
|
||||||
|
console.error(err);
|
||||||
|
setDownloading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const consolidateResults = getStudentsConsolidateScore();
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="flex flex-wrap gap-2 items-center text-center">
|
||||||
|
<IconCard
|
||||||
|
Icon={BsBank}
|
||||||
|
label="Consolidate"
|
||||||
|
value={getConsolidateScoreStr(consolidateScore)}
|
||||||
|
color="purple"
|
||||||
|
onClick={() => {
|
||||||
|
if (areAllSelected) {
|
||||||
|
setSelectedCorporates([]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setSelectedCorporates(corporates);
|
||||||
|
}}
|
||||||
|
isSelected={areAllSelected}
|
||||||
|
/>
|
||||||
|
{Object.keys(corporateUsers).map((corporateName) => {
|
||||||
|
const group = corporateUsers[corporateName];
|
||||||
|
const isSelected = group.every((id) => selectedCorporates.includes(id));
|
||||||
|
|
||||||
|
const valueHash = getCorporatesScoresHash(group);
|
||||||
|
const value = getConsolidateScoreStr(getConsolidateScore(valueHash));
|
||||||
|
return (
|
||||||
|
<IconCard
|
||||||
|
key={corporateName}
|
||||||
|
Icon={BsBank}
|
||||||
|
label={corporateName}
|
||||||
|
value={value}
|
||||||
|
color="purple"
|
||||||
|
onClick={() => {
|
||||||
|
if (isSelected) {
|
||||||
|
setSelectedCorporates((prev) => prev.filter((x) => !group.includes(x)));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setSelectedCorporates((prev) => [...new Set([...prev, ...group])]);
|
||||||
|
}}
|
||||||
|
isSelected={isSelected}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-3 w-full">
|
||||||
|
<div className="flex flex-col gap-3">
|
||||||
|
<label className="font-normal text-base text-mti-gray-dim">Date</label>
|
||||||
|
<ReactDatePicker
|
||||||
|
dateFormat="dd/MM/yyyy"
|
||||||
|
className="px-4 py-6 w-52 text-sm text-center font-normal placeholder:text-mti-gray-cool disabled:bg-mti-gray-platinum/40 disabled:text-mti-gray-dim disabled:cursor-not-allowed rounded-full border border-mti-gray-platinum focus:outline-none"
|
||||||
|
selected={startDate}
|
||||||
|
startDate={startDate}
|
||||||
|
endDate={endDate}
|
||||||
|
selectsRange
|
||||||
|
showMonthDropdown
|
||||||
|
onChange={([initialDate, finalDate]: [Date, Date]) => {
|
||||||
|
setStartDate(initialDate ?? moment("01/01/2023").toDate());
|
||||||
|
if (finalDate) {
|
||||||
|
// basicly selecting a final day works as if I'm selecting the first
|
||||||
|
// minute of that day. this way it covers the whole day
|
||||||
|
setEndDate(moment(finalDate).endOf("day").toDate());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setEndDate(null);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{renderSearch()}
|
||||||
|
<div className="flex flex-col gap-3 justify-end">
|
||||||
|
<Button className="max-w-[200px] h-[70px]" variant="outline" onClick={triggerDownload}>
|
||||||
|
Download
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<table className="rounded-xl h-full bg-mti-purple-ultralight/40 w-full">
|
||||||
|
<thead>
|
||||||
|
{table.getHeaderGroups().map((headerGroup) => (
|
||||||
|
<tr key={headerGroup.id}>
|
||||||
|
{headerGroup.headers.map((header) => (
|
||||||
|
<th className="p-4 text-left" key={header.id}>
|
||||||
|
{header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
|
||||||
|
</th>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</thead>
|
||||||
|
<tbody className="px-2">
|
||||||
|
{table.getRowModel().rows.map((row) => (
|
||||||
|
<tr className="odd:bg-white even:bg-mti-purple-ultralight/40 rounded-lg py-2" key={row.id}>
|
||||||
|
{row.getVisibleCells().map((cell) => (
|
||||||
|
<td className="px-4 py-2" key={cell.id}>
|
||||||
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||||
|
</td>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-wrap gap-2 items-center text-center">
|
||||||
|
{consolidateResults.highest && (
|
||||||
|
<IconCard onClick={() => {}} Icon={BsPersonFill} label={`Highest result: ${consolidateResults.highest.user}`} color="purple" />
|
||||||
|
)}
|
||||||
|
{consolidateResults.lowest && (
|
||||||
|
<IconCard onClick={() => {}} Icon={BsPersonFill} label={`Lowest result: ${consolidateResults.lowest.user}`} color="purple" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default MasterStatistical;
|
||||||
52
src/dashboards/MasterCorporate/MasterStatisticalPage.tsx
Normal file
52
src/dashboards/MasterCorporate/MasterStatisticalPage.tsx
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import useUsers from "@/hooks/useUsers";
|
||||||
|
import {CorporateUser, User} from "@/interfaces/user";
|
||||||
|
import {groupBy} from "lodash";
|
||||||
|
import {useRouter} from "next/router";
|
||||||
|
import {useMemo} from "react";
|
||||||
|
import {BsArrowLeft} from "react-icons/bs";
|
||||||
|
import MasterStatistical from "./MasterStatistical";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
user: User;
|
||||||
|
}
|
||||||
|
|
||||||
|
const MasterStatisticalPage = () => {
|
||||||
|
const {users} = useUsers();
|
||||||
|
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const groupedByNameCorporates = useMemo(
|
||||||
|
() =>
|
||||||
|
groupBy(
|
||||||
|
users.filter((x) => x.type === "corporate"),
|
||||||
|
(x: CorporateUser) => x.corporateInformation?.companyInformation?.name || "N/A",
|
||||||
|
),
|
||||||
|
[users],
|
||||||
|
);
|
||||||
|
|
||||||
|
const groupedByNameCorporateIds = useMemo(
|
||||||
|
() =>
|
||||||
|
Object.keys(groupedByNameCorporates).reduce((accm, x) => {
|
||||||
|
const corporateUserIds = (groupedByNameCorporates[x] as CorporateUser[]).map((y) => y.id);
|
||||||
|
return {...accm, [x]: corporateUserIds};
|
||||||
|
}, {}),
|
||||||
|
[groupedByNameCorporates],
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div
|
||||||
|
onClick={() => router.push("/")}
|
||||||
|
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
||||||
|
<BsArrowLeft className="text-xl" />
|
||||||
|
<span>Back</span>
|
||||||
|
</div>
|
||||||
|
<h2 className="text-2xl font-semibold">Master Statistical</h2>
|
||||||
|
</div>
|
||||||
|
<MasterStatistical users={users} corporateUsers={groupedByNameCorporateIds} />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default MasterStatisticalPage;
|
||||||
252
src/dashboards/MasterCorporate/StudentPerformanceList.tsx
Normal file
252
src/dashboards/MasterCorporate/StudentPerformanceList.tsx
Normal file
@@ -0,0 +1,252 @@
|
|||||||
|
/* eslint-disable @next/next/no-img-element */
|
||||||
|
import {CorporateUser, Group, Stat, User} from "@/interfaces/user";
|
||||||
|
import {useState} from "react";
|
||||||
|
import {BsFilter} from "react-icons/bs";
|
||||||
|
|
||||||
|
import {averageLevelCalculator, calculateBandScore} from "@/utils/score";
|
||||||
|
import {groupByExam} from "@/utils/stats";
|
||||||
|
import {createColumnHelper} from "@tanstack/react-table";
|
||||||
|
import List from "@/components/List";
|
||||||
|
import {getUserCompanyName} from "@/resources/user";
|
||||||
|
import Checkbox from "@/components/Low/Checkbox";
|
||||||
|
import {uniqBy} from "lodash";
|
||||||
|
import Select from "@/components/Low/Select";
|
||||||
|
import {Popover, PopoverContent, PopoverTrigger} from "@/components/ui/popover";
|
||||||
|
|
||||||
|
type StudentPerformanceItem = User & {
|
||||||
|
corporate?: CorporateUser;
|
||||||
|
group?: Group;
|
||||||
|
};
|
||||||
|
|
||||||
|
const StudentPerformanceList = ({items, stats, users, groups}: {items: StudentPerformanceItem[]; stats: Stat[]; users: User[]; groups: Group[]}) => {
|
||||||
|
const [isShowingAmount, setIsShowingAmount] = useState(false);
|
||||||
|
const [availableCorporates] = useState(
|
||||||
|
uniqBy(
|
||||||
|
items.map((x) => x.corporate),
|
||||||
|
"id",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
const [availableGroups] = useState(
|
||||||
|
uniqBy(
|
||||||
|
items.map((x) => x.group),
|
||||||
|
"id",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
const [selectedCorporate, setSelectedCorporate] = useState<CorporateUser | null | undefined>(null);
|
||||||
|
const [selectedGroup, setSelectedGroup] = useState<Group | null | undefined>(null);
|
||||||
|
|
||||||
|
const columnHelper = createColumnHelper<StudentPerformanceItem>();
|
||||||
|
|
||||||
|
const columns = [
|
||||||
|
columnHelper.accessor("name", {
|
||||||
|
header: "Student Name",
|
||||||
|
cell: (info) => info.getValue(),
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("email", {
|
||||||
|
header: "E-mail",
|
||||||
|
cell: (info) => info.getValue(),
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("demographicInformation.passport_id", {
|
||||||
|
header: "ID",
|
||||||
|
cell: (info) => info.getValue() || "N/A",
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("group", {
|
||||||
|
header: "Group",
|
||||||
|
cell: (info) => info.getValue()?.name || "N/A",
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("corporate", {
|
||||||
|
header: "Corporate",
|
||||||
|
cell: (info) => (!!info.getValue() ? getUserCompanyName(info.getValue() as User, users, groups) : "N/A"),
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("levels.reading", {
|
||||||
|
header: "Reading",
|
||||||
|
cell: (info) =>
|
||||||
|
!isShowingAmount
|
||||||
|
? calculateBandScore(
|
||||||
|
stats
|
||||||
|
.filter((x) => x.module === "reading" && x.user === info.row.original.id)
|
||||||
|
.reduce((acc, curr) => acc + curr.score.correct, 0),
|
||||||
|
stats
|
||||||
|
.filter((x) => x.module === "reading" && x.user === info.row.original.id)
|
||||||
|
.reduce((acc, curr) => acc + curr.score.total, 0),
|
||||||
|
"level",
|
||||||
|
info.row.original.focus || "academic",
|
||||||
|
) || 0
|
||||||
|
: `${Object.keys(groupByExam(stats.filter((x) => x.module === "reading" && x.user === info.row.original.id))).length} exams`,
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("levels.listening", {
|
||||||
|
header: "Listening",
|
||||||
|
cell: (info) =>
|
||||||
|
!isShowingAmount
|
||||||
|
? calculateBandScore(
|
||||||
|
stats
|
||||||
|
.filter((x) => x.module === "listening" && x.user === info.row.original.id)
|
||||||
|
.reduce((acc, curr) => acc + curr.score.correct, 0),
|
||||||
|
stats
|
||||||
|
.filter((x) => x.module === "listening" && x.user === info.row.original.id)
|
||||||
|
.reduce((acc, curr) => acc + curr.score.total, 0),
|
||||||
|
"level",
|
||||||
|
info.row.original.focus || "academic",
|
||||||
|
) || 0
|
||||||
|
: `${Object.keys(groupByExam(stats.filter((x) => x.module === "listening" && x.user === info.row.original.id))).length} exams`,
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("levels.writing", {
|
||||||
|
header: "Writing",
|
||||||
|
cell: (info) =>
|
||||||
|
!isShowingAmount
|
||||||
|
? calculateBandScore(
|
||||||
|
stats
|
||||||
|
.filter((x) => x.module === "writing" && x.user === info.row.original.id)
|
||||||
|
.reduce((acc, curr) => acc + curr.score.correct, 0),
|
||||||
|
stats
|
||||||
|
.filter((x) => x.module === "writing" && x.user === info.row.original.id)
|
||||||
|
.reduce((acc, curr) => acc + curr.score.total, 0),
|
||||||
|
"level",
|
||||||
|
info.row.original.focus || "academic",
|
||||||
|
) || 0
|
||||||
|
: `${Object.keys(groupByExam(stats.filter((x) => x.module === "writing" && x.user === info.row.original.id))).length} exams`,
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("levels.speaking", {
|
||||||
|
header: "Speaking",
|
||||||
|
cell: (info) =>
|
||||||
|
!isShowingAmount
|
||||||
|
? calculateBandScore(
|
||||||
|
stats
|
||||||
|
.filter((x) => x.module === "speaking" && x.user === info.row.original.id)
|
||||||
|
.reduce((acc, curr) => acc + curr.score.correct, 0),
|
||||||
|
stats
|
||||||
|
.filter((x) => x.module === "speaking" && x.user === info.row.original.id)
|
||||||
|
.reduce((acc, curr) => acc + curr.score.total, 0),
|
||||||
|
"level",
|
||||||
|
info.row.original.focus || "academic",
|
||||||
|
) || 0
|
||||||
|
: `${Object.keys(groupByExam(stats.filter((x) => x.module === "speaking" && x.user === info.row.original.id))).length} exams`,
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("levels.level", {
|
||||||
|
header: "Level",
|
||||||
|
cell: (info) =>
|
||||||
|
!isShowingAmount
|
||||||
|
? calculateBandScore(
|
||||||
|
stats
|
||||||
|
.filter((x) => x.module === "level" && x.user === info.row.original.id)
|
||||||
|
.reduce((acc, curr) => acc + curr.score.correct, 0),
|
||||||
|
stats
|
||||||
|
.filter((x) => x.module === "level" && x.user === info.row.original.id)
|
||||||
|
.reduce((acc, curr) => acc + curr.score.total, 0),
|
||||||
|
"level",
|
||||||
|
info.row.original.focus || "academic",
|
||||||
|
) || 0
|
||||||
|
: `${Object.keys(groupByExam(stats.filter((x) => x.module === "level" && x.user === info.row.original.id))).length} exams`,
|
||||||
|
}),
|
||||||
|
columnHelper.accessor("levels", {
|
||||||
|
id: "overall_level",
|
||||||
|
header: "Overall",
|
||||||
|
cell: (info) =>
|
||||||
|
!isShowingAmount
|
||||||
|
? averageLevelCalculator(
|
||||||
|
users,
|
||||||
|
stats.filter((x) => x.user === info.row.original.id),
|
||||||
|
).toFixed(1)
|
||||||
|
: `${Object.keys(groupByExam(stats.filter((x) => x.user === info.row.original.id))).length} exams`,
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
const filterUsers = (data: StudentPerformanceItem[]) => {
|
||||||
|
const filterByCorporate = (item: StudentPerformanceItem) => item.corporate?.id === selectedCorporate?.id;
|
||||||
|
const filterByGroup = (item: StudentPerformanceItem) => item.group?.id === selectedGroup?.id;
|
||||||
|
|
||||||
|
const filters: ((item: StudentPerformanceItem) => boolean)[] = [];
|
||||||
|
if (selectedCorporate !== null) filters.push(filterByCorporate);
|
||||||
|
if (selectedGroup !== null) filters.push(filterByGroup);
|
||||||
|
|
||||||
|
return filters.reduce((d, f) => d.filter(f), data);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4 w-full h-full">
|
||||||
|
<div className="w-full flex gap-4 justify-between items-center">
|
||||||
|
<Checkbox isChecked={isShowingAmount} onChange={setIsShowingAmount}>
|
||||||
|
Show Utilization
|
||||||
|
</Checkbox>
|
||||||
|
<Popover>
|
||||||
|
<PopoverTrigger>
|
||||||
|
<div className="flex items-center justify-center p-2 hover:bg-neutral-300/50 rounded-full transition ease-in-out duration-300">
|
||||||
|
<BsFilter size={20} />
|
||||||
|
</div>
|
||||||
|
</PopoverTrigger>
|
||||||
|
<PopoverContent className="w-96">
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<span className="font-bold text-lg">Filters</span>
|
||||||
|
<Select
|
||||||
|
options={availableCorporates.map((x) => ({
|
||||||
|
value: x?.id || "N/A",
|
||||||
|
label: x?.corporateInformation?.companyInformation?.name || x?.name || "N/A",
|
||||||
|
}))}
|
||||||
|
isClearable
|
||||||
|
value={
|
||||||
|
selectedCorporate === null
|
||||||
|
? null
|
||||||
|
: {
|
||||||
|
value: selectedCorporate?.id || "N/A",
|
||||||
|
label:
|
||||||
|
selectedCorporate?.corporateInformation?.companyInformation?.name ||
|
||||||
|
selectedCorporate?.name ||
|
||||||
|
"N/A",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
placeholder="Select a Corporate..."
|
||||||
|
onChange={(value) =>
|
||||||
|
!value
|
||||||
|
? setSelectedCorporate(null)
|
||||||
|
: setSelectedCorporate(
|
||||||
|
value.value === "N/A" ? undefined : availableCorporates.find((x) => x?.id === value.value),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<Select
|
||||||
|
options={availableGroups.map((x) => ({
|
||||||
|
value: x?.id || "N/A",
|
||||||
|
label: x?.name || "N/A",
|
||||||
|
}))}
|
||||||
|
isClearable
|
||||||
|
value={
|
||||||
|
selectedGroup === null
|
||||||
|
? null
|
||||||
|
: {
|
||||||
|
value: selectedGroup?.id || "N/A",
|
||||||
|
label: selectedGroup?.name || "N/A",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
placeholder="Select a Group..."
|
||||||
|
onChange={(value) =>
|
||||||
|
!value
|
||||||
|
? setSelectedGroup(null)
|
||||||
|
: setSelectedGroup(value.value === "N/A" ? undefined : availableGroups.find((x) => x?.id === value.value))
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</PopoverContent>
|
||||||
|
</Popover>
|
||||||
|
</div>
|
||||||
|
<List<StudentPerformanceItem>
|
||||||
|
data={filterUsers(
|
||||||
|
items.sort(
|
||||||
|
(a, b) =>
|
||||||
|
averageLevelCalculator(
|
||||||
|
users,
|
||||||
|
stats.filter((x) => x.user === b.id),
|
||||||
|
) -
|
||||||
|
averageLevelCalculator(
|
||||||
|
users,
|
||||||
|
stats.filter((x) => x.user === a.id),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)}
|
||||||
|
columns={columns}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default StudentPerformanceList;
|
||||||
46
src/dashboards/MasterCorporate/StudentPerformancePage.tsx
Normal file
46
src/dashboards/MasterCorporate/StudentPerformancePage.tsx
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import useAssignments from "@/hooks/useAssignments";
|
||||||
|
import useFilterRecordsByUser from "@/hooks/useFilterRecordsByUser";
|
||||||
|
import useGroups from "@/hooks/useGroups";
|
||||||
|
import useUsers, {userHashCorporate, userHashStudent} from "@/hooks/useUsers";
|
||||||
|
import {Stat, User} from "@/interfaces/user";
|
||||||
|
import clsx from "clsx";
|
||||||
|
import {useRouter} from "next/router";
|
||||||
|
import {BsArrowLeft, BsArrowRepeat} from "react-icons/bs";
|
||||||
|
import StudentPerformanceList from "./StudentPerformanceList";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
user: User;
|
||||||
|
}
|
||||||
|
|
||||||
|
const StudentPerformancePage = ({user}: Props) => {
|
||||||
|
const {users: students} = useUsers(userHashStudent);
|
||||||
|
const {users: corporates} = useUsers(userHashCorporate);
|
||||||
|
const {groups} = useGroups({admin: user.id, userType: user.type});
|
||||||
|
const {data: stats} = useFilterRecordsByUser<Stat[]>();
|
||||||
|
|
||||||
|
const {reload: reloadAssignments, isLoading: isAssignmentsLoading} = useAssignments({corporate: user.id});
|
||||||
|
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="w-full flex justify-between items-center">
|
||||||
|
<div
|
||||||
|
onClick={() => router.push("/")}
|
||||||
|
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
||||||
|
<BsArrowLeft className="text-xl" />
|
||||||
|
<span>Back</span>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
onClick={reloadAssignments}
|
||||||
|
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
||||||
|
<span>Reload</span>
|
||||||
|
<BsArrowRepeat className={clsx("text-xl", isAssignmentsLoading && "animate-spin")} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<StudentPerformanceList items={students} stats={stats} users={corporates} groups={groups} />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default StudentPerformancePage;
|
||||||
442
src/dashboards/MasterCorporate/index.tsx
Normal file
442
src/dashboards/MasterCorporate/index.tsx
Normal file
@@ -0,0 +1,442 @@
|
|||||||
|
/* eslint-disable @next/next/no-img-element */
|
||||||
|
import Modal from "@/components/Modal";
|
||||||
|
import useFilterRecordsByUser from "@/hooks/useFilterRecordsByUser";
|
||||||
|
import useUsers from "@/hooks/useUsers";
|
||||||
|
import {CorporateUser, MasterCorporateUser, Stat, User} from "@/interfaces/user";
|
||||||
|
import UserList from "@/pages/(admin)/Lists/UserList";
|
||||||
|
import {dateSorter} from "@/utils";
|
||||||
|
import moment from "moment";
|
||||||
|
import {useEffect, useState, useMemo} from "react";
|
||||||
|
import {
|
||||||
|
BsArrowLeft,
|
||||||
|
BsClipboard2Data,
|
||||||
|
BsClock,
|
||||||
|
BsPaperclip,
|
||||||
|
BsPersonFill,
|
||||||
|
BsPencilSquare,
|
||||||
|
BsPersonCheck,
|
||||||
|
BsPeople,
|
||||||
|
BsBank,
|
||||||
|
BsEnvelopePaper,
|
||||||
|
BsArrowRepeat,
|
||||||
|
BsPersonFillGear,
|
||||||
|
BsDatabase,
|
||||||
|
} from "react-icons/bs";
|
||||||
|
import UserCard from "@/components/UserCard";
|
||||||
|
import useGroups from "@/hooks/useGroups";
|
||||||
|
|
||||||
|
import {averageLevelCalculator, calculateAverageLevel} from "@/utils/score";
|
||||||
|
import {groupByExam} from "@/utils/stats";
|
||||||
|
import IconCard from "../IconCard";
|
||||||
|
import GroupList from "@/pages/(admin)/Lists/GroupList";
|
||||||
|
import useFilterStore from "@/stores/listFilterStore";
|
||||||
|
import {useRouter} from "next/router";
|
||||||
|
import useAssignments from "@/hooks/useAssignments";
|
||||||
|
import {Assignment} from "@/interfaces/results";
|
||||||
|
import clsx from "clsx";
|
||||||
|
import {getCorporateUser} from "@/resources/user";
|
||||||
|
import {groupBy, uniqBy} from "lodash";
|
||||||
|
import MasterStatistical from "./MasterStatistical";
|
||||||
|
import {activeAssignmentFilter} from "@/utils/assignments";
|
||||||
|
import useUserBalance from "@/hooks/useUserBalance";
|
||||||
|
import AssignmentsPage from "../views/AssignmentsPage";
|
||||||
|
import StudentPerformanceList from "./StudentPerformanceList";
|
||||||
|
import StudentPerformancePage from "./StudentPerformancePage";
|
||||||
|
import MasterStatisticalPage from "./MasterStatisticalPage";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
user: MasterCorporateUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
const studentHash = {
|
||||||
|
type: "student",
|
||||||
|
size: 25,
|
||||||
|
orderBy: "registrationDate",
|
||||||
|
};
|
||||||
|
|
||||||
|
const teacherHash = {
|
||||||
|
type: "teacher",
|
||||||
|
size: 25,
|
||||||
|
orderBy: "registrationDate",
|
||||||
|
};
|
||||||
|
|
||||||
|
const corporateHash = {
|
||||||
|
type: "corporate",
|
||||||
|
size: 25,
|
||||||
|
orderBy: "registrationDate",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function MasterCorporateDashboard({user}: Props) {
|
||||||
|
const [selectedUser, setSelectedUser] = useState<User>();
|
||||||
|
const [showModal, setShowModal] = useState(false);
|
||||||
|
const [corporateAssignments, setCorporateAssignments] = useState<(Assignment & {corporate?: CorporateUser})[]>([]);
|
||||||
|
|
||||||
|
const {data: stats} = useFilterRecordsByUser<Stat[]>();
|
||||||
|
|
||||||
|
const {users: students, total: totalStudents, reload: reloadStudents, isLoading: isStudentsLoading} = useUsers(studentHash);
|
||||||
|
const {users: teachers, total: totalTeachers, reload: reloadTeachers, isLoading: isTeachersLoading} = useUsers(teacherHash);
|
||||||
|
const {users: corporates, total: totalCorporate, reload: reloadCorporates, isLoading: isCorporatesLoading} = useUsers(corporateHash);
|
||||||
|
|
||||||
|
const {groups} = useGroups({admin: user.id, userType: user.type});
|
||||||
|
const {balance} = useUserBalance();
|
||||||
|
|
||||||
|
const {assignments, isLoading: isAssignmentsLoading, reload: reloadAssignments} = useAssignments({corporate: user.id});
|
||||||
|
|
||||||
|
const assignmentsGroups = useMemo(() => groups.filter((x) => x.admin === user.id || x.participants.includes(user.id)), [groups, user.id]);
|
||||||
|
const assignmentsUsers = useMemo(
|
||||||
|
() =>
|
||||||
|
[...students, ...teachers].filter((x) =>
|
||||||
|
!!selectedUser
|
||||||
|
? groups
|
||||||
|
.filter((g) => g.admin === selectedUser.id)
|
||||||
|
.flatMap((g) => g.participants)
|
||||||
|
.includes(x.id) || false
|
||||||
|
: groups.flatMap((g) => g.participants).includes(x.id),
|
||||||
|
),
|
||||||
|
[groups, selectedUser, teachers, students],
|
||||||
|
);
|
||||||
|
|
||||||
|
const appendUserFilters = useFilterStore((state) => state.appendUserFilter);
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setShowModal(!!selectedUser && router.asPath === "/");
|
||||||
|
}, [selectedUser, router.asPath]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setCorporateAssignments(
|
||||||
|
assignments.filter(activeAssignmentFilter).map((a) => {
|
||||||
|
const assigner = [...teachers, ...corporates].find((x) => x.id === a.assigner);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...a,
|
||||||
|
corporate: assigner ? getCorporateUser(assigner, [...teachers, ...corporates], groups) : undefined,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}, [assignments, groups, teachers, corporates]);
|
||||||
|
const getStatsByStudent = (user: User) => stats.filter((s) => s.user === user.id);
|
||||||
|
|
||||||
|
const UserDisplay = (displayUser: User) => (
|
||||||
|
<div
|
||||||
|
onClick={() => setSelectedUser(displayUser)}
|
||||||
|
className="flex w-full p-4 gap-4 items-center hover:bg-mti-purple-ultralight cursor-pointer transition ease-in-out duration-300">
|
||||||
|
<img src={displayUser.profilePicture} alt={displayUser.name} className="rounded-full w-10 h-10" />
|
||||||
|
<div className="flex flex-col gap-1 items-start">
|
||||||
|
<span>{displayUser.name}</span>
|
||||||
|
<span className="text-sm opacity-75">{displayUser.email}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
const GroupsList = () => {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div
|
||||||
|
onClick={() => router.push("/")}
|
||||||
|
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
||||||
|
<BsArrowLeft className="text-xl" />
|
||||||
|
<span>Back</span>
|
||||||
|
</div>
|
||||||
|
<h2 className="text-2xl font-semibold">Groups ({groups.length})</h2>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<GroupList user={user} />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (router.asPath === "/#studentsPerformance") return <StudentPerformancePage user={user} />;
|
||||||
|
if (router.asPath === "/#statistical") return <MasterStatisticalPage />;
|
||||||
|
if (router.asPath === "/#groups") return <GroupsList />;
|
||||||
|
|
||||||
|
if (router.asPath === "/#students")
|
||||||
|
return (
|
||||||
|
<UserList
|
||||||
|
user={user}
|
||||||
|
type="student"
|
||||||
|
renderHeader={(total) => (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div
|
||||||
|
onClick={() => router.push("/")}
|
||||||
|
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
||||||
|
<BsArrowLeft className="text-xl" />
|
||||||
|
<span>Back</span>
|
||||||
|
</div>
|
||||||
|
<h2 className="text-2xl font-semibold">Students ({total})</h2>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
if (router.asPath === "/#assignments")
|
||||||
|
return (
|
||||||
|
<AssignmentsPage
|
||||||
|
assignments={assignments}
|
||||||
|
corporateAssignments={corporateAssignments}
|
||||||
|
groups={assignmentsGroups}
|
||||||
|
user={user}
|
||||||
|
users={assignmentsUsers}
|
||||||
|
reloadAssignments={reloadAssignments}
|
||||||
|
isLoading={isAssignmentsLoading}
|
||||||
|
onBack={() => router.push("/")}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
if (router.asPath === "/#corporate")
|
||||||
|
return (
|
||||||
|
<UserList
|
||||||
|
user={user}
|
||||||
|
type="corporate"
|
||||||
|
renderHeader={(total) => (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div
|
||||||
|
onClick={() => router.push("/")}
|
||||||
|
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
||||||
|
<BsArrowLeft className="text-xl" />
|
||||||
|
<span>Back</span>
|
||||||
|
</div>
|
||||||
|
<h2 className="text-2xl font-semibold">Corporate ({total})</h2>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
if (router.asPath === "/#students")
|
||||||
|
return (
|
||||||
|
<UserList
|
||||||
|
user={user}
|
||||||
|
type="student"
|
||||||
|
renderHeader={(total) => (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div
|
||||||
|
onClick={() => router.push("/")}
|
||||||
|
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
||||||
|
<BsArrowLeft className="text-xl" />
|
||||||
|
<span>Back</span>
|
||||||
|
</div>
|
||||||
|
<h2 className="text-2xl font-semibold">Students ({total})</h2>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
if (router.asPath === "/#teachers")
|
||||||
|
return (
|
||||||
|
<UserList
|
||||||
|
user={user}
|
||||||
|
type="teacher"
|
||||||
|
renderHeader={(total) => (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div
|
||||||
|
onClick={() => router.push("/")}
|
||||||
|
className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300">
|
||||||
|
<BsArrowLeft className="text-xl" />
|
||||||
|
<span>Back</span>
|
||||||
|
</div>
|
||||||
|
<h2 className="text-2xl font-semibold">Teachers ({total})</h2>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Modal isOpen={showModal} onClose={() => setSelectedUser(undefined)}>
|
||||||
|
<>
|
||||||
|
{selectedUser && (
|
||||||
|
<div className="w-full flex flex-col gap-8">
|
||||||
|
<UserCard
|
||||||
|
maxUserAmount={
|
||||||
|
user.type === "mastercorporate"
|
||||||
|
? (user.corporateInformation?.companyInformation?.userAmount || 0) - balance
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
loggedInUser={user}
|
||||||
|
onClose={(shouldReload) => {
|
||||||
|
setSelectedUser(undefined);
|
||||||
|
if (shouldReload && selectedUser!.type === "student") reloadStudents();
|
||||||
|
if (shouldReload && selectedUser!.type === "teacher") reloadTeachers();
|
||||||
|
if (shouldReload && selectedUser!.type === "corporate") reloadCorporates();
|
||||||
|
}}
|
||||||
|
onViewStudents={
|
||||||
|
selectedUser.type === "corporate" || selectedUser.type === "teacher"
|
||||||
|
? () => {
|
||||||
|
appendUserFilters({
|
||||||
|
id: "view-students",
|
||||||
|
filter: (x: User) => x.type === "student",
|
||||||
|
});
|
||||||
|
appendUserFilters({
|
||||||
|
id: "belongs-to-admin",
|
||||||
|
filter: (x: User) =>
|
||||||
|
groups
|
||||||
|
.filter((g) => g.admin === selectedUser.id || g.participants.includes(selectedUser.id))
|
||||||
|
.flatMap((g) => g.participants)
|
||||||
|
.includes(x.id),
|
||||||
|
});
|
||||||
|
|
||||||
|
router.push("/list/users");
|
||||||
|
}
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
onViewTeachers={
|
||||||
|
selectedUser.type === "corporate" || selectedUser.type === "student"
|
||||||
|
? () => {
|
||||||
|
appendUserFilters({
|
||||||
|
id: "view-teachers",
|
||||||
|
filter: (x: User) => x.type === "teacher",
|
||||||
|
});
|
||||||
|
appendUserFilters({
|
||||||
|
id: "belongs-to-admin",
|
||||||
|
filter: (x: User) =>
|
||||||
|
groups
|
||||||
|
.filter((g) => g.admin === selectedUser.id || g.participants.includes(selectedUser.id))
|
||||||
|
.flatMap((g) => g.participants)
|
||||||
|
.includes(x.id),
|
||||||
|
});
|
||||||
|
|
||||||
|
router.push("/list/users");
|
||||||
|
}
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
user={selectedUser}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
</Modal>
|
||||||
|
|
||||||
|
<>
|
||||||
|
<section className="flex flex-wrap gap-2 items-center -lg:justify-center lg:justify-between text-center">
|
||||||
|
<IconCard
|
||||||
|
onClick={() => router.push("/#students")}
|
||||||
|
Icon={BsPersonFill}
|
||||||
|
isLoading={isStudentsLoading}
|
||||||
|
label="Students"
|
||||||
|
value={totalStudents}
|
||||||
|
color="purple"
|
||||||
|
/>
|
||||||
|
<IconCard
|
||||||
|
onClick={() => router.push("/#teachers")}
|
||||||
|
Icon={BsPencilSquare}
|
||||||
|
isLoading={isTeachersLoading}
|
||||||
|
label="Teachers"
|
||||||
|
value={totalTeachers}
|
||||||
|
color="purple"
|
||||||
|
/>
|
||||||
|
<IconCard
|
||||||
|
Icon={BsClipboard2Data}
|
||||||
|
label="Exams Performed"
|
||||||
|
value={stats.filter((s) => groups.flatMap((g) => g.participants).includes(s.user)).length}
|
||||||
|
color="purple"
|
||||||
|
/>
|
||||||
|
<IconCard
|
||||||
|
Icon={BsPaperclip}
|
||||||
|
label="Average Level"
|
||||||
|
value={averageLevelCalculator(
|
||||||
|
students,
|
||||||
|
stats.filter((s) => groups.flatMap((g) => g.participants).includes(s.user)),
|
||||||
|
).toFixed(1)}
|
||||||
|
color="purple"
|
||||||
|
/>
|
||||||
|
<IconCard onClick={() => router.push("/#groups")} Icon={BsPeople} label="Groups" value={groups.length} color="purple" />
|
||||||
|
<IconCard
|
||||||
|
Icon={BsPersonCheck}
|
||||||
|
label="User Balance"
|
||||||
|
value={`${balance}/${user.corporateInformation?.companyInformation?.userAmount || 0}`}
|
||||||
|
color="purple"
|
||||||
|
/>
|
||||||
|
<IconCard
|
||||||
|
Icon={BsClock}
|
||||||
|
label="Expiration Date"
|
||||||
|
value={user.subscriptionExpirationDate ? moment(user.subscriptionExpirationDate).format("DD/MM/yyyy") : "Unlimited"}
|
||||||
|
color="rose"
|
||||||
|
/>
|
||||||
|
<IconCard
|
||||||
|
Icon={BsBank}
|
||||||
|
label="Corporate Accounts"
|
||||||
|
value={totalCorporate}
|
||||||
|
isLoading={isCorporatesLoading}
|
||||||
|
color="purple"
|
||||||
|
onClick={() => router.push("/#corporate")}
|
||||||
|
/>
|
||||||
|
<IconCard Icon={BsBank} label="Corporate" value={totalCorporate} isLoading={isCorporatesLoading} color="purple" />
|
||||||
|
<IconCard
|
||||||
|
Icon={BsPersonFillGear}
|
||||||
|
isLoading={isStudentsLoading}
|
||||||
|
label="Student Performance"
|
||||||
|
value={students.length}
|
||||||
|
color="purple"
|
||||||
|
onClick={() => router.push("/#studentsPerformance")}
|
||||||
|
/>
|
||||||
|
<IconCard
|
||||||
|
Icon={BsDatabase}
|
||||||
|
label="Master Statistical"
|
||||||
|
// value={masterCorporateUserGroups.length}
|
||||||
|
color="purple"
|
||||||
|
onClick={() => router.push("/#statistical")}
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
disabled={isAssignmentsLoading}
|
||||||
|
onClick={() => router.push("/#assignments")}
|
||||||
|
className="bg-white col-span-2 rounded-xl shadow p-4 flex flex-col gap-4 items-center w-96 h-52 justify-center cursor-pointer hover:shadow-xl transition ease-in-out duration-300">
|
||||||
|
<BsEnvelopePaper className="text-6xl text-mti-purple-light" />
|
||||||
|
<span className="flex flex-col gap-1 items-center text-xl">
|
||||||
|
<span className="text-lg">Assignments</span>
|
||||||
|
<span className="font-semibold text-mti-purple-light">
|
||||||
|
{isAssignmentsLoading ? "Loading..." : assignments.filter((a) => !a.archived).length}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section className="grid grid-cols-1 md:grid-cols-2 gap-4 w-full justify-between">
|
||||||
|
<div className="bg-white shadow flex flex-col rounded-xl w-full">
|
||||||
|
<span className="p-4">Latest students</span>
|
||||||
|
<div className="flex flex-col items-start h-96 overflow-scroll scrollbar-hide">
|
||||||
|
{students
|
||||||
|
.sort((a, b) => dateSorter(a, b, "desc", "registrationDate"))
|
||||||
|
.map((x) => (
|
||||||
|
<UserDisplay key={x.id} {...x} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="bg-white shadow flex flex-col rounded-xl w-full">
|
||||||
|
<span className="p-4">Latest teachers</span>
|
||||||
|
<div className="flex flex-col items-start h-96 overflow-scroll scrollbar-hide">
|
||||||
|
{teachers
|
||||||
|
.sort((a, b) => dateSorter(a, b, "desc", "registrationDate"))
|
||||||
|
.map((x) => (
|
||||||
|
<UserDisplay key={x.id} {...x} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="bg-white shadow flex flex-col rounded-xl w-full">
|
||||||
|
<span className="p-4">Highest level students</span>
|
||||||
|
<div className="flex flex-col items-start h-96 overflow-scroll scrollbar-hide">
|
||||||
|
{students
|
||||||
|
.sort((a, b) => calculateAverageLevel(b.levels) - calculateAverageLevel(a.levels))
|
||||||
|
.map((x) => (
|
||||||
|
<UserDisplay key={x.id} {...x} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="bg-white shadow flex flex-col rounded-xl w-full">
|
||||||
|
<span className="p-4">Highest exam count students</span>
|
||||||
|
<div className="flex flex-col items-start h-96 overflow-scroll scrollbar-hide">
|
||||||
|
{students
|
||||||
|
.sort(
|
||||||
|
(a, b) =>
|
||||||
|
Object.keys(groupByExam(getStatsByStudent(b))).length - Object.keys(groupByExam(getStatsByStudent(a))).length,
|
||||||
|
)
|
||||||
|
.map((x) => (
|
||||||
|
<UserDisplay key={x.id} {...x} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
</>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,424 +0,0 @@
|
|||||||
import React from "react";
|
|
||||||
import { CorporateUser, User } from "@/interfaces/user";
|
|
||||||
import { BsFileExcel, BsBank, BsPersonFill } from "react-icons/bs";
|
|
||||||
import IconCard from "./IconCard";
|
|
||||||
|
|
||||||
import useAssignmentsCorporates from "@/hooks/useAssignmentCorporates";
|
|
||||||
import ReactDatePicker from "react-datepicker";
|
|
||||||
|
|
||||||
import moment from "moment";
|
|
||||||
import { AssignmentWithCorporateId } from "@/interfaces/results";
|
|
||||||
import {
|
|
||||||
flexRender,
|
|
||||||
createColumnHelper,
|
|
||||||
getCoreRowModel,
|
|
||||||
useReactTable,
|
|
||||||
} from "@tanstack/react-table";
|
|
||||||
import Checkbox from "@/components/Low/Checkbox";
|
|
||||||
import { useListSearch } from "@/hooks/useListSearch";
|
|
||||||
import axios from "axios";
|
|
||||||
import { toast } from "react-toastify";
|
|
||||||
import Button from "@/components/Low/Button";
|
|
||||||
|
|
||||||
interface GroupedCorporateUsers {
|
|
||||||
// list of user Ids
|
|
||||||
[key: string]: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
corporateUsers: GroupedCorporateUsers;
|
|
||||||
users: User[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface TableData {
|
|
||||||
user: string;
|
|
||||||
email: string;
|
|
||||||
correct: number;
|
|
||||||
corporate: string;
|
|
||||||
submitted: boolean;
|
|
||||||
date: moment.Moment;
|
|
||||||
assignment: string;
|
|
||||||
corporateId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface UserCount {
|
|
||||||
userCount: number;
|
|
||||||
maxUserCount: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
const searchFilters = [["email"], ["user"], ["userId"]];
|
|
||||||
|
|
||||||
const MasterStatistical = (props: Props) => {
|
|
||||||
const { users, corporateUsers } = props;
|
|
||||||
|
|
||||||
// const corporateRelevantUsers = React.useMemo(
|
|
||||||
// () => corporateUsers.filter((x) => x.type !== "student") as CorporateUser[],
|
|
||||||
// [corporateUsers]
|
|
||||||
// );
|
|
||||||
|
|
||||||
const corporates = React.useMemo(
|
|
||||||
() => Object.values(corporateUsers).flat(),
|
|
||||||
[corporateUsers]
|
|
||||||
);
|
|
||||||
|
|
||||||
const [selectedCorporates, setSelectedCorporates] =
|
|
||||||
React.useState<string[]>(corporates);
|
|
||||||
const [startDate, setStartDate] = React.useState<Date | null>(
|
|
||||||
moment("01/01/2023").toDate()
|
|
||||||
);
|
|
||||||
const [endDate, setEndDate] = React.useState<Date | null>(
|
|
||||||
moment().endOf("year").toDate()
|
|
||||||
);
|
|
||||||
|
|
||||||
const { assignments } = useAssignmentsCorporates({
|
|
||||||
// corporates: [...corporates, "tYU0HTiJdjMsS8SB7XJsUdMMP892"],
|
|
||||||
corporates: selectedCorporates,
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
});
|
|
||||||
|
|
||||||
const [downloading, setDownloading] = React.useState<boolean>(false);
|
|
||||||
|
|
||||||
const tableResults = React.useMemo(
|
|
||||||
() =>
|
|
||||||
assignments.reduce((accmA: TableData[], a: AssignmentWithCorporateId) => {
|
|
||||||
const userResults = a.assignees.map((assignee) => {
|
|
||||||
const userStats =
|
|
||||||
a.results.find((r) => r.user === assignee)?.stats || [];
|
|
||||||
const userData = users.find((u) => u.id === assignee);
|
|
||||||
const corporate = users.find((u) => u.id === a.assigner)?.name || "";
|
|
||||||
const commonData = {
|
|
||||||
user: userData?.name || "",
|
|
||||||
email: userData?.email || "",
|
|
||||||
userId: assignee,
|
|
||||||
corporateId: a.corporateId,
|
|
||||||
corporate,
|
|
||||||
assignment: a.name,
|
|
||||||
};
|
|
||||||
if (userStats.length === 0) {
|
|
||||||
return {
|
|
||||||
...commonData,
|
|
||||||
correct: 0,
|
|
||||||
submitted: false,
|
|
||||||
// date: moment(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...commonData,
|
|
||||||
correct: userStats.reduce((n, e) => n + e.score.correct, 0),
|
|
||||||
submitted: true,
|
|
||||||
date: moment.max(userStats.map((e) => moment(e.date))),
|
|
||||||
};
|
|
||||||
}) as TableData[];
|
|
||||||
|
|
||||||
return [...accmA, ...userResults];
|
|
||||||
}, []),
|
|
||||||
[assignments, users]
|
|
||||||
);
|
|
||||||
|
|
||||||
const getCorporateScores = (corporateId: string): UserCount => {
|
|
||||||
const corporateAssignmentsUsers = assignments
|
|
||||||
.filter((a) => a.corporateId === corporateId)
|
|
||||||
.reduce((acc, a) => acc + a.assignees.length, 0);
|
|
||||||
|
|
||||||
const corporateResults = tableResults.filter(
|
|
||||||
(r) => r.corporateId === corporateId
|
|
||||||
).length;
|
|
||||||
|
|
||||||
return {
|
|
||||||
maxUserCount: corporateAssignmentsUsers,
|
|
||||||
userCount: corporateResults,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const getCorporatesScoresHash = (data: string[]) =>
|
|
||||||
data.reduce(
|
|
||||||
(accm, id) => ({
|
|
||||||
...accm,
|
|
||||||
[id]: getCorporateScores(id),
|
|
||||||
}),
|
|
||||||
{}
|
|
||||||
) as Record<string, UserCount>;
|
|
||||||
|
|
||||||
const getConsolidateScore = (data: Record<string, UserCount>) =>
|
|
||||||
Object.values(data).reduce(
|
|
||||||
(acc: UserCount, { userCount, maxUserCount }: UserCount) => ({
|
|
||||||
userCount: acc.userCount + userCount,
|
|
||||||
maxUserCount: acc.maxUserCount + maxUserCount,
|
|
||||||
}),
|
|
||||||
{ userCount: 0, maxUserCount: 0 }
|
|
||||||
);
|
|
||||||
|
|
||||||
const corporateScores = getCorporatesScoresHash(corporates);
|
|
||||||
const consolidateScore = getConsolidateScore(corporateScores);
|
|
||||||
|
|
||||||
const getConsolidateScoreStr = (data: UserCount) =>
|
|
||||||
`${data.userCount}/${data.maxUserCount}`;
|
|
||||||
|
|
||||||
const columnHelper = createColumnHelper<TableData>();
|
|
||||||
|
|
||||||
const defaultColumns = [
|
|
||||||
columnHelper.accessor("user", {
|
|
||||||
header: "User",
|
|
||||||
id: "user",
|
|
||||||
cell: (info) => {
|
|
||||||
return <span>{info.getValue()}</span>;
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("email", {
|
|
||||||
header: "Email",
|
|
||||||
id: "email",
|
|
||||||
cell: (info) => {
|
|
||||||
return <span>{info.getValue()}</span>;
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("corporate", {
|
|
||||||
header: "Corporate",
|
|
||||||
id: "corporate",
|
|
||||||
cell: (info) => {
|
|
||||||
return <span>{info.getValue()}</span>;
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("assignment", {
|
|
||||||
header: "Assignment",
|
|
||||||
id: "assignment",
|
|
||||||
cell: (info) => {
|
|
||||||
return <span>{info.getValue()}</span>;
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("submitted", {
|
|
||||||
header: "Submitted",
|
|
||||||
id: "submitted",
|
|
||||||
cell: (info) => {
|
|
||||||
return (
|
|
||||||
<Checkbox isChecked={info.getValue()} disabled onChange={() => {}}>
|
|
||||||
<span></span>
|
|
||||||
</Checkbox>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("correct", {
|
|
||||||
header: "Correct",
|
|
||||||
id: "correct",
|
|
||||||
cell: (info) => {
|
|
||||||
return <span>{info.getValue()}</span>;
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("date", {
|
|
||||||
header: "Date",
|
|
||||||
id: "date",
|
|
||||||
cell: (info) => {
|
|
||||||
const date = info.getValue();
|
|
||||||
if (date) {
|
|
||||||
return <span>{date.format("DD/MM/YYYY")}</span>;
|
|
||||||
}
|
|
||||||
|
|
||||||
return <span>{""}</span>;
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
];
|
|
||||||
|
|
||||||
const {
|
|
||||||
rows: filteredRows,
|
|
||||||
renderSearch,
|
|
||||||
text: searchText,
|
|
||||||
} = useListSearch(searchFilters, tableResults);
|
|
||||||
|
|
||||||
const table = useReactTable({
|
|
||||||
data: filteredRows,
|
|
||||||
columns: defaultColumns,
|
|
||||||
getCoreRowModel: getCoreRowModel(),
|
|
||||||
});
|
|
||||||
|
|
||||||
const areAllSelected = selectedCorporates.length === corporates.length;
|
|
||||||
|
|
||||||
const getStudentsConsolidateScore = () => {
|
|
||||||
if (tableResults.length === 0) {
|
|
||||||
return { highest: null, lowest: null };
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the student with the highest and lowest score
|
|
||||||
return tableResults.reduce(
|
|
||||||
(acc, curr) => {
|
|
||||||
if (curr.correct > acc.highest.correct) {
|
|
||||||
acc.highest = curr;
|
|
||||||
}
|
|
||||||
if (curr.correct < acc.lowest.correct) {
|
|
||||||
acc.lowest = curr;
|
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
},
|
|
||||||
{ highest: tableResults[0], lowest: tableResults[0] }
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const triggerDownload = async () => {
|
|
||||||
try {
|
|
||||||
setDownloading(true);
|
|
||||||
const res = await axios.post("/api/assignments/statistical/excel", {
|
|
||||||
ids: selectedCorporates,
|
|
||||||
...(startDate ? { startDate: startDate.toISOString() } : {}),
|
|
||||||
...(endDate ? { endDate: endDate.toISOString() } : {}),
|
|
||||||
searchText,
|
|
||||||
});
|
|
||||||
toast.success("Report ready!");
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = res.data;
|
|
||||||
// download should have worked but there are some CORS issues
|
|
||||||
// https://firebase.google.com/docs/storage/web/download-files#cors_configuration
|
|
||||||
// link.download="report.pdf";
|
|
||||||
link.target = "_blank";
|
|
||||||
link.rel = "noreferrer";
|
|
||||||
link.click();
|
|
||||||
setDownloading(false);
|
|
||||||
} catch (err) {
|
|
||||||
toast.error("Failed to display the report!");
|
|
||||||
console.error(err);
|
|
||||||
setDownloading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const consolidateResults = getStudentsConsolidateScore();
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<div className="flex flex-wrap gap-2 items-center text-center">
|
|
||||||
<IconCard
|
|
||||||
Icon={BsBank}
|
|
||||||
label="Consolidate"
|
|
||||||
value={getConsolidateScoreStr(consolidateScore)}
|
|
||||||
color="purple"
|
|
||||||
onClick={() => {
|
|
||||||
if (areAllSelected) {
|
|
||||||
setSelectedCorporates([]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setSelectedCorporates(corporates);
|
|
||||||
}}
|
|
||||||
isSelected={areAllSelected}
|
|
||||||
/>
|
|
||||||
{Object.keys(corporateUsers).map((corporateName) => {
|
|
||||||
const group = corporateUsers[corporateName];
|
|
||||||
const isSelected = group.every((id) =>
|
|
||||||
selectedCorporates.includes(id)
|
|
||||||
);
|
|
||||||
|
|
||||||
const valueHash = getCorporatesScoresHash(group);
|
|
||||||
const value = getConsolidateScoreStr(getConsolidateScore(valueHash));
|
|
||||||
return (
|
|
||||||
<IconCard
|
|
||||||
key={corporateName}
|
|
||||||
Icon={BsBank}
|
|
||||||
label={corporateName}
|
|
||||||
value={value}
|
|
||||||
color="purple"
|
|
||||||
onClick={() => {
|
|
||||||
if (isSelected) {
|
|
||||||
setSelectedCorporates((prev) =>
|
|
||||||
prev.filter((x) => !group.includes(x))
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setSelectedCorporates((prev) => [
|
|
||||||
...new Set([...prev, ...group]),
|
|
||||||
]);
|
|
||||||
}}
|
|
||||||
isSelected={isSelected}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
<div className="flex gap-3 w-full">
|
|
||||||
<div className="flex flex-col gap-3">
|
|
||||||
<label className="font-normal text-base text-mti-gray-dim">
|
|
||||||
Date
|
|
||||||
</label>
|
|
||||||
<ReactDatePicker
|
|
||||||
dateFormat="dd/MM/yyyy"
|
|
||||||
className="px-4 py-6 w-52 text-sm text-center font-normal placeholder:text-mti-gray-cool disabled:bg-mti-gray-platinum/40 disabled:text-mti-gray-dim disabled:cursor-not-allowed rounded-full border border-mti-gray-platinum focus:outline-none"
|
|
||||||
selected={startDate}
|
|
||||||
startDate={startDate}
|
|
||||||
endDate={endDate}
|
|
||||||
selectsRange
|
|
||||||
showMonthDropdown
|
|
||||||
onChange={([initialDate, finalDate]: [Date, Date]) => {
|
|
||||||
setStartDate(initialDate ?? moment("01/01/2023").toDate());
|
|
||||||
if (finalDate) {
|
|
||||||
// basicly selecting a final day works as if I'm selecting the first
|
|
||||||
// minute of that day. this way it covers the whole day
|
|
||||||
setEndDate(moment(finalDate).endOf("day").toDate());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setEndDate(null);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
{renderSearch()}
|
|
||||||
<div className="flex flex-col gap-3 justify-end">
|
|
||||||
<Button
|
|
||||||
className="max-w-[200px] h-[70px]"
|
|
||||||
variant="outline"
|
|
||||||
onClick={triggerDownload}
|
|
||||||
>
|
|
||||||
Download
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<table className="rounded-xl h-full bg-mti-purple-ultralight/40 w-full">
|
|
||||||
<thead>
|
|
||||||
{table.getHeaderGroups().map((headerGroup) => (
|
|
||||||
<tr key={headerGroup.id}>
|
|
||||||
{headerGroup.headers.map((header) => (
|
|
||||||
<th className="p-4 text-left" key={header.id}>
|
|
||||||
{header.isPlaceholder
|
|
||||||
? null
|
|
||||||
: flexRender(
|
|
||||||
header.column.columnDef.header,
|
|
||||||
header.getContext()
|
|
||||||
)}
|
|
||||||
</th>
|
|
||||||
))}
|
|
||||||
</tr>
|
|
||||||
))}
|
|
||||||
</thead>
|
|
||||||
<tbody className="px-2">
|
|
||||||
{table.getRowModel().rows.map((row) => (
|
|
||||||
<tr
|
|
||||||
className="odd:bg-white even:bg-mti-purple-ultralight/40 rounded-lg py-2"
|
|
||||||
key={row.id}
|
|
||||||
>
|
|
||||||
{row.getVisibleCells().map((cell) => (
|
|
||||||
<td className="px-4 py-2" key={cell.id}>
|
|
||||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
||||||
</td>
|
|
||||||
))}
|
|
||||||
</tr>
|
|
||||||
))}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
<div className="flex flex-wrap gap-2 items-center text-center">
|
|
||||||
{consolidateResults.highest && (
|
|
||||||
<IconCard
|
|
||||||
onClick={() => {}}
|
|
||||||
Icon={BsPersonFill}
|
|
||||||
label={`Highest result: ${consolidateResults.highest.user}`}
|
|
||||||
color="purple"
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{consolidateResults.lowest && (
|
|
||||||
<IconCard
|
|
||||||
onClick={() => {}}
|
|
||||||
Icon={BsPersonFill}
|
|
||||||
label={`Lowest result: ${consolidateResults.lowest.user}`}
|
|
||||||
color="purple"
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default MasterStatistical;
|
|
||||||
@@ -9,7 +9,7 @@ export const userHashStudent = {type: "student"} as {type: Type};
|
|||||||
export const userHashTeacher = {type: "teacher"} as {type: Type};
|
export const userHashTeacher = {type: "teacher"} as {type: Type};
|
||||||
export const userHashCorporate = {type: "corporate"} as {type: Type};
|
export const userHashCorporate = {type: "corporate"} as {type: Type};
|
||||||
|
|
||||||
export default function useUsers(props?: {type?: Type; page?: number; size?: number}) {
|
export default function useUsers(props?: {type?: string; page?: number; size?: number; orderBy?: string; direction?: "asc" | "desc"}) {
|
||||||
const [users, setUsers] = useState<User[]>([]);
|
const [users, setUsers] = useState<User[]>([]);
|
||||||
const [total, setTotal] = useState(0);
|
const [total, setTotal] = useState(0);
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
@@ -35,7 +35,7 @@ export default function useUsers(props?: {type?: Type; page?: number; size?: num
|
|||||||
.finally(() => setIsLoading(false));
|
.finally(() => setIsLoading(false));
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(getData, [props?.page, props?.size, props?.type]);
|
useEffect(getData, [props?.page, props?.size, props?.type, props?.orderBy, props?.direction]);
|
||||||
|
|
||||||
return {users, total, isLoading, isError, reload: getData};
|
return {users, total, isLoading, isError, reload: getData};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,13 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const {size, type, page} = req.query as {size?: string; type?: Type; page?: string};
|
const {
|
||||||
console.log(size, type, page);
|
size,
|
||||||
|
type,
|
||||||
|
page,
|
||||||
|
orderBy,
|
||||||
|
direction = "desc",
|
||||||
|
} = req.query as {size?: string; type?: Type; page?: string; orderBy?: string; direction?: "asc" | "desc"};
|
||||||
|
|
||||||
const {users, total} = await getLinkedUsers(
|
const {users, total} = await getLinkedUsers(
|
||||||
req.session.user?.id,
|
req.session.user?.id,
|
||||||
@@ -22,6 +27,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
type,
|
type,
|
||||||
page !== undefined ? parseInt(page) : undefined,
|
page !== undefined ? parseInt(page) : undefined,
|
||||||
size !== undefined ? parseInt(size) : undefined,
|
size !== undefined ? parseInt(size) : undefined,
|
||||||
|
orderBy,
|
||||||
|
direction,
|
||||||
);
|
);
|
||||||
|
|
||||||
res.status(200).json({users, total});
|
res.status(200).json({users, total});
|
||||||
|
|||||||
@@ -44,7 +44,15 @@ export async function getSpecificUsers(ids: string[]) {
|
|||||||
.toArray();
|
.toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getLinkedUsers(userID?: string, userType?: Type, type?: Type, page?: number, size?: number) {
|
export async function getLinkedUsers(
|
||||||
|
userID?: string,
|
||||||
|
userType?: Type,
|
||||||
|
type?: Type,
|
||||||
|
page?: number,
|
||||||
|
size?: number,
|
||||||
|
sort?: string,
|
||||||
|
direction?: "asc" | "desc",
|
||||||
|
) {
|
||||||
const filters = {
|
const filters = {
|
||||||
...(!!type ? {type} : {}),
|
...(!!type ? {type} : {}),
|
||||||
};
|
};
|
||||||
@@ -53,6 +61,7 @@ export async function getLinkedUsers(userID?: string, userType?: Type, type?: Ty
|
|||||||
const users = await db
|
const users = await db
|
||||||
.collection("users")
|
.collection("users")
|
||||||
.find<User>(filters)
|
.find<User>(filters)
|
||||||
|
.sort(sort ? {[sort]: direction === "desc" ? -1 : 1} : {})
|
||||||
.skip(page && size ? page * size : 0)
|
.skip(page && size ? page * size : 0)
|
||||||
.limit(size || 0)
|
.limit(size || 0)
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|||||||
Reference in New Issue
Block a user