feat: work shifts history
This commit is contained in:
		
							
								
								
									
										64
									
								
								src/pages/AdminPage/tabs/WorkShifts/hooks/columns.tsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/pages/AdminPage/tabs/WorkShifts/hooks/columns.tsx
									
									
									
									
									
										Normal 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],
 | 
			
		||||
    );
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user