Gérer les retours de livres par l'admin
This commit is contained in:
@@ -10,6 +10,7 @@ import BookDetail from './pages/BookDetail';
|
||||
import Customers from './pages/Customers';
|
||||
import Login from './pages/Login';
|
||||
import Reservations from './pages/Reservations';
|
||||
import Returns from './pages/Returns';
|
||||
import { useAuth } from './context/AuthContext';
|
||||
|
||||
function RequireAuth({ children }) {
|
||||
@@ -37,6 +38,7 @@ export default function App() {
|
||||
<Route path="reservations" element={<RequireAuth><Reservations /></RequireAuth>} />
|
||||
<Route path="profile" element={<RequireAuth><Profile /></RequireAuth>} />
|
||||
<Route path="customers" element={<RequireAdmin><Customers /></RequireAdmin>} />
|
||||
<Route path="returns" element={<RequireAdmin><Returns /></RequireAdmin>} />
|
||||
<Route path="*" element={<NotFound />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
|
||||
@@ -22,6 +22,7 @@ export default function Navbar() {
|
||||
{user?.role === 'user' && <li><Link to="/reservations">Mes réservations</Link></li>}
|
||||
{user && <li><Link to="/profile">Mon compte</Link></li>}
|
||||
{user?.role === 'admin' && <li><Link to="/customers">Clients</Link></li>}
|
||||
{user?.role === 'admin' && <li><Link to="/returns">Retours</Link></li>}
|
||||
</ul>
|
||||
|
||||
<div className="navbar__actions">
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { createContext, useContext, useState } from 'react';
|
||||
|
||||
const ReturnContext = createContext(null);
|
||||
|
||||
export function ReturnProvider({ children }) {
|
||||
const [returns, setReturns] = useState(() => {
|
||||
const saved = localStorage.getItem('returns');
|
||||
return saved ? JSON.parse(saved) : [];
|
||||
});
|
||||
|
||||
function addReturn(bookId, bookTitle, customerPhone, reason) {
|
||||
const bookReturn = {
|
||||
returnId: crypto.randomUUID(),
|
||||
bookId,
|
||||
bookTitle,
|
||||
customerPhone,
|
||||
reason,
|
||||
status: 'PROCESSED',
|
||||
returnedAt: new Date().toISOString(),
|
||||
};
|
||||
const updated = [...returns, bookReturn];
|
||||
setReturns(updated);
|
||||
localStorage.setItem('returns', JSON.stringify(updated));
|
||||
return bookReturn;
|
||||
}
|
||||
|
||||
return (
|
||||
<ReturnContext.Provider value={{ returns, addReturn }}>
|
||||
{children}
|
||||
</ReturnContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useReturns() {
|
||||
return useContext(ReturnContext);
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { BrowserRouter } from 'react-router-dom';
|
||||
import { AuthProvider } from './context/AuthContext';
|
||||
import { ReservationProvider } from './context/ReservationContext';
|
||||
import { ReviewProvider } from './context/ReviewContext';
|
||||
import { ReturnProvider } from './context/ReturnContext';
|
||||
import App from './App';
|
||||
import './styles/global.css';
|
||||
|
||||
@@ -14,7 +15,9 @@ root.render(
|
||||
<AuthProvider>
|
||||
<ReservationProvider>
|
||||
<ReviewProvider>
|
||||
<App />
|
||||
<ReturnProvider>
|
||||
<App />
|
||||
</ReturnProvider>
|
||||
</ReviewProvider>
|
||||
</ReservationProvider>
|
||||
</AuthProvider>
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
import { useState } from 'react';
|
||||
import { useReturns } from '../context/ReturnContext';
|
||||
import { getBooks } from '../api/books';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
export default function Returns() {
|
||||
const { returns, addReturn } = useReturns();
|
||||
const [books, setBooks] = useState([]);
|
||||
const [form, setForm] = useState({ bookId: '', customerPhone: '', reason: '' });
|
||||
const [message, setMessage] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
getBooks(0, 100).then(res => setBooks(res.data.content)).catch(console.error);
|
||||
}, []);
|
||||
|
||||
function handleChange(e) {
|
||||
const { name, value } = e.target;
|
||||
setForm(f => ({ ...f, [name]: value }));
|
||||
}
|
||||
|
||||
function handleSubmit(e) {
|
||||
e.preventDefault();
|
||||
const book = books.find(b => String(b.isbn) === String(form.bookId));
|
||||
const bookTitle = book ? book.title : form.bookId;
|
||||
addReturn(form.bookId, bookTitle, form.customerPhone, form.reason);
|
||||
setMessage({ success: true, text: 'Retour enregistré avec succès !' });
|
||||
setForm({ bookId: '', customerPhone: '', reason: '' });
|
||||
}
|
||||
|
||||
return (
|
||||
<main>
|
||||
<h1>Gestion des retours</h1>
|
||||
|
||||
<section>
|
||||
<h2>Enregistrer un retour</h2>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<label>
|
||||
Livre :
|
||||
<select name="bookId" value={form.bookId} onChange={handleChange} required>
|
||||
<option value="">-- Choisir un livre --</option>
|
||||
{books.map(b => (
|
||||
<option key={b.isbn} value={b.isbn}>{b.title} — {b.author}</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
Téléphone du client :
|
||||
<input
|
||||
name="customerPhone"
|
||||
type="tel"
|
||||
value={form.customerPhone}
|
||||
onChange={handleChange}
|
||||
placeholder="0612345678"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Motif :
|
||||
<textarea
|
||||
name="reason"
|
||||
value={form.reason}
|
||||
onChange={handleChange}
|
||||
placeholder="Motif du retour..."
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<button type="submit">Enregistrer le retour</button>
|
||||
</form>
|
||||
{message && <p style={{ color: message.success ? 'green' : 'red' }}>{message.text}</p>}
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h2>Historique des retours</h2>
|
||||
{returns.length === 0 ? (
|
||||
<p>Aucun retour enregistré.</p>
|
||||
) : (
|
||||
<ul>
|
||||
{returns.map(r => (
|
||||
<li key={r.returnId}>
|
||||
<strong>{r.bookTitle}</strong>
|
||||
<br />
|
||||
Client : {r.customerPhone}
|
||||
<br />
|
||||
Motif : {r.reason}
|
||||
<br />
|
||||
Statut : {r.status}
|
||||
<br />
|
||||
<small>{new Date(r.returnedAt).toLocaleDateString('fr-FR')}</small>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user