From 8037f4daf4efd4ad7380a79ddb12c08634050015 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Sat, 16 Nov 2024 23:20:47 +0200 Subject: [PATCH] add timeperiod filter to logs (default all) --- pages/cart/reports/coverMe.tsx | 199 +++++++++++++++++++-------------- 1 file changed, 113 insertions(+), 86 deletions(-) diff --git a/pages/cart/reports/coverMe.tsx b/pages/cart/reports/coverMe.tsx index 7d0eedf..9b06a68 100644 --- a/pages/cart/reports/coverMe.tsx +++ b/pages/cart/reports/coverMe.tsx @@ -1,16 +1,11 @@ -//page to show all repots in the database with a link to the report page -import axiosInstance from '../../../src/axiosSecure'; import { useEffect, useState } from "react"; -import toast from "react-hot-toast"; +import axiosInstance from "../../../src/axiosSecure"; import { useRouter } from "next/router"; import Link from "next/link"; -import { useSession } from "next-auth/react" -//const common = require('src/helpers/common'); -import common from '../../../src/helpers/common'; +import { useSession } from "next-auth/react"; import Layout from "../../../components/layout"; -import ProtectedRoute from '../../../components/protectedRoute'; -import { Location, Shift, UserRole, EventLog, EventType, EventLogType } from "@prisma/client"; -import { set } from 'date-fns'; +import ProtectedRoute from "../../../components/protectedRoute"; +import { UserRole, EventLogType } from "@prisma/client"; const timeFilters = [ { label: "1 седмица", value: 7 }, @@ -22,66 +17,105 @@ const timeFilters = [ export default function EventLogList() { const [eventLogs, setEventLog] = useState([]); const [requestedAssignments, setRequestedAssignments] = useState([]); - const router = useRouter(); - const { data: session } = useSession(); - const [locations, setLocations] = useState([]); const [showOpenRequests, setShowOpenRequests] = useState(false); + const [selectedTimeFilter, setSelectedTimeFilter] = useState(null); // Time filter state + const { data: session } = useSession(); useEffect(() => { const fetchLocations = async () => { try { - const { data: eventLogsDataold } = await axiosInstance.get(`/api/data/prisma/eventLog?where={"type":"${EventLogType.AssignmentReplacementAccepted}"}&include={"publisher":{"select":{"firstName":true,"lastName":true}},"shift":{"include":{"assignments":{"include":{"publisher":{"select":{"firstName":true,"lastName":true}}}}}}}`); - - // const where = encodeURIComponent(`{OR: [{type: "${EventLogType.AssignmentReplacementAccepted}"}, {type: "${EventLogType.AssignmentReplacementManual}"}]}`); const where = encodeURIComponent(JSON.stringify({ OR: [ { type: EventLogType.AssignmentReplacementAccepted }, - { type: EventLogType.AssignmentReplacementManual } - ] + { type: EventLogType.AssignmentReplacementManual }, + { type: EventLogType.AssignmentReplacementRequested } + ], })); - const { data: eventLogsData } = await axiosInstance.get(`/api/data/prisma/eventLog?where=${where}&include={"publisher":{"select":{"firstName":true,"lastName":true}},"shift":{"include":{"assignments":{"include":{"publisher":{"select":{"firstName":true,"lastName":true}}}}}}}`); + const { data: eventLogsData } = await axiosInstance.get( + `/api/data/prisma/eventLog?where=${where}&include={"publisher":{"select":{"firstName":true,"lastName":true}},"shift":{"include":{"assignments":{"include":{"publisher":{"select":{"firstName":true,"lastName":true}}}}}}}` + ); setEventLog(eventLogsData); - const { data: shiftsData } = await axiosInstance.get(`/api/data/prisma/assignment?where={"publicGuid":{"not":"null"}}&include={"shift":{"include":{"assignments":{"include":{"publisher":{"select":{"firstName":true,"lastName":true}}}}}},"publisher":{"select":{"firstName":true,"lastName":true}}}`); + const { data: shiftsData } = await axiosInstance.get( + `/api/data/prisma/assignment?where={"publicGuid":{"not":"null"}}&include={"shift":{"include":{"assignments":{"include":{"publisher":{"select":{"firstName":true,"lastName":true}}}}}},"publisher":{"select":{"firstName":true,"lastName":true}}}` + ); setRequestedAssignments(shiftsData); - } catch (error) { console.error(error); } }; + if (!locations.length) { fetchLocations(); } }, []); - + // Filter events based on the selected time range + const filteredEventLogs = eventLogs.filter((event) => { + if (!selectedTimeFilter) return true; + const eventDate = new Date(event.date); + const cutoffDate = new Date(); + cutoffDate.setDate(cutoffDate.getDate() - selectedTimeFilter); + return eventDate >= cutoffDate; + }); return ( -

Заявки за заместване

- {/* - - */} -
-
-
- - +
+ +
+
); } - -