//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 { 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 Layout from "../../../components/layout"; import ProtectedRoute from '../../../components/protectedRoute'; import { Location, UserRole, ReportType } from "@prisma/client"; export default function Reports() { const [reports, setReports] = useState([]); const [filteredReports, setFilteredReports] = useState([]); const router = useRouter(); const { data: session } = useSession(); const [filterType, setFilterType] = useState('ServiceReport'); const handleFilterChange = (event) => { setFilterType(event.target.value); console.log("event filter set to:" + event.target.value); }; useEffect(() => { const isFeedbackType = type => [ 'Feedback_Problem', 'Feedback_Suggestion', 'Feedback' ].includes(type); setFilteredReports(reports.filter(report => filterType === 'Feedback' ? isFeedbackType(report.type) : report.type === filterType )); }, [reports, filterType]); const deleteReport = (id) => { axiosInstance .delete(`/api/data/reports/${id}`) .then((res) => { toast.success("Успешно изтрит отчет"); // router.push("/cart/reports/list"); setReports(reports.filter(report => report.id !== id)); }) .catch((err) => { console.log(err); }); }; const [locations, setLocations] = useState([]); useEffect(() => { const fetchLocations = async () => { try { console.log("fetching locations"); const { data } = await axiosInstance.get("/api/data/locations"); setLocations(data); console.log(data); axiosInstance.get(`/api/data/reports?include=publisher,location,shift`) .then((res) => { // let reports = res.data; // reports.forEach((report) => { // report.location = data.find((loc) => loc.id === report.locationId); // }); setReports(res.data); }) .catch((err) => { console.log(err); }); } catch (error) { console.error(error); } }; if (!locations.length) { fetchLocations(); } }, []); return (

Отчети

{filteredReports.map((report) => ( ))}
От Дата Място Отчет Действия
{report.publisher.firstName + " " + report.publisher.lastName} {common.getDateFormated(new Date(report.date))} {report.type === ReportType.ServiceReport ? (report.shift ? " от " + common.getTimeFormatted(report.shift?.startTime) + " ч." : "") : common.getTimeFormatted(report.date)} {report.location?.name} {report.type === ReportType.ServiceReport ? (report.shift ? "" : "за целия ден") : report.comments} {(report.type === ReportType.ServiceReport) ? ( <>
Отчет
Издания: {report.placementCount}
Разговори: {report.conversationCount}
Клипове: {report.videoCount}
Адреси / Телефони: {report.returnVisitInfoCount}
) : (report.type === ReportType.Feedback || report.type === ReportType.Feedback_Problem || report.type === ReportType.Feedback_Suggestion) ? ( <>
Отзив {report.type === "Feedback_Problem" ? - Проблем : report.type === "Feedback_Suggestion" ? - Предложение : ""}
) : ( <>
Случка
) }
); }