time tracking
This commit is contained in:
@@ -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;
|
||||
@@ -0,0 +1,29 @@
|
||||
import {useEffect, useState} from "react";
|
||||
import {TimeTrackingRecord, TimeTrackingService} from "../../../../../client";
|
||||
import {dateWithoutTimezone} from "../../../../../shared/lib/date.ts";
|
||||
|
||||
const useWorkTableState = () => {
|
||||
const [month, setMonth] = useState<Date>(new Date(new Date().getFullYear(), new Date().getMonth(), 1));
|
||||
const [trackingRecords, setTrackingRecords] = useState<TimeTrackingRecord[]>([]);
|
||||
const refetch = async () => {
|
||||
return TimeTrackingService.getTimeTrackingRecords({
|
||||
requestBody: {
|
||||
date: dateWithoutTimezone(month),
|
||||
userIds: []
|
||||
}
|
||||
}).then((response) => setTrackingRecords(response.records));
|
||||
}
|
||||
useEffect(() => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
refetch().then(_ => {
|
||||
});
|
||||
}, [month])
|
||||
return {
|
||||
month,
|
||||
setMonth,
|
||||
refetch,
|
||||
trackingRecords
|
||||
}
|
||||
}
|
||||
|
||||
export default useWorkTableState;
|
||||
Reference in New Issue
Block a user