finalize hosting
This commit is contained in:
@@ -2,29 +2,6 @@ import { useState, useEffect } from 'react';
|
||||
import type { Hosting } from '../types';
|
||||
import { API_URL } from '../constants/constants';
|
||||
|
||||
// const mockData: Hosting = {
|
||||
// 1: {
|
||||
// reservedBy: "",
|
||||
// name: "Матрац 160см",
|
||||
// capacity: 2
|
||||
// },
|
||||
// 2: {
|
||||
// reservedBy: "",
|
||||
// name: "Кровать 120см",
|
||||
// capacity: 2
|
||||
// },
|
||||
// 3: {
|
||||
// reservedBy: "",
|
||||
// name: "Матрац 90см",
|
||||
// capacity: 1
|
||||
// },
|
||||
// 4: {
|
||||
// reservedBy: "",
|
||||
// name: "Диван",
|
||||
// capacity: 1
|
||||
// },
|
||||
// };
|
||||
|
||||
const useFetchHosting = () => {
|
||||
const [data, setData] = useState<Hosting|null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
@@ -93,12 +70,36 @@ const useFetchHosting = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const createHosting = async (token: string, name: string, capacity: number) => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/hosting/create`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ token, name, capacity })
|
||||
});
|
||||
|
||||
if (!response.ok) { // Check for non-200 responses
|
||||
const errorText = await response.text(); // Capture the response text for further insights
|
||||
throw new Error(`Error ${response.status}: ${errorText}`);
|
||||
}
|
||||
|
||||
// Optional: Fetch the updated data after reservation
|
||||
await fetchData();
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
fetchData(); // Initial fetch on mount
|
||||
}, []);
|
||||
|
||||
return { data, error, loading, refetch: fetchData, update: updateData, unreserveHosting};
|
||||
return { data, error, loading, refetch: fetchData, update: updateData, unreserveHosting, createHosting};
|
||||
};
|
||||
|
||||
export default useFetchHosting;
|
||||
|
||||
Reference in New Issue
Block a user