add reserve unreserve endpoints
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useState } from 'react';
|
||||
import { useState } from 'react';
|
||||
import './App.css';
|
||||
import Greeting from './components/Greeting';
|
||||
import Hosting from './components/Hosting';
|
||||
|
||||
@@ -6,38 +6,52 @@ import CenteredContainer from "./ChildrenContainer";
|
||||
|
||||
interface ReserveButtonProps {
|
||||
update: (name: string, id: number) => void,
|
||||
unreserve: (token: string, id: number) => void,
|
||||
reservedBy: string,
|
||||
id: number,
|
||||
}
|
||||
|
||||
const ReserveButton: React.FC<ReserveButtonProps> = (props) => {
|
||||
const { reservedBy, update, id } = props;
|
||||
const { reservedBy, update, id, unreserve } = props;
|
||||
const [cookie] = useCookies<string>(['userName'])
|
||||
const [tokenCookie] = useCookies<string>(['apiToken'])
|
||||
const userName = cookie.userName;
|
||||
const isReserved = reservedBy !== '';
|
||||
const notify = useNotification();
|
||||
|
||||
const handleReserve = async () => {
|
||||
const handleReserve = async (name: string) => {
|
||||
try {
|
||||
await update(userName, id);
|
||||
notify(`Успешно забронировано для ${userName}`, 'success');
|
||||
await update(name, id);
|
||||
notify(`Успешно забронировано для ${name}`, 'success');
|
||||
} catch (error) {
|
||||
notify(`Не удалось забронировать: ${error instanceof Error ? error.message : 'Unknown error'}`, 'error');
|
||||
}
|
||||
};
|
||||
|
||||
const handleUnreserve = async () => {
|
||||
try {
|
||||
await unreserve(tokenCookie.apiToken, id);
|
||||
notify(`Удалось разбронировать ${name}`, 'success');
|
||||
} catch (error) {
|
||||
notify(`Не удалось разбронировать: ${error instanceof Error ? error.message : 'Unknown error'}`, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<button onClick={handleReserve} disabled={isReserved}>
|
||||
<button onClick={() => handleReserve(userName)} disabled={isReserved}>
|
||||
{isReserved ? `${reservedBy}` : 'Занять'}
|
||||
</button>
|
||||
{(reservedBy == userName) && (
|
||||
<button onClick={() => handleUnreserve()} disabled={false}>❌</button>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
function Hosting() {
|
||||
|
||||
const { data, error, loading, update } = useFetchHosting();
|
||||
const { data, error, loading, update, unreserveHosting } = useFetchHosting();
|
||||
return (
|
||||
<>
|
||||
<CenteredContainer>
|
||||
@@ -50,31 +64,33 @@ function Hosting() {
|
||||
(Лучше бронировать заранее если есть надобность. Оба в 1-1,5км от нашего дома).
|
||||
Спальные места:
|
||||
</p>
|
||||
{loading && <div>Loading...</div>}
|
||||
{error && <div>Error</div>}
|
||||
{data && (
|
||||
<div className="table-wrapper scroll-indicator">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Размещение</th>
|
||||
<th>Спальных мест</th>
|
||||
<th>Бронирование</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{Object.entries(data).map(([id, item]) => (
|
||||
<tr key={id}>
|
||||
<td>{item.name}</td>
|
||||
<td>{item.capacity}</td>
|
||||
<td>{<ReserveButton update={update} reservedBy={item.reservedBy} id={Number(id)} />}</td>
|
||||
{loading && <div>Loading...</div>}
|
||||
{error && <div>Error</div>}
|
||||
{data && (
|
||||
<div className="table-wrapper scroll-indicator">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Размещение</th>
|
||||
<th>Спальных мест</th>
|
||||
<th>Бронирование</th>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
<p className="scroll-instruction">Таблицу можно скроллить</p>
|
||||
</div>
|
||||
)}
|
||||
</thead>
|
||||
<tbody>
|
||||
{data.furniture && data.furniture.map((item) => (
|
||||
<tr key={item.id}>
|
||||
<td>{item.name}</td>
|
||||
<td>{item.capacity}</td>
|
||||
<td>
|
||||
<ReserveButton update={update} reservedBy={item.reservedBy} id={item.id} unreserve={unreserveHosting} />
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
<p className="scroll-instruction">Таблицу можно скроллить</p>
|
||||
</div>
|
||||
)}
|
||||
</CenteredContainer>
|
||||
<br />
|
||||
</>
|
||||
|
||||
9
frontend/src/types/index.d.ts
vendored
9
frontend/src/types/index.d.ts
vendored
@@ -1,11 +1,12 @@
|
||||
// src/types/index.d.ts
|
||||
|
||||
export interface Furniture {
|
||||
reservedBy: string;
|
||||
name: string;
|
||||
capacity: number;
|
||||
id: number; // Unique identifier for the furniture
|
||||
name: string; // Name of the furniture
|
||||
capacity: number; // Capacity of the furniture
|
||||
reservedBy: string; // Name of the person who reserved the furniture
|
||||
}
|
||||
|
||||
export interface Hosting {
|
||||
[key: number]: Furniture;
|
||||
furniture: Furniture[]; // Array of furniture items
|
||||
}
|
||||
|
||||
@@ -2,31 +2,31 @@ 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: "Vasya",
|
||||
name: "Кровать 120см",
|
||||
capacity: 2
|
||||
},
|
||||
3: {
|
||||
reservedBy: "",
|
||||
name: "Матрац 90см",
|
||||
capacity: 1
|
||||
},
|
||||
4: {
|
||||
reservedBy: "",
|
||||
name: "Диван",
|
||||
capacity: 1
|
||||
},
|
||||
};
|
||||
// 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(mockData);
|
||||
const [data, setData] = useState<Hosting|null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
@@ -69,12 +69,36 @@ const useFetchHosting = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const unreserveHosting = async (token: string, id: number) => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/hosting/${id}/unreserve`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ token })
|
||||
});
|
||||
|
||||
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
|
||||
fetchData(); // Initial fetch on mount
|
||||
}, []);
|
||||
|
||||
return { data, error, loading, refetch: fetchData, update: updateData };
|
||||
return { data, error, loading, refetch: fetchData, update: updateData, unreserveHosting};
|
||||
};
|
||||
|
||||
export default useFetchHosting;
|
||||
|
||||
Reference in New Issue
Block a user