hosting: fix notification on reserve

This commit is contained in:
tylen
2025-10-29 16:09:41 +02:00
parent 13ae6c6942
commit 8771e859fd
4 changed files with 105 additions and 15 deletions

View File

@@ -1,5 +1,6 @@
import { useState } from "react";
import useFetchHosting from "../utils/fetchHosting";
import { useNotification } from "../NotificationContext";
interface ReserveButtonProps {
@@ -10,12 +11,21 @@ interface ReserveButtonProps {
const ReserveButton: React.FC<ReserveButtonProps> = (props) => {
const { reservedBy, update, id } = props;
const [name, setName] = useState(reservedBy);
const [name, setName] = useState(reservedBy || ''); // Default to empty if not reserved
const isReserved = reservedBy !== '';
const notify = useNotification();
const handleReserve = async () => {
if (name.trim()) {
await update(name, id); // Call the update function from props with the name and id
if (!name.trim()) {
notify('Поле имени не может быть пустым', 'error');
return;
}
try {
await update(name, id); // Await the update call
notify(`Успешно забронировано для ${name}`, 'success'); // Move success notification here
} catch (error) {
notify(`Не удалось забронировать: ${error instanceof Error ? error.message : 'Unknown error'}`, 'error');
}
};
@@ -26,7 +36,7 @@ const ReserveButton: React.FC<ReserveButtonProps> = (props) => {
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Введите ваше имя"
disabled={isReserved} // Disable if already reserved
disabled={isReserved} // Disable input if already reserved
/>
<button onClick={handleReserve} disabled={isReserved}>
{isReserved ? 'Занято' : 'Занять'}