diff --git a/components/absences/absencesMonthlyModal.tsx b/components/absences/absencesMonthlyModal.tsx index e3a7630..dc0b63a 100644 --- a/components/absences/absencesMonthlyModal.tsx +++ b/components/absences/absencesMonthlyModal.tsx @@ -1,10 +1,7 @@ -import React from "react"; -import Modal from 'react-bootstrap/Modal'; -import Button from 'react-bootstrap/Button'; -import { getServerSession } from "next-auth/next"; -import { NextApiRequest, NextApiResponse } from "next"; -import { authOptions } from '../../pages/api/auth/[...nextauth]'; -import { getSession } from 'next-auth/react'; +import React, { useEffect, useState } from "react"; +import Modal from "react-bootstrap/Modal"; +import Button from "react-bootstrap/Button"; +import { getSession } from "next-auth/react"; import DateUtility from "../../utils/dateUtility"; import { AbsenceInMonth } from "../../types/absenceInMonth"; import { CustomSession } from "../../types/customSession"; @@ -14,87 +11,88 @@ interface AbsencesMonthlyModalProps { close: () => void; parameters: string; } -interface AbsencesMonthlyModalState { - data: AbsenceInMonth | null; - show: boolean; - title: string -} -class AbsencesMonthlyModal extends React.Component { - constructor(props:any) { - super(props); - this.state = { data: null, show:false, title: ""}; - } - async componentDidUpdate(propsPrecedenti:any) { - if (this.props.tmpshow !== propsPrecedenti.tmpshow) { - if (this.props.tmpshow){ - const parameters = this.props.parameters - const session = await getSession() as CustomSession; - let accessToken = null; - if (session) { - accessToken = session.accessToken; - const url = '/api/rest/v4?endpoint=absences%2FabsenceInMonth?'+parameters; +const AbsencesMonthlyModal: React.FC = ({ tmpshow, close, parameters }) => { + const [data, setData] = useState(null); + const [show, setShow] = useState(false); + const [title, setTitle] = useState(""); - fetch(url, { - method: 'GET', + useEffect(() => { + const fetchData = async () => { + if (tmpshow) { + const session = (await getSession()) as CustomSession | null; + if (session?.accessToken) { + const accessToken = session.accessToken; + const url = `/api/rest/v4?endpoint=absences%2FabsenceInMonth&${parameters}`; + try { + const response = await fetch(url, { + method: "GET", headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - Authorization: 'Bearer '+accessToken - } - }).then(response => response.json()) - .catch(error => console.error("unable to achive this", error)) - .then(data => { - this.setState({'data':data, 'show':true, 'title':"Date in cui è stata effettuata l'assenza "+data.code}) + Accept: "application/json", + "Content-Type": "application/json", + Authorization: `Bearer ${accessToken}`, + }, }); + + if (response.ok) { + const data = await response.json(); + setData(data); + setShow(true); + setTitle(`Date in cui è stata effettuata l'assenza ${data.code}`); + } else { + console.error("Errore durante il recupero dei dati:", response.statusText); + setData(null); + setShow(false); + setTitle(""); + } + } catch (error) { + console.error("Errore durante il fetch:", error); + setData(null); + setShow(false); + setTitle(""); + } + } else { + setData(null); + setShow(false); + setTitle(""); } - else { - this.setState({'data':null,'show':false, 'title':""}) - } + } else { + setData(null); + setShow(false); + setTitle(""); } - else { - this.setState({'data':null,'show':false, 'title':""}) - } - } - } - - handleClose = () => { - this.setState ({'show': false}) - } + }; - render() { + fetchData(); + }, [tmpshow, parameters]); - let dateList = this.state.data ? this.state.data.dateAbsences?.map((date) =>
  • {DateUtility.formatDate(date)}
  • ) : "" + const handleClose = () => { + setShow(false); + close(); + }; - return ( - - - {this.state.title} - - - {this.state.show ? dateList : ''} - - - - - - ); - } + const dateList = data + ? data.dateAbsences?.map((date) => ( +
  • {DateUtility.formatDate(date)}
  • + )) + : null; -} + return ( + + + {title} + + {show ?
      {dateList}
    : "Nessun dato disponibile."}
    + + + +
    + ); +}; -export async function getServerSideProps({ req, res }: { req: NextApiRequest, res: NextApiResponse }) { - return { - props: { - session: await getServerSession(req, res, authOptions), - }, - }; -} -export default AbsencesMonthlyModal; \ No newline at end of file +export default AbsencesMonthlyModal; diff --git a/components/absences/absencesMonthlyTable.tsx b/components/absences/absencesMonthlyTable.tsx index 1605e65..b2a1d56 100644 --- a/components/absences/absencesMonthlyTable.tsx +++ b/components/absences/absencesMonthlyTable.tsx @@ -40,7 +40,7 @@ const AbsencesMonthlyTable: React.FC = ({ {absence.description} {absence.code} - setModalParam(absence.code, month, year)}> + setModalParam(absence.code, month, year)}> {absence.numberOfDays}