Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 24849c6414 | |||
| d7dfec49fd | |||
| 0d9f8dc539 |
@@ -11,6 +11,7 @@ import Customers from './pages/Customers';
|
|||||||
import Login from './pages/Login';
|
import Login from './pages/Login';
|
||||||
import Reservations from './pages/Reservations';
|
import Reservations from './pages/Reservations';
|
||||||
import Returns from './pages/Returns';
|
import Returns from './pages/Returns';
|
||||||
|
import Subscription from './pages/Subscription';
|
||||||
import { useAuth } from './context/AuthContext';
|
import { useAuth } from './context/AuthContext';
|
||||||
|
|
||||||
function RequireAuth({ children }) {
|
function RequireAuth({ children }) {
|
||||||
@@ -39,6 +40,7 @@ export default function App() {
|
|||||||
<Route path="profile" element={<RequireAuth><Profile /></RequireAuth>} />
|
<Route path="profile" element={<RequireAuth><Profile /></RequireAuth>} />
|
||||||
<Route path="customers" element={<RequireAdmin><Customers /></RequireAdmin>} />
|
<Route path="customers" element={<RequireAdmin><Customers /></RequireAdmin>} />
|
||||||
<Route path="returns" element={<RequireAdmin><Returns /></RequireAdmin>} />
|
<Route path="returns" element={<RequireAdmin><Returns /></RequireAdmin>} />
|
||||||
|
<Route path="subscription" element={<RequireAuth><Subscription /></RequireAuth>} />
|
||||||
<Route path="*" element={<NotFound />} />
|
<Route path="*" element={<NotFound />} />
|
||||||
</Route>
|
</Route>
|
||||||
</Routes>
|
</Routes>
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ export default function Navbar() {
|
|||||||
<li><Link to="/books">Catalogue</Link></li>
|
<li><Link to="/books">Catalogue</Link></li>
|
||||||
{user && <li><Link to="/orders">Commandes</Link></li>}
|
{user && <li><Link to="/orders">Commandes</Link></li>}
|
||||||
{user?.role === 'user' && <li><Link to="/reservations">Mes réservations</Link></li>}
|
{user?.role === 'user' && <li><Link to="/reservations">Mes réservations</Link></li>}
|
||||||
|
{user?.role === 'user' && <li><Link to="/subscription">Mon abonnement</Link></li>}
|
||||||
{user && <li><Link to="/profile">Mon compte</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="/customers">Clients</Link></li>}
|
||||||
{user?.role === 'admin' && <li><Link to="/returns">Retours</Link></li>}
|
{user?.role === 'admin' && <li><Link to="/returns">Retours</Link></li>}
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import { createContext, useContext, useState } from 'react';
|
||||||
|
|
||||||
|
const SubscriptionContext = createContext(null);
|
||||||
|
|
||||||
|
const PLANS = [
|
||||||
|
{ id: 'basic', name: 'Basic', price: 9.99, description: '2 livres par mois' },
|
||||||
|
{ id: 'standard', name: 'Standard', price: 14.99, description: '5 livres par mois' },
|
||||||
|
{ id: 'premium', name: 'Premium', price: 24.99, description: 'Livres illimités par mois' },
|
||||||
|
];
|
||||||
|
|
||||||
|
export { PLANS };
|
||||||
|
|
||||||
|
export function SubscriptionProvider({ children }) {
|
||||||
|
const [subscription, setSubscription] = useState(() => {
|
||||||
|
const saved = localStorage.getItem('subscription');
|
||||||
|
return saved ? JSON.parse(saved) : null;
|
||||||
|
});
|
||||||
|
|
||||||
|
function subscribe(planId, phoneNumber) {
|
||||||
|
const plan = PLANS.find(p => p.id === planId);
|
||||||
|
const newSubscription = {
|
||||||
|
subscriptionId: crypto.randomUUID(),
|
||||||
|
planId,
|
||||||
|
planName: plan.name,
|
||||||
|
price: plan.price,
|
||||||
|
phoneNumber,
|
||||||
|
status: 'ACTIVE',
|
||||||
|
startDate: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
setSubscription(newSubscription);
|
||||||
|
localStorage.setItem('subscription', JSON.stringify(newSubscription));
|
||||||
|
return newSubscription;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cancelSubscription() {
|
||||||
|
const updated = { ...subscription, status: 'CANCELLED' };
|
||||||
|
setSubscription(updated);
|
||||||
|
localStorage.setItem('subscription', JSON.stringify(updated));
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SubscriptionContext.Provider value={{ subscription, subscribe, cancelSubscription, PLANS }}>
|
||||||
|
{children}
|
||||||
|
</SubscriptionContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useSubscription() {
|
||||||
|
return useContext(SubscriptionContext);
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import { AuthProvider } from './context/AuthContext';
|
|||||||
import { ReservationProvider } from './context/ReservationContext';
|
import { ReservationProvider } from './context/ReservationContext';
|
||||||
import { ReviewProvider } from './context/ReviewContext';
|
import { ReviewProvider } from './context/ReviewContext';
|
||||||
import { ReturnProvider } from './context/ReturnContext';
|
import { ReturnProvider } from './context/ReturnContext';
|
||||||
|
import { SubscriptionProvider } from './context/SubscriptionContext';
|
||||||
import App from './App';
|
import App from './App';
|
||||||
import './styles/global.css';
|
import './styles/global.css';
|
||||||
|
|
||||||
@@ -16,7 +17,9 @@ root.render(
|
|||||||
<ReservationProvider>
|
<ReservationProvider>
|
||||||
<ReviewProvider>
|
<ReviewProvider>
|
||||||
<ReturnProvider>
|
<ReturnProvider>
|
||||||
<App />
|
<SubscriptionProvider>
|
||||||
|
<App />
|
||||||
|
</SubscriptionProvider>
|
||||||
</ReturnProvider>
|
</ReturnProvider>
|
||||||
</ReviewProvider>
|
</ReviewProvider>
|
||||||
</ReservationProvider>
|
</ReservationProvider>
|
||||||
|
|||||||
@@ -4,10 +4,12 @@ import { getBookById } from '../api/books';
|
|||||||
import { useAuth } from '../context/AuthContext';
|
import { useAuth } from '../context/AuthContext';
|
||||||
import { useReservations } from '../context/ReservationContext';
|
import { useReservations } from '../context/ReservationContext';
|
||||||
import { useReviews } from '../context/ReviewContext';
|
import { useReviews } from '../context/ReviewContext';
|
||||||
|
import { useSubscription } from '../context/SubscriptionContext';
|
||||||
|
|
||||||
export default function BookDetail() {
|
export default function BookDetail() {
|
||||||
const { user } = useAuth();
|
const { user } = useAuth();
|
||||||
const { addReservation } = useReservations();
|
const { subscription } = useSubscription();
|
||||||
|
const { addReservation, reservations } = useReservations();
|
||||||
const { addReview, getReviewsByBook } = useReviews();
|
const { addReview, getReviewsByBook } = useReviews();
|
||||||
const [rating, setRating] = useState(5);
|
const [rating, setRating] = useState(5);
|
||||||
const [comment, setComment] = useState('');
|
const [comment, setComment] = useState('');
|
||||||
@@ -97,27 +99,60 @@ export default function BookDetail() {
|
|||||||
)}
|
)}
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
{user?.role === 'user' && <section>
|
{user?.role === 'user' && (
|
||||||
<h2>Réserver ce livre</h2>
|
<section>
|
||||||
<form onSubmit={handleReservation}>
|
<h2>Réserver ce livre</h2>
|
||||||
<label>
|
{!subscription || subscription.status !== 'ACTIVE' ? (
|
||||||
Numéro de téléphone :
|
<p>
|
||||||
<input
|
Vous devez avoir un abonnement actif pour réserver un livre.{' '}
|
||||||
type="tel"
|
<Link to="/subscription">S'abonner</Link>
|
||||||
value={phoneNumber}
|
</p>
|
||||||
onChange={(e) => setPhoneNumber(e.target.value)}
|
) : (() => {
|
||||||
placeholder="0612345678"
|
const QUOTAS = { basic: 2, standard: 5, premium: Infinity };
|
||||||
required
|
const quota = QUOTAS[subscription.planId];
|
||||||
/>
|
const thisMonthStart = new Date();
|
||||||
</label>
|
thisMonthStart.setDate(1);
|
||||||
<button type="submit">Réserver</button>
|
thisMonthStart.setHours(0, 0, 0, 0);
|
||||||
</form>
|
const usedThisMonth = reservations.filter(r =>
|
||||||
{reservationStatus && (
|
new Date(r.reservedAt) >= thisMonthStart
|
||||||
<p style={{ color: reservationStatus.success ? 'green' : 'red' }}>
|
).length;
|
||||||
{reservationStatus.message}
|
const remaining = quota - usedThisMonth;
|
||||||
</p>
|
|
||||||
)}
|
if (remaining <= 0) {
|
||||||
</section>}
|
return (
|
||||||
|
<p style={{ color: 'orange' }}>
|
||||||
|
Vous avez atteint votre quota de {quota} réservation(s) ce mois-ci.
|
||||||
|
<Link to="/subscription"> Passer au plan supérieur</Link>
|
||||||
|
</p>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<p>Réservations restantes ce mois : <strong>{remaining === Infinity ? 'illimitées' : remaining}</strong></p>
|
||||||
|
<form onSubmit={handleReservation}>
|
||||||
|
<label>
|
||||||
|
Numéro de téléphone :
|
||||||
|
<input
|
||||||
|
type="tel"
|
||||||
|
value={phoneNumber}
|
||||||
|
onChange={(e) => setPhoneNumber(e.target.value)}
|
||||||
|
placeholder="0612345678"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<button type="submit">Réserver</button>
|
||||||
|
</form>
|
||||||
|
{reservationStatus && (
|
||||||
|
<p style={{ color: reservationStatus.success ? 'green' : 'red' }}>
|
||||||
|
{reservationStatus.message}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
})()}
|
||||||
|
</section>
|
||||||
|
)}
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { createOrder } from '../api/orders';
|
import { createOrder } from '../api/orders';
|
||||||
|
import { getBookById } from '../api/books';
|
||||||
|
|
||||||
export default function Orders() {
|
export default function Orders() {
|
||||||
const [customerId, setCustomerId] = useState('');
|
const [customerId, setCustomerId] = useState('');
|
||||||
const [paymentMethod, setPaymentMethod] = useState('CREDIT_CARD');
|
const [paymentMethod, setPaymentMethod] = useState('CREDIT_CARD');
|
||||||
const [address, setAddress] = useState({ street: '', city: '', postalCode: '', country: '' });
|
const [address, setAddress] = useState({ street: '', city: '', postalCode: '', country: '' });
|
||||||
const [lines, setLines] = useState([{ bookId: '', quantity: 1 }]);
|
const [lines, setLines] = useState([{ bookId: '', quantity: 1 }]);
|
||||||
|
const [promoCode, setPromoCode] = useState('');
|
||||||
const [message, setMessage] = useState(null);
|
const [message, setMessage] = useState(null);
|
||||||
const [submitting, setSubmitting] = useState(false);
|
const [submitting, setSubmitting] = useState(false);
|
||||||
|
|
||||||
@@ -21,6 +23,10 @@ export default function Orders() {
|
|||||||
setLines((prev) => [...prev, { bookId: '', quantity: 1 }]);
|
setLines((prev) => [...prev, { bookId: '', quantity: 1 }]);
|
||||||
}
|
}
|
||||||
function removeLine(index) {
|
function removeLine(index) {
|
||||||
|
const [promoValid, setPromoValid] = useState(true);
|
||||||
|
const [estimate, setEstimate] = useState(null);
|
||||||
|
const [estimating, setEstimating] = useState(false);
|
||||||
|
const [estimateError, setEstimateError] = useState(null);
|
||||||
setLines((prev) => prev.filter((_, i) => i !== index));
|
setLines((prev) => prev.filter((_, i) => i !== index));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,6 +43,7 @@ export default function Orders() {
|
|||||||
bookId: Number(line.bookId),
|
bookId: Number(line.bookId),
|
||||||
quantity: Number(line.quantity),
|
quantity: Number(line.quantity),
|
||||||
})),
|
})),
|
||||||
|
...(promoCode ? { promoCode } : {}),
|
||||||
};
|
};
|
||||||
|
|
||||||
createOrder(payload)
|
createOrder(payload)
|
||||||
@@ -63,6 +70,51 @@ export default function Orders() {
|
|||||||
onChange={(e) => handleLineChange(index, 'bookId', e.target.value)} required />
|
onChange={(e) => handleLineChange(index, 'bookId', e.target.value)} required />
|
||||||
<input type="number" placeholder="Quantité" value={line.quantity}
|
<input type="number" placeholder="Quantité" value={line.quantity}
|
||||||
onChange={(e) => handleLineChange(index, 'quantity', e.target.value)} required />
|
onChange={(e) => handleLineChange(index, 'quantity', e.target.value)} required />
|
||||||
|
function validatePromo(code) {
|
||||||
|
if (!code) return true;
|
||||||
|
// simple format: PROMO10 => 10% , PROMO5 => 5%
|
||||||
|
return /^PROMO\d{1,2}$/.test(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleEstimate() {
|
||||||
|
setEstimating(true);
|
||||||
|
setEstimate(null);
|
||||||
|
setEstimateError(null);
|
||||||
|
try {
|
||||||
|
const ids = lines.map((l) => Number(l.bookId)).filter((n) => !Number.isNaN(n) && n > 0);
|
||||||
|
if (ids.length === 0) {
|
||||||
|
setEstimateError('Aucun ISBN valide');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const unique = Array.from(new Set(ids));
|
||||||
|
const responses = await Promise.all(unique.map((id) => getBookById(id).then((r) => r.data)));
|
||||||
|
const priceMap = new Map();
|
||||||
|
unique.forEach((id, idx) => priceMap.set(id, responses[idx].prix ?? responses[idx].price ?? 0));
|
||||||
|
|
||||||
|
let total = 0;
|
||||||
|
for (const l of lines) {
|
||||||
|
const id = Number(l.bookId);
|
||||||
|
const qty = Number(l.quantity) || 0;
|
||||||
|
total += (priceMap.get(id) || 0) * qty;
|
||||||
|
}
|
||||||
|
|
||||||
|
let discounted = total;
|
||||||
|
if (promoCode) {
|
||||||
|
const m = promoCode.match(/^PROMO(\d{1,2})$/);
|
||||||
|
if (m) {
|
||||||
|
const pct = Math.max(0, Math.min(100, Number(m[1])));
|
||||||
|
discounted = total * (1 - pct / 100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setEstimate({ total, discounted });
|
||||||
|
} catch (err) {
|
||||||
|
setEstimateError('Erreur lors de l\'estimation');
|
||||||
|
console.error(err);
|
||||||
|
} finally {
|
||||||
|
setEstimating(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
{lines.length > 1 && (
|
{lines.length > 1 && (
|
||||||
<button type="button" onClick={() => removeLine(index)}>Retirer</button>
|
<button type="button" onClick={() => removeLine(index)}>Retirer</button>
|
||||||
)}
|
)}
|
||||||
@@ -77,10 +129,27 @@ export default function Orders() {
|
|||||||
<input name="country" placeholder="Pays" value={address.country} onChange={handleAddressChange} required />
|
<input name="country" placeholder="Pays" value={address.country} onChange={handleAddressChange} required />
|
||||||
|
|
||||||
<h2>Paiement</h2>
|
<h2>Paiement</h2>
|
||||||
|
<label>Code promo
|
||||||
|
<input value={promoCode} onChange={(e) => { setPromoCode(e.target.value); setPromoValid(validatePromo(e.target.value)); }} placeholder="Code promo (optionnel)" />
|
||||||
|
</label>
|
||||||
|
{!promoValid && <div style={{ color: 'red' }}>Format de code promo invalide (ex: PROMO10)</div>}
|
||||||
<select value={paymentMethod} onChange={(e) => setPaymentMethod(e.target.value)}>
|
<select value={paymentMethod} onChange={(e) => setPaymentMethod(e.target.value)}>
|
||||||
<option value="CREDIT_CARD">Carte bancaire</option>
|
<option value="CREDIT_CARD">Carte bancaire</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<div style={{ marginTop: 8 }}>
|
||||||
|
<button type="button" onClick={handleEstimate} disabled={estimating}>
|
||||||
|
{estimating ? 'Estimation…' : 'Estimer le total'}
|
||||||
|
</button>
|
||||||
|
{estimateError && <div style={{ color: 'red' }}>{estimateError}</div>}
|
||||||
|
{estimate && (
|
||||||
|
<div>
|
||||||
|
<div>Total estimé : {estimate.total.toFixed(2)} €</div>
|
||||||
|
<div>Après promo : {estimate.discounted.toFixed(2)} €</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
<button type="submit" disabled={submitting}>
|
<button type="submit" disabled={submitting}>
|
||||||
{submitting ? 'Envoi…' : 'Valider la commande'}
|
{submitting ? 'Envoi…' : 'Valider la commande'}
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
|
import { useSubscription } from '../context/SubscriptionContext';
|
||||||
|
|
||||||
|
export default function Subscription() {
|
||||||
|
const { subscription, subscribe, cancelSubscription, PLANS } = useSubscription();
|
||||||
|
const [selectedPlan, setSelectedPlan] = useState('basic');
|
||||||
|
const [phoneNumber, setPhoneNumber] = useState('');
|
||||||
|
const [message, setMessage] = useState(null);
|
||||||
|
|
||||||
|
function handleSubscribe(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
subscribe(selectedPlan, phoneNumber);
|
||||||
|
setMessage({ success: true, text: 'Abonnement créé avec succès !' });
|
||||||
|
setPhoneNumber('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleCancel() {
|
||||||
|
cancelSubscription();
|
||||||
|
setMessage({ success: false, text: 'Abonnement annulé.' });
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<h1>Mon abonnement</h1>
|
||||||
|
|
||||||
|
{subscription && subscription.status === 'ACTIVE' ? (
|
||||||
|
<section>
|
||||||
|
<h2>Abonnement actif</h2>
|
||||||
|
<p>Plan : <strong>{subscription.planName}</strong></p>
|
||||||
|
<p>Prix : <strong>{subscription.price} € / mois</strong></p>
|
||||||
|
<p>Téléphone : {subscription.phoneNumber}</p>
|
||||||
|
<p>Depuis le : {new Date(subscription.startDate).toLocaleDateString('fr-FR')}</p>
|
||||||
|
<p>Statut : <strong style={{ color: 'green' }}>{subscription.status}</strong></p>
|
||||||
|
<button onClick={handleCancel}>Annuler l'abonnement</button>
|
||||||
|
</section>
|
||||||
|
) : (
|
||||||
|
<section>
|
||||||
|
<h2>Choisir un abonnement</h2>
|
||||||
|
<form onSubmit={handleSubscribe}>
|
||||||
|
<div>
|
||||||
|
{PLANS.map(plan => (
|
||||||
|
<label key={plan.id} style={{ display: 'block', marginBottom: '8px' }}>
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="plan"
|
||||||
|
value={plan.id}
|
||||||
|
checked={selectedPlan === plan.id}
|
||||||
|
onChange={() => setSelectedPlan(plan.id)}
|
||||||
|
/>
|
||||||
|
{' '}<strong>{plan.name}</strong> , {plan.price} € / mois , {plan.description}
|
||||||
|
</label>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<label>
|
||||||
|
Numéro de téléphone :
|
||||||
|
<input
|
||||||
|
type="tel"
|
||||||
|
value={phoneNumber}
|
||||||
|
onChange={e => setPhoneNumber(e.target.value)}
|
||||||
|
placeholder="0612345678"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<button type="submit">S'abonner</button>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{message && (
|
||||||
|
<p style={{ color: message.success ? 'green' : 'red' }}>{message.text}</p>
|
||||||
|
)}
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user