Files
Checkmate/client/src/pages/teams/Teams.tsx
T

125 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-11-13 13:47:26 -08:00
import { BasePage, InfoBox } from "@/components/design-elements";
import { Table } from "@/components/design-elements";
2025-11-28 22:48:32 +00:00
import { HeaderCreate } from "@/components/common";
2025-11-13 13:47:26 -08:00
import { ActionsMenu } from "@/components/actions-menu";
import { Dialog } from "@/components/inputs";
2025-12-12 12:17:18 -08:00
import Typography from "@mui/material/Typography";
2025-11-13 13:47:26 -08:00
import { useNavigate } from "react-router";
import { useTranslation } from "react-i18next";
import type { ActionMenuItem } from "@/components/actions-menu";
import type { Header } from "@/components/design-elements/Table";
import type { ApiResponse } from "@/hooks/UseApi";
import { useGet, useDelete } from "@/hooks/UseApi";
import type { ITeam } from "@/types/team";
import { useState } from "react";
import { mutate } from "swr";
2025-12-12 12:17:18 -08:00
import { useTheme } from "@mui/material/styles";
2025-11-13 13:47:26 -08:00
const TeamsPage = () => {
2025-12-12 12:17:18 -08:00
const theme = useTheme();
2025-11-13 13:47:26 -08:00
const navigate = useNavigate();
const [selectedTeam, setSelectedTeam] = useState<ITeam | null>(null);
const isDialogOpen = Boolean(selectedTeam);
const { t } = useTranslation();
const { response, loading, refetch } = useGet<ApiResponse<any>>("/teams");
2025-12-08 21:40:04 +00:00
const { deleteFn, loading: isDeleting } = useDelete();
2025-11-13 13:47:26 -08:00
const teams = response?.data || [];
const handleConfirm = async () => {
await deleteFn(`/teams/${selectedTeam?._id}`);
setSelectedTeam(null);
refetch();
mutate("/teams/joined");
};
const handleCancel = () => {
setSelectedTeam(null);
};
const getActions = (team: ITeam): ActionMenuItem[] => {
return [
{
id: 1,
2025-12-12 12:17:18 -08:00
label: t("monitors.common.actions.configure"),
2025-11-13 13:47:26 -08:00
action: () => {
navigate(`/teams/${team._id}/configure`);
},
closeMenu: true,
},
{
id: 2,
2025-12-12 12:17:18 -08:00
label: (
<Typography color={theme.palette.error.main}>
{t("common.buttons.delete")}
</Typography>
),
2025-11-13 13:47:26 -08:00
action: () => {
setSelectedTeam(team);
},
closeMenu: true,
},
];
};
const getHeaders = () => {
const headers: Header<ITeam>[] = [
{
id: "name",
2025-12-12 12:17:18 -08:00
content: t("common.table.headers.name"),
2025-11-13 13:47:26 -08:00
render: (row) => {
return row.name;
},
},
{
id: "description",
2025-12-12 12:17:18 -08:00
content: t("teams.table.headers.teamDescription"),
2025-11-13 13:47:26 -08:00
render: (row) => {
return row.description;
},
},
{
id: "actions",
2025-12-12 12:17:18 -08:00
content: t("common.table.headers.actions"),
2025-11-13 13:47:26 -08:00
render: (row) => {
return <ActionsMenu items={getActions(row)} />;
},
},
];
return headers;
};
const headers = getHeaders();
return (
<BasePage>
<InfoBox
2025-12-12 12:17:18 -08:00
title={t("teams.infoBox.title")}
description={t("teams.infoBox.description")}
2025-11-13 13:47:26 -08:00
/>
<HeaderCreate
label="Create new team"
isLoading={loading}
path="/teams/create"
2025-11-28 22:48:32 +00:00
entitlement="teamsMax"
entitlementCount={teams.length}
2025-11-13 13:47:26 -08:00
/>
<Table
headers={headers}
data={teams}
onRowClick={(row) => {
navigate(`/teams/${row._id}`);
}}
/>
<Dialog
open={isDialogOpen}
2025-12-12 12:17:18 -08:00
title={t("common.dialog.delete.title")}
content={t("common.dialog.delete.description")}
2025-11-13 13:47:26 -08:00
onConfirm={handleConfirm}
onCancel={handleCancel}
2025-12-08 21:40:04 +00:00
loading={isDeleting}
2025-11-13 13:47:26 -08:00
/>
</BasePage>
);
};
export default TeamsPage;