school-dashboard-exercise/src/Pages/lesson/useTableColumns.tsx
2024-07-27 14:57:06 +03:00

117 lines
3.3 KiB
TypeScript

import { Space, TableColumnsType, Tooltip } from "antd";
import { Lesson } from "../../types/Item";
import { FaPlus } from "react-icons/fa";
import useModalHandler from "../../utils/useModalHandler";
import { ModalEnum } from "../../enums/Model";
import { useObjectToEdit } from "../../zustand/ObjectToEditState";
import { RiDeleteBin6Fill } from "react-icons/ri";
import { MdOutlineEdit } from "react-icons/md";
import { findLabelByValue } from "../../utils/findLabelByValue";
import { useTranslation } from "react-i18next";
import { hasAbility } from "../../utils/hasAbility";
import { ABILITIES_ENUM, ABILITIES_VALUES_ENUM } from "../../enums/abilities";
import { formatNumber } from "../../utils/formatNumber";
import { BsEyeFill } from "react-icons/bs";
import { useNavigate } from "react-router-dom";
import { canAddLesson, canDeleteLesson, canEditLesson, canShowLesson } from "../../utils/hasAbilityFn";
export const useColumns = () => {
const { handel_open_model } = useModalHandler();
const { setObjectToEdit } = useObjectToEdit((state) => state);
const navigate = useNavigate()
const handelShow = (record: any) => {
navigate(`${ABILITIES_ENUM.LESSON}/${record?.id}`);
};
const handelDelete = (data: any) => {
setObjectToEdit(data);
handel_open_model(ModalEnum?.LESSON_DELETE);
};
const handleEdit = (record: any) => {
setObjectToEdit(record);
handel_open_model(ModalEnum?.LESSON_EDIT);
};
const [t] = useTranslation();
const columns: TableColumnsType<Lesson> = [
{
title: t("columns.id"),
dataIndex: "id",
key: "id",
align: "center",
render: (text, record) => record?.id,
},
{
title: `${t("columns.name")}`,
dataIndex: "name",
key: "name",
align: "center",
render: (text, record) => record?.name,
},
{
title: canAddLesson ? (
<button
onClick={() => handel_open_model(ModalEnum?.LESSON_ADD)}
className="add_button"
>
{t("practical.add")} {t("models.lesson")} <FaPlus />
</button>
) : (
""
),
key: "actions",
align: "end",
className: "custom_add_button_column",
render: (text, record, index) => {
const className =
index % 2 === 0 ? "even-row buttonAction" : "odd-row buttonAction";
return (
<Space size="middle" className={className}>
{canEditLesson && (
<Tooltip
placement="top"
title={t("practical.edit")}
color="#E0E0E0"
>
<span onClick={() => handleEdit(record)}>
<MdOutlineEdit
size={22}
style={{ color: "#A098AE" }}
/>
</span>
</Tooltip>
)}
{canDeleteLesson && (
<RiDeleteBin6Fill
onClick={() => handelDelete(record)}
size={22}
style={{ color: "#C11313" }}
/>
)}
{canShowLesson && (
<BsEyeFill
onClick={() => handelShow(record)}
size={22}
style={{ color: "green" }}
/>
)}
</Space>
);
},
},
];
return columns;
};