Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 49x 49x 75x 49x 49x 49x 49x 49x 49x 1x 1x 1x 49x 20x 20x 20x 49x 49x 41x 41x 41x 49x 41x 41x 49x 2x 2x 1x 3x 49x 8x 8x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 49x 29x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 1x 1x 2x 7x 1x | /**
* Page du panier de l'application ClientJO - Gestion des réservations
*
* Cette page permet aux utilisateurs de visualiser, modifier et valider leur panier
* de billets pour les Jeux Olympiques. Elle offre une interface complète pour
* la gestion des réservations avant le paiement.
*
* ## Structure de la page
* - **Header** : Navigation principale avec compteur du panier
* - **Groupement par événement** : Organisation claire des offres sélectionnées
* - **Contrôles de quantité** : Boutons +/- pour ajuster les réservations
* - **Récapitulatif financier** : Total et détails des montants
* - **Bouton de validation** : Processus de paiement
*
* ## Fonctionnalités principales
* - Affichage des offres groupées par événement
* - Modification des quantités avec contrôles +/-
* - Suppression complète d'offres (icône poubelle)
* - Calcul automatique des totaux
* - Validation et redirection vers le paiement
* - Vidage complet du panier
*
* ## Gestion d'état Redux
* - **useSelector** : Récupération des items du panier
* - **useDispatch** : Actions d'ajout/suppression
* - **Persistance** : Sauvegarde automatique en localStorage
* - **Synchronisation** : État partagé dans toute l'application
*
* ## Intégrations API
* - **EvenementService** : Récupération des détails d'événements
* - **OffreService** : Données des offres via useOffres hook
* - **PaiementService** : Processus de validation et paiement
*
* ## Interface utilisateur
* - Design responsive adapté à tous les écrans
* - Icônes intuitives pour les actions
* - Affichage des places restantes
* - Notifications pour le feedback utilisateur
*
* @module app/panier/page
* @group Pages
*/
'use client';
import {useCallback, useEffect, useState} from 'react';
import {useDispatch, useSelector} from 'react-redux';
import {OffrePanier} from '@/type/achat/offrePanier';
import {Evenement} from '@/type/evenement/evenement';
import {useOffres} from '@/hook/useOffre';
import {EvenementService} from '@/lib/api/service/evenementService';
import {addOneArticleToCart, clearCart, removeOneArticleFromCart} from '@/lib/reducer/panier/panierSlice';
import Image from 'next/image';
import Header from "@/components/header/Header";
import {PaiementService} from "@/lib/api/service/paiementService";
import {useRouter} from 'next/navigation';
import Notification from "@/components/common/Notification";
import Spinner from "@/components/common/Spinner";
import {NotificationProps} from "@/type/common/notification";
/**
* Page du panier pour la gestion des réservations de billets.
* Voir la documentation du module ci-dessus pour les détails complets.
*
* @returns Page complète de gestion du panier avec validation
*/
export default function PanierPage() {
const dispatch = useDispatch();
const router = useRouter();
const panierItems = useSelector((state: { panier: { items: OffrePanier[] } }) => state.panier.items);
const {offres} = useOffres();
const [evenements, setEvenements] = useState<Evenement[]>([]);
const [loading, setLoading] = useState(true);
const [notification, setNotification] = useState<NotificationProps | null>();
const [showNotification, setShowNotification] = useState(false);
const handleCloseNotification = useCallback(() => {
requestAnimationFrame(() => {
setShowNotification(false);
setNotification(null);
});
}, []);
useEffect(() => {
EvenementService.getAllEvenements().then(data => {
setEvenements(data);
setLoading(false);
});
}, []);
// Grouper les items du panier par événement
const panierParEvenement: Record<number, OffrePanier[]> = {};
panierItems.forEach(item => {
if (!panierParEvenement[item.evenementId]) {
panierParEvenement[item.evenementId] = [];
}
panierParEvenement[item.evenementId].push(item);
});
// Calcul du total du panier
const totalPanier = panierItems.reduce((acc, item) => {
const offre = offres.find(o => o.id === item.offreId);
return acc + (offre ? offre.montant * item.quantity : 0);
}, 0);
const handleClickDash = (item: OffrePanier) => {
const confirmed = window.confirm("Voulez-vous vraiment supprimer toute cette offre du panier ?");
if (!confirmed) return;
for (let i = 0; i < item.quantity; i++) {
dispatch(removeOneArticleFromCart({
evenementId: item.evenementId,
offreId: item.offreId
}));
}
};
const handleValidCart = useCallback(async (_force_failed: boolean = false) => {
try {
const reponse = await PaiementService.checkPaiement({
amount: totalPanier,
force_failed: _force_failed,
items: panierItems
});
const status = reponse.gateway_response.status;
switch (status) {
case "requires_confirmation":
setNotification({
message: "Paiement autorisé, en attente de confirmation.",
type: "info"
})
setShowNotification(true);
break;
case "succeeded":
dispatch(clearCart());
setNotification({
message: "Paiement validé ! Redirection en cours...",
type: "success"
})
setShowNotification(true);
setTimeout(() => {
router.push('/billets');
}, 1000);
break;
case "failed":
setNotification({
message: "Le paiement a échoué. Veuillez réessayer.",
type: "error"
})
setShowNotification(true);
break;
case "refunded":
setNotification({
message: "Paiement remboursé. Contactez le support.",
type: "info"
})
setShowNotification(true);
break;
default:
setNotification({
message: "Statut de paiement inconnu.",
type: "error"
})
setShowNotification(true);
}
} catch (err) {
setNotification({
message: "Une erreur inattendue est survenue.",
type: "error"
})
console.error("Erreur inattendue lors du paiement", err);
setShowNotification(true);
}
}, [dispatch, panierItems, totalPanier, router]);
if (loading) return (
<div className="flex flex-col items-center justify-center min-h-screen gap-3">
<Spinner/>
<span>Chargement du panier...</span>
</div>)
return (
<>
<Header/>
{showNotification && notification && (
<Notification
message={notification.message}
type={notification.type}
duration={notification.type === 'success' ? 1000 : 3000}
onCloseAction={handleCloseNotification}
/>
)}
<div className="w-screen min-h-screen bg-base-100 flex flex-col items-center justify-start p-0 m-0">
<div className="w-full max-w-2xl mt-8 p-4 bg-white rounded-xl shadow-lg border border-accent">
<h1 className="text-2xl font-bold mb-6 text-center text-accent">Mon panier</h1>
{Object.entries(panierParEvenement).length === 0 ? (
<div className="text-center text-black mt-10">Votre panier est vide.</div>
) : (
<>
{Object.entries(panierParEvenement).map(([evenementId, items]) => {
const evenement = evenements.find(e => e.id === Number(evenementId));
// Calcul du nombre de places réservées pour cet événement
const totalReservedPlaces = items.reduce((acc, item) => {
const offre = offres.find(o => o.id === item.offreId);
return acc + (offre ? offre.nb_personne * item.quantity : 0);
}, 0);
const remainingTickets = evenement ? evenement.nb_place_restante - totalReservedPlaces : 0;
return (
<div key={evenementId} className="mb-8 border-b pb-4">
<h2 className="text-lg md:text-xl font-semibold mb-2 text-accent flex flex-wrap items-center gap-2">
<span>{evenement ? evenement.description : `Événement #${evenementId}`}</span>
{evenement && (
<span
className="ml-2 text-sm md:text-base font-normal text-black bg-base-200 rounded px-2 py-1">Places restantes : {remainingTickets}</span>
)}
</h2>
<ul className="divide-y divide-base-200">
{items.map(item => {
const offre = offres.find(o => o.id === item.offreId);
const montant = offre ? offre.montant * item.quantity : 0;
const canAdd = offre && remainingTickets >= offre.nb_personne;
return (
<li key={item.offreId}
className="flex flex-wrap md:flex-nowrap items-center justify-between py-2 text-black gap-2 md:gap-4">
<span
className="font-medium text-black min-w-[100px] md:min-w-[140px] truncate">{offre ? offre.libelle : `Offre #${item.offreId}`}</span>
<div className="flex items-center gap-1 md:gap-2">
<button
className="p-1 rounded hover:bg-gray-200 disabled:opacity-50 border border-base-200"
onClick={() => dispatch(removeOneArticleFromCart({
evenementId: item.evenementId,
offreId: item.offreId
}))}
aria-label="Retirer une offre"
>
<Image src="/images/minus.png" alt="moins" width={20}
height={20}/>
</button>
<span
className="text-lg font-medium text-black min-w-[30px] text-center">{item.quantity}</span>
<button
className="p-1 rounded hover:bg-gray-200 disabled:opacity-50 border border-base-200"
disabled={!canAdd}
onClick={() => dispatch(addOneArticleToCart({
evenementId: item.evenementId,
offreId: item.offreId
}))}
aria-label="Ajouter une offre"
>
<Image src="/images/add.png" alt="plus" width={20}
height={20}/>
</button>
</div>
<span
className="font-bold text-black min-w-[60px] text-right">{montant} €</span>
<button
className="p-1 rounded hover:bg-red-100 border border-base-200"
onClick={()=>handleClickDash(item)}
aria-label="Vider l'offre du panier"
>
<Image src="/images/delete.png" alt="poubelle" width={20}
height={20}/>
</button>
</li>
);
})}
</ul>
</div>
);
})}
{/* Total panier et bouton valider */}
<div className="flex flex-col md:flex-row items-center justify-between mt-8 gap-4">
<div className="text-xl font-bold text-black">Total : <span
className="text-accent">{totalPanier} €</span></div>
<button
className="bg-accent text-white font-semibold px-8 py-3 rounded-lg shadow hover:bg-accent/90 transition text-lg w-full md:w-auto"
onClick={() => handleValidCart()}
disabled={totalPanier === 0}
>
Valider le panier
</button>
<button
className="bg-error text-white font-semibold px-8 py-3 rounded-lg shadow hover:bg-accent/90 transition text-lg w-full md:w-auto"
onClick={() => handleValidCart(true)}
disabled={totalPanier === 0}
>
achat en echec
</button>
</div>
</>
)}
</div>
</div>
</>
);
}
|