initial commit - code moved to separate repo
This commit is contained in:
73
components/publisher/PublisherInlineForm.js
Normal file
73
components/publisher/PublisherInlineForm.js
Normal file
@ -0,0 +1,73 @@
|
||||
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 (
|
||||
<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"
|
||||
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;
|
Reference in New Issue
Block a user