feat: work shifts history

This commit is contained in:
2024-11-28 18:01:47 +04:00
parent f41083d2a8
commit 900427275f
14 changed files with 434 additions and 248 deletions

View File

@@ -0,0 +1,64 @@
import { useMemo } from "react";
import { MRT_ColumnDef, MRT_Row } from "mantine-react-table";
import { WorkShiftSchema } from "../../../../../client";
type Props = {
isActiveShiftsTable: boolean;
}
export const useShiftsTableColumns = ({ isActiveShiftsTable }: Props) => {
const getWorkedHoursString = (startedAtStr: string, finishedAtStr: string) => {
const finishedAt = new Date(finishedAtStr);
const startedAt = new Date(startedAtStr);
const diff: number = finishedAt.getTime() - startedAt.getTime();
const hours = Math.floor(diff / 3_600_000);
const minutes = Math.round(diff % 3_600_000 / 60_000);
if (hours === 0) {
return `${minutes} мин.`;
}
return `${hours} ч. ${minutes} мин.`;
};
const getColumnsForHistory = () => {
return isActiveShiftsTable ? [] : [
{
header: "Конец смены",
accessorKey: "finishedAt",
Cell: ({ row }: { row: MRT_Row<WorkShiftSchema> }) =>
row.original.finishedAt && new Date(row.original.finishedAt).toLocaleString("ru-RU"),
},
{
header: "Отработано",
Cell: ({ row }: { row: MRT_Row<WorkShiftSchema> }) =>
getWorkedHoursString(row.original.startedAt, row.original.finishedAt ?? ""),
},
];
};
return useMemo<MRT_ColumnDef<WorkShiftSchema>[]>(
() => [
{
header: "ФИО",
Cell: ({ row }: { row: MRT_Row<WorkShiftSchema> }) =>
`${row.original.user.firstName} ${row.original.user.secondName}`,
},
{
header: "Роль",
accessorKey: "user.role.name",
},
{
header: "Должность",
accessorKey: "user.position.name",
},
{
header: "Начало смены",
accessorKey: "startedAt",
Cell: ({ row }) =>
new Date(row.original.startedAt).toLocaleString("ru-RU"),
},
...getColumnsForHistory(),
],
[isActiveShiftsTable],
);
};