import React, { useState, useEffect, useRef } from "react"; import axiosInstance from '../../src/axiosSecure'; import toast from "react-hot-toast"; import { useRouter } from "next/router"; const PublisherInlineForm = ({ publisherId, initialShiftsPerMonth }) => { const [desiredShiftsPerMonth, setDesiredShiftsPerMonth] = useState(initialShiftsPerMonth); const router = useRouter(); const storedValue = useRef(initialShiftsPerMonth); useEffect(() => { const fetchPublisherData = async () => { if (publisherId != null) { try { const response = await axiosInstance.get(`/api/data/publishers/${publisherId}`); const publisher = response.data; setDesiredShiftsPerMonth(publisher.desiredShiftsPerMonth); storedValue.current = publisher.desiredShiftsPerMonth; } catch (error) { console.error("Error fetching publisher data:", error); toast.error("Не може да се зареди информация."); } } }; //if (storedValue.current == null) { fetchPublisherData(); //} }, [publisherId]); useEffect(() => { const saveShiftsPerMonth = async () => { if (publisherId && desiredShiftsPerMonth != null && initialShiftsPerMonth != desiredShiftsPerMonth && storedValue.current != desiredShiftsPerMonth) { try { await axiosInstance.put(`/api/data/publishers/${publisherId}`, { desiredShiftsPerMonth, }); toast.success("Смени на месец запазени", { position: "bottom-center", }); } catch (error) { console.error("Error saving desired shifts per month:", error); toast.error("Грешка при запазване на смени на месец"); } } }; saveShiftsPerMonth(); }, [desiredShiftsPerMonth]); return (