time tracking

This commit is contained in:
2024-08-03 05:37:59 +03:00
parent deb7574380
commit 2ae47287a2
19 changed files with 483 additions and 16 deletions

View File

@@ -0,0 +1,91 @@
import {useMemo} from "react";
import {MRT_ColumnDef} from "mantine-react-table";
import {getDatesInMonth, getDayOfWeek} from "../../../../../shared/lib/date.ts";
import {Box, Flex, NumberInput} from "@mantine/core";
import {isNumber} from "lodash";
export type EmployeeData = {
name: string;
userId: number;
totalAmount: number;
[key: string]: number | string;
};
type Props = {
month: Date;
data: EmployeeData[];
onUpdate: (date: Date, userId: number, value: number) => void
}
const useWorkTableColumns = ({month, onUpdate, data}: Props) => {
const totalAmount = useMemo(() => data.reduce((acc, value) => acc + value.totalAmount, 0), [data]);
return useMemo<MRT_ColumnDef<EmployeeData>[]>(() => [
{
accessorKey: "name",
header: "ФИО"
},
...getDatesInMonth(month).map(date => ({
size: 80,
accessorKey: date.date().toString(),
header: date.date().toString(),
enableSorting: false,
enableColumnActions: false,
Header: (
<Flex
align={"center"}
direction={"column"}>
<Box>
{date.date()}
</Box>
<Box>
{getDayOfWeek(date.day())}
</Box>
</Flex>
),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
Cell: ({cell, row}) => {
return (
<Flex direction={"column"}>
<NumberInput
// key={row.original.name + date.date().toString()}
onChange={event => isNumber(event) && onUpdate(date.toDate(), row.original.userId, event)}
styles={{input: {textAlign: "center"}}}
hideControls
value={cell.renderValue()}
/>
</Flex>
);
}
})),
{
header: "Всего часов",
Cell: ({row}) => {
const v = Object.entries(row.original).reduce((acc, [key, value]) => {
if (isNaN(parseInt(key)) || !isNumber(value)) return acc;
console.log("dobro")
acc += value;
return acc;
}, 0);
console.log(`V: ${v}`)
return v;
},
},
{
accessorKey: "totalAmount",
header: "Итоговая сумма заработка",
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
Cell: ({cell}) => cell.renderValue().toLocaleString("ru-RU"),
Footer: (
<Flex>
Всего: {totalAmount.toLocaleString("ru-RU")}
</Flex>
)
},
], [month]);
}
export default useWorkTableColumns;