Files
mwitnessing/components/publisher/PublisherInlineForm.js
2024-05-23 02:52:03 +03:00

74 lines
2.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 || 2);
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 (
<div className="flex flex-col sm:flex-row items-center space-y-2 sm:space-y-0 sm:space-x-2">
<label htmlFor="desiredShiftsPerMonth" className="block text-sm font-medium text-gray-700">
Желани смени на месец:
</label>
<input
type="number"
id="desiredShiftsPerMonth"
name="desiredShiftsPerMonth"
min="0" max="8"
value={desiredShiftsPerMonth}
onChange={(e) => setDesiredShiftsPerMonth(parseInt(e.target.value))}
className="textbox mt-1 sm:mt-0 w-full sm:w-auto flex-grow"
placeholder="Желани смени на месец"
min="0" max="10"
/>
</div>
);
};
export default PublisherInlineForm;