finalize hosting
This commit is contained in:
@@ -2,6 +2,7 @@ import useFetchHosting from "../utils/fetchHosting";
|
||||
import { useNotification } from "../NotificationContext";
|
||||
import { useCookies } from "react-cookie";
|
||||
import CenteredContainer from "./ChildrenContainer";
|
||||
import { useState } from "react";
|
||||
|
||||
|
||||
interface ReserveButtonProps {
|
||||
@@ -49,9 +50,68 @@ const ReserveButton: React.FC<ReserveButtonProps> = (props) => {
|
||||
);
|
||||
};
|
||||
|
||||
function Hosting() {
|
||||
interface CreateHostingFormProps {
|
||||
createHosting: (token: string, name: string, capacity: number) => void,
|
||||
}
|
||||
|
||||
const { data, error, loading, update, unreserveHosting } = useFetchHosting();
|
||||
const CreateHostingForm: React.FC<CreateHostingFormProps> = (props) => {
|
||||
const { createHosting } = props;
|
||||
const [name, setName] = useState('');
|
||||
const [capacity, setCapacity] = useState('');
|
||||
const [tokenCookie] = useCookies<string>(['apiToken'])
|
||||
const notify = useNotification();
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!name || !capacity) {
|
||||
notify('Надо заполнить все поля', 'error')
|
||||
return
|
||||
}
|
||||
try {
|
||||
await createHosting(tokenCookie.apiToken, name, Number(capacity)); // Call the existing hook
|
||||
|
||||
// Reset form fields
|
||||
setName('');
|
||||
setCapacity('');
|
||||
notify('Удалось создать!', 'success')
|
||||
} catch (error) {
|
||||
notify(`Не удалось создать: ${error instanceof Error ? error.message : 'Unknown error'}`, 'error'); // Capture any error message
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div>
|
||||
<label>
|
||||
Размещение:
|
||||
<input
|
||||
type="text"
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<div>
|
||||
<label>
|
||||
Спальных мест:
|
||||
<input
|
||||
type="number"
|
||||
value={capacity}
|
||||
onChange={(e) => setCapacity(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<button type="submit" onClick={() => handleSubmit()}>
|
||||
{'Создать размещение'}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
const Hosting = () => {
|
||||
const { data, error, loading, update, unreserveHosting, createHosting } = useFetchHosting();
|
||||
return (
|
||||
<>
|
||||
<CenteredContainer>
|
||||
@@ -91,10 +151,12 @@ function Hosting() {
|
||||
<p className="scroll-instruction">Таблицу можно скроллить</p>
|
||||
</div>
|
||||
)}
|
||||
Если вы хотите организовать себе свои спальные места и хотите, чтобы остальные это видели, вы можете добавить свое месо в таблицу.
|
||||
<CreateHostingForm createHosting={createHosting}/>
|
||||
</CenteredContainer>
|
||||
<br />
|
||||
</>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default Hosting;
|
||||
Reference in New Issue
Block a user